In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to achieve Session in distributed Redis. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
What I want to use here is a custom Session provided by netizens. I need to inherit System.Web.SessionState.SessionStateStoreProviderBase to implement my own SessionStateStoreProvide. Here is how to implement a custom RedisSessionStateStore.
SessionStateStoreProviderBase
SessionStateStoreProviderBase is a required member of the session state provider that defines the datastore provided by asp.net. Like the common InProcSessionStateStore (mode= "InProc") and SqlSessionStateStore (mode= "SQLServer"), they all inherit the storage implemented by SessionStateStoreProviderBase. Let's take a look at msdn's definition of its members.
Member description
InitializeRequest method
Performs all initialization operations necessary for the session-state storage provider.
EndRequest method
Perform all cleanup operations necessary for the session-state storage provider.
Dispose method
Releases all resources that are no longer used by the session-state storage provider.
GetItemExclusive method
Retrieves the value and information of the session from the session data store and locks the session item data in the data store for the duration of the request. The GetItemExclusive method sets several output parameter values that notify the status of the current session state item in the data store to the calling SessionStateModule.
If no session item data is found in the datastore, the GetItemExclusive method sets the locked output parameter to false and returns null. This causes SessionStateModule to call the CreateNewStoreData method to create a new SessionStateStoreData object for the request.
If the session item data is found in the datastore but the data is locked, the GetItemExclusive method sets the locked output parameter to true, the lockAge output parameter to the difference between the current date and time and the lock date and time of the item, the lockId output parameter to the lock identifier retrieved from the data store, and returns null. This causes SessionStateModule to call the GetItemExclusive method again after a half-second interval to try to retrieve session item information and obtain a lock on the data. If the setting value of the lockAge output parameter exceeds the ExecutionTimeout value, SessionStateModule calls the ReleaseItemExclusive method to clear the lock on the session item data, and then calls the GetItemExclusive method again.
If the regenerateExpiredSessionId property is set to true, the actionFlags parameter is used for sessions whose Cookieless property is true. The actionFlags value set to InitializeItem (1) indicates that the item in the session data store is a new session that needs to be initialized. You can create uninitialized items in the session data store by calling the CreateUninitializedItem method. If the item in the session data store has been initialized, the actionFlags parameter is set to zero.
If the provider supports no Cookie sessions, set the actionFlags output parameter to the value returned by the current item from the session data store. If the actionFlags parameter value of the requested session store item is equal to the InitializeItem enumeration value (1), the GetItemExclusive method should set the value in the datastore to zero after setting the actionFlags out parameter.
GetItem method
This method performs the same action as the GetItemExclusive method, except that it does not attempt to lock session items in the datastore. The GetItem method is called when the EnableSessionState property is set to ReadOnly.
SetAndReleaseItemExclusive method
Take the HttpContext instance of the current request, the SessionID value of the current request, the SessionStateStoreData object containing the current session value to store, the lock identifier of the current request, and a value indicating whether the data to be stored belongs to a new session or an existing session.
If the newItem parameter is true, the SetAndReleaseItemExclusive method inserts a new item into the datastore using the value provided. Otherwise, existing items in the data store are updated with the values provided and any locks on the data are released. Note that only session data for the current application that matches the provided SessionID and lock identifier values is updated.
After calling the SetAndReleaseItemExclusive method, SessionStateModule calls the ResetItemTimeout method to update the expiration date and time of the session item data.
ReleaseItemExclusive method
Takes the HttpContext instance of the current request, the SessionID value of the current request, and the lock identifier of the current request as input, and releases the lock on the item in the session data store. This method is called when the GetItem or GetItemExclusive method is called and the datastore specifies that the requested item is locked, but the lock time has exceeded the ExecutionTimeout value. This method clears the lock and releases the requested item for use by other requests.
RemoveItem method
This method is called when the Abandon method is called.
CreateUninitializedItem method
Take the HttpContext instance of the current request, the SessionID value of the current request, and the lock identifier of the current request as input, and add an uninitialized item with an actionFlags value of InitializeItem to the session data store.
If the regenerateExpiredSessionId property is set to true, the CreateUninitializedItem method is used for no Cookie sessions, which causes SessionStateModule to generate a new SessionID value when an expired session ID is encountered.
The process of generating a new SessionID value requires the browser to redirect to the URL that contains the newly generated session ID. The CreateUninitializedItem method is called during an initial request that contains an expired session ID. After SessionStateModule gets a new SessionID value to replace the expired session ID, it calls the CreateUninitializedItem method to add an uninitialized item to the session-state data store. The browser is then redirected to the URL that contains the newly generated SessionID value. If there are uninitialized items in the session data store, you can ensure that a redirect request containing the newly generated SessionID value is treated as a new session and not mistaken for an expired session.
Uninitialized items in the session data store are associated with newly generated SessionID values and contain only default values, including expiration date and time and values corresponding to the actionFlags parameters of the GetItem and GetItemExclusive methods. Uninitialized items in the session state store should contain an actionFlags value equal to the InitializeItem enumeration value (1). This value is passed to SessionStateModule by the GetItem and GetItemExclusive methods, and specifies that the current session is a new session for SessionStateModule. SessionStateModule then initializes the new session and raises the Session_OnStart event.
CreateNewStoreData method
Takes the currently requested HttpContext instance and the Timeout value of the current session as input and returns a new SessionStateStoreData object with an empty ISessionStateItemCollection object, a HttpStaticObjectsCollection collection, and the specified timeout value. Use the GetSessionStaticObjects method to retrieve an HttpStaticObjectsCollection instance of an ASP.NET application.
The above definition is a bit long, but in fact, many of them point out that native Session is implemented in a single-threaded way, and when multiple reads are performed, the lock will be added and the others will wait. The following implementation removes these locks and speeds up concurrent reads and writes.
Implement your own RedisSessionStateStore
Inheriting SessionStateStoreProviderBase to implement your own RedisSessionStateStore is also very simple. You only need to inherit SessionStateStoreProviderBase to rewrite CreateNewStoreData,CreateUninitializedItem,GetItem and other methods. Please post the code below and refer to the InProcSessionStateStore implementation.
It is also easy to use, modify web.config
You can then keep the original code unchanged, like Session ["UserCode"] = "admin", but now Session has distributed features and supports cross-domain. The disadvantage of this method is that the key-value pairs need to be deserialized and serialized in GetItem and SetAndReleaseItemExclusive, but the performance of storing a large amount of data is greatly reduced compared with the way provided by the system, so you need to consider your own actual scenarios when using it.
The above is how Redis implements Session in distributed environment. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 234
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.