Home  >  Article  >  Database  >  How to save session in redis

How to save session in redis

(*-*)浩
(*-*)浩Original
2019-11-21 10:12:373348browse

How to save session in redis

Issues to consider when storing sessions in Redis:

How is session data stored in Redis? When does session attribute change trigger storage? (Recommended learning: Redis video tutorial)

Implementation:

Considering that the data in the session has a map-like structure, redis is used Hash is more suitable for storing session data. If a single value is used to store session data, there will be a problem of session overwriting without locking. Therefore, using hash to store the session only saves the data that changes the session attribute each time, which avoids Lock handling, better performance.

If storage is triggered every time a session attribute is changed, multiple redis write operations will be triggered when more session attributes are changed, which will also have an impact on performance. We process each request after Finally, do a session write and write the changed attributes.

If no session changes are made this time, redis writing will not be done. Only when the unchanged session exceeds a time threshold (the threshold of session refresh expiration time is not changed), it will be triggered. The session is saved so that the validity period of the session can be extended.

Two implementation methods:

session centralized storage (redis, memcached, hbase, etc.).

Copying session data on different servers. The advantages and disadvantages of the two methods should be clear to everyone.

Implementation plan based on session centralized storage:

Add Filter, intercept requests, and wrap HttpServletRequest

Rewrite the getSession method and obtain it from session storage session data, return the customized HttpSession implementation

After generating a new Session, write the sessionid into the cookie

The above is the detailed content of How to save session in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn