add memcached
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent ca37588e11
commit e4a963e0c0

@ -9,21 +9,33 @@ import (
// Big https://github.com/allegro/bigcache
type Big struct {
bigCache *bigcache.BigCache // 驱动
db *bigcache.BigCache // 驱动
expiration time.Duration // 默认过期时间
}
// NewBig 实例化
func NewBig(expiration time.Duration) *Big {
c, _ := bigcache.NewBigCache(bigcache.DefaultConfig(expiration))
return &Big{bigCache: c, expiration: expiration}
return &Big{db: c, expiration: expiration}
}
// Set 插入数据 将只显示给定结构的导出字段 序列化并存储
func (b *Big) Set(key string, value interface{}) error {
// 将 value 序列化为 bytes
valueBytes, err := serialize(value)
if err != nil {
return err
}
return b.db.Set(key, valueBytes)
}
// Get 获取单个数据
func (b *Big) Get(key string) (interface{}, error) {
// 获取以 bytes 格式存储的 value
valueBytes, err := b.bigCache.Get(key)
valueBytes, err := b.db.Get(key)
if err != nil {
return nil, err
}
@ -37,18 +49,6 @@ func (b *Big) Get(key string) (interface{}, error) {
return value, nil
}
// Set 插入数据 将只显示给定结构的导出字段 序列化并存储
func (b *Big) Set(key string, value interface{}) error {
// 将 value 序列化为 bytes
valueBytes, err := serialize(value)
if err != nil {
return err
}
return b.bigCache.Set(key, valueBytes)
}
// 序列化
func serialize(value interface{}) ([]byte, error) {
buf := bytes.Buffer{}

18
go.go

@ -7,7 +7,7 @@ import (
// Go https://github.com/patrickmn/go-cache
type Go struct {
goCache *cache.Cache // 驱动
db *cache.Cache // 驱动
expiration time.Duration // 默认过期时间
clear time.Duration // 清理过期数据
}
@ -15,20 +15,20 @@ type Go struct {
// NewGo 实例化
func NewGo(expiration, clear time.Duration) *Go {
c := cache.New(expiration, clear)
return &Go{goCache: c, expiration: expiration, clear: clear}
}
// Get 获取单个数据
func (g *Go) Get(key string) (interface{}, bool) {
return g.goCache.Get(key)
return &Go{db: c, expiration: expiration, clear: clear}
}
// Set 插入数据 并设置过期时间
func (g *Go) Set(key string, value interface{}, expirationTime time.Duration) {
g.goCache.Set(key, value, expirationTime)
g.db.Set(key, value, expirationTime)
}
// Get 获取单个数据
func (g *Go) Get(key string) (interface{}, bool) {
return g.db.Get(key)
}
// SetDefault 插入数据 并设置为默认过期时间
func (g *Go) SetDefault(key string, value interface{}) {
g.goCache.Set(key, value, g.expiration)
g.db.Set(key, value, g.expiration)
}

@ -10,6 +10,7 @@ require (
)
require (
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.0 // indirect

@ -1,5 +1,7 @@
github.com/allegro/bigcache/v3 v3.0.2 h1:AKZCw+5eAaVyNTBmI2fgyPVJhHkdWder3O9IrprcQfI=
github.com/allegro/bigcache/v3 v3.0.2/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw=
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=

@ -0,0 +1,46 @@
package gocache
import (
"github.com/bradfitz/gomemcache/memcache"
)
// Memcached https://github.com/bradfitz/gomemcache
type Memcached struct {
db *memcache.Client // 驱动
}
// NewMemcached 实例化
func NewMemcached(dns string) *Memcached {
mc := memcache.New(dns)
if mc == nil {
panic("连接失败")
}
return &Memcached{db: mc}
}
// NewMemcachedDb 实例化
func NewMemcachedDb(memcached *memcache.Client) *Memcached {
return &Memcached{db: memcached}
}
// Set 插入数据
func (m *Memcached) Set(key string, value []byte) error {
return m.db.Set(&memcache.Item{Key: key, Value: value})
}
// Get 获取单个数据
func (m *Memcached) Get(key string) (string, error) {
it, err := m.db.Get(key)
if err == memcache.ErrCacheMiss {
return "", memcache.ErrCacheMiss
}
if it.Key == key {
return string(it.Value), nil
}
return "", memcache.ErrCacheMiss
}
// Del 删除单个数据
func (m *Memcached) Del(key string) error {
return m.db.Delete(key)
}

@ -6,7 +6,7 @@ import (
// Ristretto https://github.com/dgraph-io/ristretto
type Ristretto struct {
ristretto *ristretto.Cache // 驱动
db *ristretto.Cache // 驱动
numCounters int64 // 跟踪频率的键数 (10M)
maxCost int64 // 缓存的最大成本1GB
bufferItems int64 // 每个Get缓冲区的密钥数
@ -19,21 +19,21 @@ func NewRistretto() *Ristretto {
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
return &Ristretto{ristretto: cache}
}
// Get 获取单个数据
func (r *Ristretto) Get(key string) (interface{}, bool) {
return r.ristretto.Get(key)
return &Ristretto{db: cache}
}
// Set 插入数据
func (r *Ristretto) Set(key string, value interface{}, cost int64) {
r.ristretto.Set(key, value, cost)
r.ristretto.Wait()
r.db.Set(key, value, cost)
r.db.Wait()
}
// Get 获取单个数据
func (r *Ristretto) Get(key string) (interface{}, bool) {
return r.db.Get(key)
}
// Del 删除数据
func (r *Ristretto) Del(key string) {
r.ristretto.Del(key)
r.db.Del(key)
}

Loading…
Cancel
Save