A non-locking in-process ASP.NET session state store

后端 未结 5 1506
挽巷
挽巷 2020-12-06 05:34

I\'m using ASP.NET\'s in-process session state store. It locks access to a session exclusively, and which means concurrent requests to the same session are served sequential

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 06:11

    You will have to write your own Session State Provider, it is not that hard as it seems. I wouldn't recommend you to use Cache because as Darin said Cache is not reliable enough, for example items there expire and/or removed from cache when there is not enough memory.

    In your Session Store Provider you can lock items of session instead of the whole Session state (which suits us in our project) when writing, the essential code for this should be in ISessionStateItemCollection's implementation (in indexer), something like this:

    public object this[string name]
    {
        get
        {
            //no lock, just returning item (maybe immutable, it depends on how you use it)
        }
        set
        {
            //lock here
        }
    }
    

    For our project we have created implementation which stores items in AppFabric Cache and rely on GetAndLock, PutAndUnlock methods of the cache. If you have in-proc Session then you might just need to lock(smthg) {}

    Hope this helps.

提交回复
热议问题