1 Star 0 Fork 447

qqq/极客时间-Go实战训练营

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
types.go 2.02 KB
Copy Edit Raw Blame History
Ming Deng authored 2022-11-06 15:19 +08:00 . 11.5
package cache
import (
"context"
"errors"
"io"
"sync"
"time"
)
// Cache 屏蔽不同的缓存中间件的差异
type Cache interface {
Get(ctx context.Context, key string) (any, error)
Set(ctx context.Context, key string, val any,
expiration time.Duration) error
Delete(ctx context.Context, key string) error
LoadAndDelete(ctx context.Context, key string) (any, error)
// 作业在这里
// OnEvicted(ctx context.Context) <- chan KV
}
// type KV struct {
// Key string
// Val any
// }
type CacheV4 interface {
Get(ctx context.Context, key string) (any, error)
Set(ctx context.Context, key string, val any,
expiration time.Duration) error
Delete(ctx context.Context, key string) error
LoadAndDelete(ctx context.Context, key string) (any, error)
io.Closer
}
type ClosedCache struct {
Cache
mutex sync.RWMutex
closed bool
}
func (c *ClosedCache) Get(ctx context.Context, key string) (any, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.closed {
return nil, errors.New("closed 了缓存")
}
return c.Cache.Get(ctx, key)
}
func (c *ClosedCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.closed {
return errors.New("closed 了缓存")
}
return c.Cache.Set(ctx, key, val, expiration)
}
func (c *ClosedCache) Delete(ctx context.Context, key string) error {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.closed {
return errors.New("closed 了缓存")
}
return c.Cache.Delete(ctx, key)
}
func (c *ClosedCache) LoadAndDelete(ctx context.Context, key string) (any, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.closed {
return nil, errors.New("closed 了缓存")
}
return c.Cache.LoadAndDelete(ctx, key)
}
func (c *ClosedCache) Close() error {
c.mutex.Lock()
defer c.mutex.Unlock()
c.closed = true
return nil
}
// func NewCache() {
// f := io.ReadAll()
// var c Cache
// if xxx {
// c = NewBuildinMapCache()
// } else {
// c = NewRedisCache()
// }
// 继续解析配置
// 用装饰器来装饰 c
// }
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/q_101082/geektime-go.git
git@gitee.com:q_101082/geektime-go.git
q_101082
geektime-go
极客时间-Go实战训练营
master

Search