Data cache vs session object in ASP.Net

前端 未结 5 1208
眼角桃花
眼角桃花 2021-01-02 03:53

Should dynamic business objects for a site be stored in the users session or use ASP.Net caching (objects such as orders, profile information etc)?

I have worked wi

5条回答
  •  温柔的废话
    2021-01-02 04:25

    Caching is just that -- caching. You can never rely on entries being there, so no assumptions must be made in that respect: be prepared to go straight to the DB (or wherever else) to refetch data.

    Session, on the other hand, is more suited towards storing objects, though personally I try to avoid session store in favour of a DB. I usually do that by abstracting away the store behind an opaque ISessionStoreService interface:

    interface ISessionStore
    {
        T GetEntry(string key);
        void SaveEntry(string key, T entry);
    }
    

    and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore, DbSessionStore or whatever.

提交回复
热议问题