基于gocache的内存缓存实现
This commit is contained in:
parent
4b0b92d4a2
commit
a157542a88
1
go.mod
1
go.mod
@ -18,6 +18,7 @@ require (
|
|||||||
github.com/go-ini/ini v1.67.0 // indirect
|
github.com/go-ini/ini v1.67.0 // indirect
|
||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/redis/go-redis/v9 v9.5.3 // indirect
|
github.com/redis/go-redis/v9 v9.5.3 // indirect
|
||||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -34,6 +34,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
|
|||||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||||
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
91
gocache.go
Normal file
91
gocache.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
// Package cache ...
|
||||||
|
//
|
||||||
|
// Description : cache ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2024-06-14 16:50
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract"
|
||||||
|
gocache "github.com/patrickmn/go-cache"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Gocache 基于gocache的内存缓存
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 16:51 2024/6/14
|
||||||
|
type Gocache struct{}
|
||||||
|
|
||||||
|
func (g *Gocache) Exist(ctx context.Context, cacheInstanceFlag string, key string) (bool, error) {
|
||||||
|
_, exist := GocacheStorageClient.Get(cacheInstanceFlag).Get(key)
|
||||||
|
return exist, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error {
|
||||||
|
now := time.Now()
|
||||||
|
if now.Unix() < ttl {
|
||||||
|
ttl = ttl - now.Unix()
|
||||||
|
}
|
||||||
|
GocacheStorageClient.Get(cacheInstanceFlag).Set(key, value, time.Duration(ttl)*time.Second)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gocache) Get(ctx context.Context, cacheInstanceFlag string, key string) (string, error) {
|
||||||
|
val, exist := GocacheStorageClient.Get(cacheInstanceFlag).Get(key)
|
||||||
|
if !exist {
|
||||||
|
return "", errors.New(key + " : not found")
|
||||||
|
}
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gocache) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (string, error) {
|
||||||
|
val, err := g.Get(ctx, cacheInstanceFlag, key)
|
||||||
|
if nil != err {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if nil == parseFunc {
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
return val, parseFunc(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gocache) Delete(ctx context.Context, cacheInstanceFlag string, key string) error {
|
||||||
|
GocacheStorageClient.Get(cacheInstanceFlag).Delete(key)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var GocacheStorageClient = &GocacheStorage{
|
||||||
|
lock: &sync.RWMutex{},
|
||||||
|
clientTable: make(map[string]*gocache.Cache),
|
||||||
|
}
|
||||||
|
|
||||||
|
type GocacheStorage struct {
|
||||||
|
lock *sync.RWMutex
|
||||||
|
clientTable map[string]*gocache.Cache
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GocacheStorage) Add(cacheInstanceFlag string) {
|
||||||
|
_ = g.Get(cacheInstanceFlag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GocacheStorage) Del(cacheInstanceFlag string) {
|
||||||
|
g.lock.Lock()
|
||||||
|
defer g.lock.Unlock()
|
||||||
|
delete(g.clientTable, cacheInstanceFlag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GocacheStorage) Get(cacheInstanceFlag string) *gocache.Cache {
|
||||||
|
g.lock.Lock()
|
||||||
|
defer g.lock.Unlock()
|
||||||
|
if _, exist := g.clientTable[cacheInstanceFlag]; !exist {
|
||||||
|
g.clientTable[cacheInstanceFlag] = gocache.New(0, 0)
|
||||||
|
}
|
||||||
|
return g.clientTable[cacheInstanceFlag]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user