1. session Storage Structure in redis 2
stay The previous chapter When we talk about spring session storage in redis, we can see that every session storage in redis generates three records. The record format is as follows:
This is very troublesome, why can't one by one correspond, do each other's angels, do a pair of three, very influential weathering ah. Whether it is moral decay or human distortion, let's go into redis and look at the specific data content:
The first k-v stores the Session id, a Redis data structure of Set type. The last value of 1570550340000 in this key is a timestamp, calculated by scrolling the Session expiration time to the next minute. That's the value inside.
The second k-v, a String type, is used to indicate that Session expires in Redis. This K-V does not store any useful data, but is set to indicate that Session expires. The expiration time of this key in Redis is the expiration time interval of Session.
The third k-v is used to store the details of Session, which is the hash type, including the creation time, expiration time interval, recent access time, attributes, and so on. The expiration time of this k is the maximum expiration time of Session + 5 minutes. If the default maximum expiration time is 30 minutes, then the K expiration time is 35 minutes.
At this point, there will be a soul torture, on the storage of a session, why do three records? The interface specification for HttpSession is mentioned here.
session is also a data, but it is different from ordinary data. Looking at javadoc of httpsession, we can see that adding and removing sessions can trigger events.
The premise of triggering is that the object implements the HttpSession BindingListener interface, and then we'll look at the HttpSession BindingListener part.
In it, HttpSession Binding Event is used to notify session attribute s of changes.
This is a typical listener model and the main manifestation of message mechanism. When spring session achieves distributed session through redis, it achieves standard session change notification through redis message mechanism. Let's first look at the specific approach. The message notification function of redis.
2. What! redis supports publishing and subscribing messages?
Redis launched the Keyspace Notifications feature from version 2.8.0. Keyspace Notifications allows clients to receive operational events that affect keys and values in the database in a Sub/Pub mode. Specifically, these operational events are del, expire, set, rpop, etc.
Redis does not open keyspace notifications by default because it consumes the cpu after opening, so opening requires additional configuration of redis.conf file
############################# EVENT NOTIFICATION ############################## # Redis can notify Pub/Sub clients about events happening in the key space. # This feature is documented at http://redis.io/topics/notifications # # For instance if keyspace events notification is enabled, and a client # performs a DEL operation on key "foo" stored in the Database 0, two # messages will be published via Pub/Sub: # # PUBLISH __keyspace@0__:foo del # PUBLISH __keyevent@0__:del foo # # It is possible to select the events that Redis will notify among a set # of classes. Every class is identified by a single character: # # K Keyspace events, published with __keyspace@<db>__ prefix. # E Keyevent events, published with __keyevent@<db>__ prefix. # g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... # $ String commands # l List commands # s Set commands # h Hash commands # z Sorted set commands # x Expired events (events generated every time a key expires) # e Evicted events (events generated when a key is evicted for maxmemory) # A Alias for g$lshzxe, so that the "AKE" string means all the events. # # The "notify-keyspace-events" takes as argument a string that is composed # of zero or multiple characters. The empty string means that notifications # are disabled. # # Example: to enable list and generic events, from the point of view of the # event name, use: # # notify-keyspace-events Elg # # Example 2: to get the stream of the expired keys subscribing to channel # name __keyevent@0__:expired use: # # notify-keyspace-events Ex # # By default all notifications are disabled because most users don't need # this feature and the feature has some overhead. Note that if you don't # specify at least one of K or E, no events will be delivered. notify-keyspace-events "Ex"
The key is the last sentence, Ex means you can see the notes above. After startup, as long as we subscribe to a particular channel, we will be notified of the data changes under that channel.
If you change the configuration and restart redis, you can open the message notification.
There are two ways to have an expired key in Redis:
- When visited, it was found to be out of date
- Redis background step-by-step search for expired keys
Question time visits found that expiration may be a long time later, so how can you know when the key expires?
There is a timed task in spring-session. Each full minute queries the expired SessionId in the corresponding spring:session:expirations: full minute timestamp, and then visits the session Id again, that is, spring:session:sessions:expires:SessionId, so that Redis can produce key expiration events in time, that is, Session Outdated events. The time of this timed task can be configured, and the configuration parameters are cleanupCron at the end of the previous chapter.
public class RedisHttpSessionConfiguration extends SpringHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware, SchedulingConfigurer { static final String DEFAULT_CLEANUP_CRON = "0 * * * * *"; private String cleanupCron = DEFAULT_CLEANUP_CRON;
The default is to clean up the expired cache once a minute and make changes according to your needs.
3. Summary
- Each session data stored in spring session s to redis will have three records, one for session itself and two for session expiration
- Redis can open the Keyspace Notifications service, and there will be notification messages when redis data changes after opening.
- spring session can configure two kinds of expiration information stored in redis session data by timing task scanning. The configuration of timing task is in cleanupCron.
I'm not really going to go into detail here. You can see the details about spring session. This series of articles It's very good, but I'm tired of watching it.