beego中cache缓存模块redis

注意加上  _ “github.com/astaxie/beego/cache/redis” 这样一句话

package main

import (
   "encoding/json"
   "fmt"
   "github.com/astaxie/beego"
   "github.com/astaxie/beego/cache"
   _ "github.com/astaxie/beego/cache/redis"
   "time"

   "strings"
)

func main() {


   mstr:=map[string]string{}

   mstr["key"]="chatengine"
   mstr["conn"]=":6379"
   mstr["dbNum"]="0"

   bytes, _ := json.Marshal(mstr)

   //从redis缓存中拿数据拿数据
   cache_conn, err := cache.NewCache("redis", string(bytes))
   if err != nil {
      fmt.Println(err)
   }


   timeoutDuration := 10000000 * time.Second

   err = cache_conn.Put("wilson1231111", "xu", timeoutDuration)
   if err != nil {
      fmt.Println("数据读取出错,错误为:",err)
   }else {
      fmt.Println("redis 读取正常")
   }

   if areaData := cache_conn.Get("area");areaData!=nil{
      beego.Info("get data from cache===========")
      //resp["data"] = areaData
      fmt.Println("从redis中读取出的数据为:",areaData)
   }else {
      fmt.Println("需要读取MySQL")
   }

   fmt.Println(strings.Index("i am a good man", "haha"))
}

你可能感兴趣的:(golang)