mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-29 11:42:03 +08:00
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
package cache
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"github.com/zhufuyi/sponge/internal/model"
|
||
"github.com/zhufuyi/sponge/pkg/cache"
|
||
"github.com/zhufuyi/sponge/pkg/encoding"
|
||
|
||
"github.com/go-redis/redis/v8"
|
||
"github.com/spf13/cast"
|
||
)
|
||
|
||
const (
|
||
// PrefixUserExampleCacheKey cache prefix
|
||
PrefixUserExampleCacheKey = "userExample:%d"
|
||
)
|
||
|
||
var _ UserExampleCache = (*userExampleCache)(nil)
|
||
|
||
// UserExampleCache cache interface
|
||
type UserExampleCache interface {
|
||
Set(ctx context.Context, id uint64, data *model.UserExample, duration time.Duration) error
|
||
Get(ctx context.Context, id uint64) (ret *model.UserExample, err error)
|
||
MultiGet(ctx context.Context, ids []uint64) (map[string]*model.UserExample, error)
|
||
MultiSet(ctx context.Context, data []*model.UserExample, duration time.Duration) error
|
||
Del(ctx context.Context, id uint64) error
|
||
SetCacheWithNotFound(ctx context.Context, id uint64) error
|
||
}
|
||
|
||
// userExampleCache define a cache struct
|
||
type userExampleCache struct {
|
||
cache cache.Cache
|
||
}
|
||
|
||
// NewUserExampleCache new a cache
|
||
func NewUserExampleCache(rdb *redis.Client) UserExampleCache {
|
||
jsonEncoding := encoding.JSONEncoding{}
|
||
cachePrefix := ""
|
||
return &userExampleCache{
|
||
cache: cache.NewRedisCache(rdb, cachePrefix, jsonEncoding, func() interface{} {
|
||
return &model.UserExample{}
|
||
}),
|
||
}
|
||
}
|
||
|
||
// GetUserExampleCacheKey 设置缓存
|
||
func (c *userExampleCache) GetUserExampleCacheKey(id uint64) string {
|
||
return fmt.Sprintf(PrefixUserExampleCacheKey, id)
|
||
}
|
||
|
||
// Set write to cache
|
||
func (c *userExampleCache) Set(ctx context.Context, id uint64, data *model.UserExample, duration time.Duration) error {
|
||
if data == nil || id == 0 {
|
||
return nil
|
||
}
|
||
cacheKey := c.GetUserExampleCacheKey(id)
|
||
err := c.cache.Set(ctx, cacheKey, data, duration)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Get 获取cache
|
||
func (c *userExampleCache) Get(ctx context.Context, id uint64) (*model.UserExample, error) {
|
||
var data *model.UserExample
|
||
cacheKey := c.GetUserExampleCacheKey(id)
|
||
err := c.cache.Get(ctx, cacheKey, &data)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return data, nil
|
||
}
|
||
|
||
// MultiSet 批量设置cache
|
||
func (c *userExampleCache) MultiSet(ctx context.Context, data []*model.UserExample, duration time.Duration) error {
|
||
valMap := make(map[string]interface{})
|
||
for _, v := range data {
|
||
cacheKey := c.GetUserExampleCacheKey(v.ID)
|
||
valMap[cacheKey] = v
|
||
}
|
||
|
||
err := c.cache.MultiSet(ctx, valMap, duration)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// MultiGet 批量获取cache,返回map中的key是id值
|
||
func (c *userExampleCache) MultiGet(ctx context.Context, ids []uint64) (map[string]*model.UserExample, error) {
|
||
var keys []string
|
||
for _, v := range ids {
|
||
cacheKey := c.GetUserExampleCacheKey(v)
|
||
keys = append(keys, cacheKey)
|
||
}
|
||
|
||
// NOTE: 需要在这里make实例化,如果在返回参数里直接定义会报 nil map
|
||
itemMap := make(map[string]*model.UserExample)
|
||
err := c.cache.MultiGet(ctx, keys, itemMap)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
retMap := make(map[string]*model.UserExample)
|
||
for _, v := range ids {
|
||
val, ok := itemMap[c.GetUserExampleCacheKey(v)]
|
||
if ok {
|
||
retMap[cast.ToString(v)] = val
|
||
}
|
||
}
|
||
|
||
return retMap, nil
|
||
}
|
||
|
||
// Del 删除cache
|
||
func (c *userExampleCache) Del(ctx context.Context, id uint64) error {
|
||
cacheKey := c.GetUserExampleCacheKey(id)
|
||
err := c.cache.Del(ctx, cacheKey)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// SetCacheWithNotFound 设置空缓存
|
||
func (c *userExampleCache) SetCacheWithNotFound(ctx context.Context, id uint64) error {
|
||
cacheKey := c.GetUserExampleCacheKey(id)
|
||
err := c.cache.SetCacheWithNotFound(ctx, cacheKey)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|