README
¶
cache
memory and redis cache libraries.
Example of use
// Choose to create a memory or redis cache depending on CType
cache := cache.NewUserExampleCache(&model.CacheType{
CType: "redis",
Rdb: c.RedisClient,
})
// -----------------------------------------------------------------------------------------
type userExampleDao struct {
db *gorm.DB
cache cache.UserExampleCache
}
// NewUserExampleDao creating the dao interface
func NewUserExampleDao(db *gorm.DB, cache cache.UserExampleCache) UserExampleDao {
return &userExampleDao{db: db, cache: cache}
}
// GetByID get a record based on id
func (d *userExampleDao) GetByID(ctx context.Context, id uint64) (*model.UserExample, error) {
record, err := d.cache.Get(ctx, id)
if err == nil {
return record, nil
}
if errors.Is(err, model.ErrCacheNotFound) {
// get from mysql
table := &model.UserExample{}
err = d.db.WithContext(ctx).Where("id = ?", id).First(table).Error
if err != nil {
// if data is empty, set not found cache to prevent cache penetration(preventing Cache Penetration)
if errors.Is(err, model.ErrRecordNotFound) {
err = d.cache.SetCacheWithNotFound(ctx, id)
if err != nil {
return nil, err
}
return nil, model.ErrRecordNotFound
}
return nil, err
}
// set cache
err = d.cache.Set(ctx, id, table, 10*time.Minute)
if err != nil {
return nil, fmt.Errorf("cache.Set error: %v, id=%d", err, id)
}
return table, nil
} else if errors.Is(err, cacheBase.ErrPlaceholder) {
return nil, model.ErrRecordNotFound
}
// fail fast, if cache error return, don't request to db
return nil, err
}
Documentation
¶
Overview ¶
Package cache is memory and redis cache libraries.
Index ¶
- Variables
- func BuildCacheKey(keyPrefix string, key string) (string, error)
- func Del(ctx context.Context, keys ...string) error
- func Get(ctx context.Context, key string, val interface{}) error
- func MultiGet(ctx context.Context, keys []string, valueMap interface{}) error
- func MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error
- func Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error
- func SetCacheWithNotFound(ctx context.Context, key string) error
- type Cache
- func NewMemoryCache(keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache
- func NewRedisCache(client *redis.Client, keyPrefix string, encode encoding.Encoding, ...) Cache
- func NewRedisClusterCache(client *redis.ClusterClient, keyPrefix string, encode encoding.Encoding, ...) Cache
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // DefaultExpireTime default expiry time DefaultExpireTime = time.Hour * 24 // DefaultNotFoundExpireTime expiry time when result is empty 1 minute, // often used for cache time when data is empty (cache pass-through) DefaultNotFoundExpireTime = time.Minute * 10 // NotFoundPlaceholder placeholder NotFoundPlaceholder = "*" // DefaultClient generate a cache client, where keyPrefix is generally the business prefix DefaultClient Cache // ErrPlaceholder . ErrPlaceholder = errors.New("cache: placeholder") // ErrSetMemoryWithNotFound . ErrSetMemoryWithNotFound = errors.New("cache: set memory cache err for not found") )
View Source
var CacheNotFound = redis.Nil
CacheNotFound no hit cache
Functions ¶
func BuildCacheKey ¶
BuildCacheKey construct a cache key with a prefix
Types ¶
type Cache ¶
type Cache interface { Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error Get(ctx context.Context, key string, val interface{}) error MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error MultiGet(ctx context.Context, keys []string, valueMap interface{}) error Del(ctx context.Context, keys ...string) error SetCacheWithNotFound(ctx context.Context, key string) error }
Cache driver interface
func NewMemoryCache ¶
NewMemoryCache create a memory cache
Click to show internal directories.
Click to hide internal directories.