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

How to use redis as a database for cache and session

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "redis as a cache and session database how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "redis as a cache and session database how to use" this article.

Package mainimport (_ ". / routers"fmt"github.com/astaxie/beego" _ "github.com/astaxie/beego/cache/redis"github.com/astaxie/beego/cache"log"time") type hashes struct {name string age int sex int} func main () {/ / key adds a beego: adapter, err: = cache.NewCache ("redis") before the key. `{"key": "beego", "conn": ": 6379", "dbNum": "0", "password": ""}`) if err! = nil {log.Fatal (err)} err = adapter.Put ("account", "Zhang San", 3600 * time.Second) if err! = nil {log.Fatal (err)} fmt.Println (fmt.Sprintf ("% s") Adapter.Get ("account")) / / the way to store the array / hash err = adapter.Put ("hashes", hashes {name: "dingyi", age:18, sex:1}, 3600 * time.Second) if err! = nil {log.Fatal (err)} fmt.Println (fmt.Sprintf ("% s", adapter.Get ("hashes")) beego.Run ()}

Github.com/gomodule/redigo/redis must be installed to use it (although github.com/astaxie/beego/cache/redis inherits it)

To introduce:

"github.com/astaxie/beego/cache"

_ "github.com/astaxie/beego/cache/redis"

Session uses redis as the method to store the database:

As long as it is very simple to do a configuration, do not need a large piece of code online, app.conf:

SessionProvider = redissessionProviderConfig = 127.0.0.1 purl 6379100

Among them, 127.0.0.1 6379 is the ip and port, 100 is the connection pool, and the last one is the password.

Plus, in the main method:

Beego.BConfig.WebConfig.Session.SessionOn = true

Of course, as above, you also need to install: github.com/gomodule/redigo/redis

The above is all the contents of the article "how to use redis as a database for cache and session". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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: 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