Data cache vs session object in ASP.Net

前端 未结 5 1186
眼角桃花
眼角桃花 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:23

    If the objects are shareable between user sessions, then use the cache. If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data.

    0 讨论(0)
  • 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<T>(string key);
        void SaveEntry<T>(string key, T entry);
    }
    

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

    0 讨论(0)
  • 2021-01-02 04:41

    Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. Imagine you that you have a process of getting 1000 record from database (and it take about 3 seconds) and you will need it for a few minutes. You can store your objects in Cache and set expire date, priority and dependency to it (like SqlDependency or FileDependency), so for next requests you can use Cached data instead of retriving it from database. You can store your object in Session but you can not set dependency for Session by default. Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session).

    0 讨论(0)
  • 2021-01-02 04:43

    The ASP.NET system cache is global to the application where as the session is unique to the current user. If you chose to use the global cache to store objects you would need to create an object identification strategy so you could get the correct objects for your per user basis.

    If you are looking to enhance performance you would be better off replacing the ASP.NET session state with a distributed memory cache such as Microsoft's velocity. Microsoft has posted articles on how to replace session usage to target Velocity. You could also use Memcache or other similar products in a related fashion.

    0 讨论(0)
  • 2021-01-02 04:46

    Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application.
    The key to save in one or the other is to determine if what are you trying to store will be user-only data or it needs to be shared in all the application.

    Session => A webpage with a step by step interface (an online test for example).
    Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page).
    Hope this helps.

    0 讨论(0)
提交回复
热议问题