Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and mechanism of redis cache storage Session

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the principle and mechanism of redis cache storage Session". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the principle and mechanism of redis cache storage Session".

Storing Session based on Redis

If we want to save session data to redis, we just need to change the storage engine of session to redis.

An example of using redis as a storage engine:

First install the package go get github.com/gin-contrib/sessions/redis// of the redis storage engine to initialize the redis-based storage engine / / Parameter description: / / 1st parameter-redis maximum number of idle connections / / 2nd parameter-numerical communication protocol tcp or udp / / 3rd parameter-redis address, format Host:port / / 4th parameter-redis password / / 5th parameter-session encryption key store, _: = redis.NewStore (10, "tcp", "localhost:6379", "", [] byte ("secret")) r.Use (sessions.Sessions ("mysession") Store)) set session expiration time / / configure the expiration time of session session.Options (sessions.Options {MaxAge:3600*6}) / / 6 hours = 60 / 60 / 6 distributed acquisition Session: (redis)

View current reds value: keys * set key vlaue set key value for get key View value (encryption) package mainimport ("github.com/gin-contrib/sessions"github.com/gin-contrib/sessions/cookie"github.com/gin-contrib/sessions/redis"github.com/gin-gonic/gin") func main () {r: = gin.Default () / configure session's middleware store _: = redis.NewStore (10, "tcp", "localhost:6379", "", [] byte ("secret")) r.Use (sessions.Sessions ("mysession", store)) / / initMiddleware: configure routing middleware r.GET ("/") Func (c * gin.Context) {/ / set sessions session: = sessions.Default (c) / / configure the expiration time of session session.Options (sessions.Options {MaxAge:3600*6}) / / 6 hours = 60 hours 60 hours 6 session.Set ("username") "Chengqiang") / / Save sessions: session.Save () c.String (must be called) for other pages ("gin homepage")}) r.GET ("/ news") Func (c * gin.Context) {/ / get sessions session: = sessions.Default (c) username: = session.Get ("username") c.String (200,200, "username=%v", username)})} Thank you for reading The above is the content of "what is the principle mechanism of redis cache storage Session". After the study of this article, I believe you have a deeper understanding of what the principle mechanism of redis cache storage Session is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report