1 Star 4 Fork 2

aesoper/cache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
aesoper 提交于 2021-05-25 17:04 +08:00 . fix(bugs): 修复bug,移除无用依赖
package cache
import (
"gitee.com/aesoper/cache/factory"
"gitee.com/aesoper/cache/memcache"
"gitee.com/aesoper/cache/memory"
"gitee.com/aesoper/cache/redis"
)
type Cfg struct {
// 启用的缓存驱动 值为 redis|redis_cluster|memcache
Driver string `mapstructure:"driver" json:"driver"`
Redis redis.Options `mapstructure:"redis" json:"redis"`
RedisCluster redis.ClusterOptions `mapstructure:"redisCluster" json:"redisCluster"`
Memcache memcache.Options `mapstructure:"memcache" json:"memcache"`
Memory memory.Option `mapstructure:"memory" json:"memory"`
}
func New(cfg Cfg) (c factory.Cache, err error) {
var op factory.CacheOption
if len(cfg.Driver) == 0 {
cfg.Driver = "memory"
}
switch cfg.Driver {
case "redis":
op = redis.WithRedisOptions(cfg.Redis)
break
case "redis_cluster":
op = redis.WithRedisClusterOptions(cfg.RedisCluster)
break
case "memcache":
op = memcache.WithMemcacheOption(cfg.Memcache)
break
case "memory":
op = memory.WithMemoryOption(cfg.Memory)
break
default:
op = redis.WithRedisOptions(cfg.Redis)
}
return factory.NewCache(cfg.Driver, op)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aesoper/cache.git
git@gitee.com:aesoper/cache.git
aesoper
cache
cache
master

搜索帮助