redis作为cache和session的数据库的使用

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()
}

redis作为cache和session的数据库的使用

要安装github.com/gomodule/redigo/redis才能使用(虽然github.com/astaxie/beego/cache/redis继承了它)

猜你喜欢

转载自blog.51cto.com/5660061/2373866