Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use redis as a database for cache and session

Shulou Source: shulou.com Published: 2022-06-03 04:46:38 11月14日 Update

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!

Tags: Data database content article method learning help code function chunk password array way easy to understand is in more organization knowledge port ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi Redmi Apple macOS