cache 模块
2 分钟阅读
Cache Module 缓存模块
Beego’s cache module is used for caching data, inspired by database/sql
. It supports four cache providers: file, memcache, memory and redis. You can install it by:
Beego 的缓存模块用于缓存数据,灵感来自 database/sql
。它支持四种缓存提供程序:文件、memcache、内存和 redis。您可以通过以下方式安装它:
github.com/beego/beego/v2/client/cache
If you use the memcache
or redis
provider, you should first install:
如果您使用 memcache
或 redis
提供程序,则应首先安装:
go get -u github.com/beego/beego/v2/client/cache/memcache
and then import: 然后导入:
import _ "github.com/beego/beego/v2/client/cache/memcache"
Basic Usage 基本用法
First step is importing the package:
第一步是导入包:
import (
"github.com/beego/beego/v2/client/cache"
)
Then initialize a global variable object:
然后初始化一个全局变量对象:
bm, err := cache.NewCache("memory", `{"interval":60}`)
Then we can use bm
to modify the cache:
然后我们可以使用 bm
来修改缓存:
bm.Put("astaxie", 1, 10*time.Second)
bm.Get("astaxie")
bm.IsExist("astaxie")
bm.Delete("astaxie")
Provider Settings 提供程序设置
Here is how to configure the four providers:
以下是配置四个提供程序的方法:
memory
interval
stands for GC time, which means the cache will be cleared every 60s:
interval
代表 GC 时间,这意味着缓存每 60 秒清除一次:{"interval":60}
file
{"CachePath":"./cache","FileSuffix":".cache","DirectoryLevel":2,"EmbedExpiry":120}
redis
redis uses redigo redis 使用 redigo
{"key":"collectionName","conn":":6039","dbNum":"0","password":"thePassWord"}
- key: the Redis collection name key:Redis 集合名称
- conn: Redis connection info conn:Redis 连接信息
- dbNum: Select the DB having the specified zero-based numeric index. dbNum:选择具有指定基于零的数字索引的数据库。
- password: the password for connecting password-protected Redis server password:用于连接受密码保护的 Redis 服务器的密码
memcache
memcache uses vitess memcache 使用 vitess
{"conn":"127.0.0.1:11211"}
Creating your own provider 创建您自己的提供程序
The cache module uses the Cache interface, so you can create your own cache provider by implementing this interface and registering it.
缓存模块使用 Cache 接口,因此您可以通过实现此接口并注册它来创建自己的缓存提供程序。
|
|
Register your provider:
注册您的提供程序:
func init() {
cache.Register("myowncache", NewOwnCache())
}