[开源] Goravel(Golang Web 框架) - 新增 Cache 模块
关于 Goravel
Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框架。 作为一个起始脚手架帮助 Golang 开发者快速构建自己的应用。
项目地址:https://github.com/goravel/goravel
欢迎 star 与 issues :)
缓存模块
介绍
Goravel 提供了可拓展的缓存模块。该模块可以使用 facades.Cache
进行操作。
配置
在 config/cache.php
中进行所有自定义配置。允许配置不同的缓存驱动,默认使用 redis
,你也可以自定义驱动,可以进入配置文件进行查看。
可用的缓存驱动
名称 | 描述 |
---|---|
| Redis 驱动 |
| 自定义驱动 |
缓存使用
从缓存中获取数据
value := facades.Cache.Get("goravel", func() interface{} {
return "default"
})
你可以传递一个 func
作为默认值。如果指定的数据在缓存中不存在,将返回 func
的结果。传递闭包的方法允许你从数据库或其他外部服务中获取默认值。注意闭包结构 func() interface{}
。
value := facades.Cache.Get("goravel", func() interface{} {
return "default"
})
检查缓存项是否存在
value := facades.Cache.Has("goravel")
获取和存储
有时你可能想从缓存中获取一个数据,而当请求的缓存项不存在时,程序能为你存储一个默认值。
value, err := facades.Cache.Remember("goravel", 5 * time.Second, func() interface{} {
return "goravel"
})
如果缓存中不存在你想要的数据时,则传递给 Remember
方法的闭包将被执行,然后将其结果返回并放置到缓存中。
你可以使用 RememberForever
方法从缓存中获取数据或者永久存储它:
value, err := facades.Cache.RememberForever("goravel", func() interface{} {
return "default"
})
获取和删除
value := facades.Cache.Pull("goravel", "default")
在缓存中存储数据
err := facades.Cache.Put("goravel", "value", 5 * time.Second)
如果缓存的过期时间设置为 0, 则缓存将永久有效:
err := facades.Cache.Put("goravel", "value", 0)
只存储没有的数据
Add
方法将只存储缓存中不存在的数据。如果存储成功,将返回 true
,否则返回 false
:
res := facades.Cache.Add("goravel", "value", 5 * time.Second)
数据永久存储
Forever
方法可用于将数据持久化存储到缓存中。因为这些数据不会过期,所以必须通过 Forget
方法从缓存中手动删除它们:
res := facades.Cache.Forever("goravel", "value")
从缓存中删除数据
res := facades.Cache.Forget("goravel")
你可以使用 Flush
方法清空所有的缓存:
res := facades.Cache.Flush()
添加自定义缓存驱动
配置
如果你想定义一个完全自定义的驱动,可以在 config/cache.php
配置文件中指定 custom
驱动类型。
然后包含一个 via
选项,实现一个 frameworkcontractscacheStore
结构:
//config/cache.php
"stores": map[string]interface{}{
"redis": map[string]interface{}{
"driver": "redis",
"connection": "default",
},
"custom": map[string]interface{}{
"driver": "custom",
"via": Logger{},//自定义驱动
},
},
编写驱动
实现 frameworkcontractscacheStore
接口,并配置到 config/cache.go
即可。文件可以统一储存到 app/extensions
文件夹中(可修改)。
//frameworkcontractscacheStore
package cache
import "time"
type Store interface {
//Get Retrieve an item from the cache by key.
Get(key string, defaults interface{}) interface{}
//Has Determine if an item exists in the cache.
Has(key string) bool
//Put Store an item in the cache for a given number of seconds.
Put(key string, value interface{}, seconds time.Duration) error
//Pull Retrieve an item from the cache and delete it.
Pull(key string, defaults interface{}) interface{}
//Add Store an item in the cache if the key does not exist.
Add(key string, value interface{}, seconds time.Duration) bool
//Remember Get an item from the cache, or execute the given Closure and store the result.
Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)
//RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
RememberForever(key string, callback func() interface{}) (interface{}, error)
//Forever Store an item in the cache indefinitely.
Forever(key string, value interface{}) bool
//Forget Remove an item from the cache.
Forget(key string) bool
//Flush Remove all items from the cache.
Flush() bool
}
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击