W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
beego 的 cache 模塊是用來做數據緩存的,設計思路來自于 database/sql,目前支持 file、memcache、memory 和 redis 四種引擎,安裝方式如下:
go get github.com/astaxie/beego/cache
如果你使用memcache 或者 redis 驅動就需要手工安裝引入包
go get -u github.com/astaxie/beego/cache/memcache
而且需要在使用的地方引入包
import _ "github.com/astaxie/beego/cache/memcache"
首先引入包:
import (
"github.com/astaxie/beego/cache"
)
然后初始化一個全局變量對象:
bm, err := cache.NewCache("memory", `{"interval":60}`)
然后我們就可以使用bm增刪改緩存:
bm.Put("astaxie", 1, 10*time.Second)
bm.Get("astaxie")
bm.IsExist("astaxie")
bm.Delete("astaxie")
目前支持四種不同的引擎,接下來分別介紹這四種引擎如何設置:
cache 模塊采用了接口的方式實現,因此用戶可以很方便的實現接口,然后注冊就可以實現自己的 Cache 引擎:
type Cache interface {
Get(key string) interface{}
GetMulti(keys []string) []interface{}
Put(key string, val interface{}, timeout time.Duration) error
Delete(key string) error
Incr(key string) error
Decr(key string) error
IsExist(key string) bool
ClearAll() error
StartAndGC(config string) error
}
用戶開發(fā)完畢在最后寫類似這樣的:
func init() {
Register("myowncache", NewOwnCache())
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: