
让我们一起支持群主维护simple-admin 社群吧!!!
不能加入星球的朋友记得来点个Star!!
https://github.com/suyuan32/simple-admin-core
一、redis框架推荐
1、go-zero redis
go-zero 本身整合了个redis框架
优势:在svc中注册后,全局直接调用
,且整合了redis分布式锁
https://github.com/zeromicro/go-zero/core/stores/redis
2、redis官方版本
推荐使用 v9版本
优势:使用更加灵活
,可以控制返回值
https://github.com/redis/go-redis
二、2个框架与redis
官方文档:
1、go-zero整合v9 且兼容go-zero redis
这里面简单介绍一下2个框架如何整合redis的
1.1 config
这里simple-admin 框架
整合了redis官方v9版本
且github地址:https://github.com/suyuan32/simple-admin-common
go-zero的使用的是自己整合的redis
github.com/zeromicro/go-zero/core/stores/cache
package config
import (
"github.com/suyuan32/simple-admin-common/config"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/core/stores/cache"
)
type Config struct {
rest.RestConf
// simple-admin
RedisConf config.RedisConf
// go-zero
Cache cache.CacheConf
}
1.2 svc
svc/service_context.go (根据生成习惯也能是:servicecontext.go)
package svc
import (
"github.com/zeromicro/go-zero/core/stores/redis"
"redis1 "github.com/redis/go-redis/v9"
)
type ServiceContext struct {
Redis redis1.UniversalClient
Config config.Config
RedisCache *redis.Redis
}
func NewServiceContext(c config.Config) *ServiceContext {
// 初始化 cache,其中 WithLimit 可以指定最大缓存的数量
cCache, err := collection.NewCache(time.Minute, collection.WithLimit(10000))
if err != nil {