redis作為cache和session的數據庫的使用
阿新 • • 發佈:2019-04-03
router new redis text package route github cbc 51cto
package main
import (
_ "./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的作用是在鍵前面加個:beego:
adapter, err := cache.NewCache("redis", `{"key":"beego","conn":":6379","dbNum":"0","password":""}`)
if err != nil {
log.Fatal(err)
}
err = adapter.Put("account", "張三", 3600 * time.Second)
if err != nil {
log.Fatal(err)
}
fmt.Println(fmt.Sprintf("%s", adapter.Get("account")))
//存數組/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才能使用(雖然github.com/astaxie/beego/cache/redis繼承了它)
redis作為cache和session的數據庫的使用