1 Star 0 Fork 0

yzsunjianguo / common_pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 11:08 . fix
package gotest
import (
"context"
"gitee.com/yzsunjianguo/common_pkg/utils"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v8"
)
// Cache redis cache
type Cache struct {
Ctx context.Context
TestDataSlice []interface{}
TestDataMap map[string]interface{}
RedisClient *redis.Client
redisServer *miniredis.Miniredis
ICache interface{}
}
// NewCache instantiated redis cache
func NewCache(testDataMap map[string]interface{}) *Cache {
var tds []interface{}
for _, data := range testDataMap {
tds = append(tds, data)
}
redisServer, err := miniredis.Run()
if err != nil {
panic(err)
}
return &Cache{
Ctx: context.Background(),
TestDataSlice: tds,
TestDataMap: testDataMap,
RedisClient: redis.NewClient(&redis.Options{Addr: redisServer.Addr()}),
redisServer: redisServer,
}
}
// Close redis server
func (c *Cache) Close() {
c.redisServer.Close()
}
// GetIDs get test data ids
func (c *Cache) GetIDs() []uint64 {
var ids []uint64
for idStr := range c.TestDataMap {
ids = append(ids, utils.StrToUint64(idStr))
}
return ids
}
// GetTestData get test data
func (c *Cache) GetTestData() map[string]interface{} {
return c.TestDataMap
}
Go
1
https://gitee.com/yzsunjianguo/common_pkg.git
git@gitee.com:yzsunjianguo/common_pkg.git
yzsunjianguo
common_pkg
common_pkg
v1.0.1

搜索帮助