package memory import ( "context" "sync" "time" "git.nobla.cn/golang/aeus/pkg/errors" ) type memCache struct { opts Options items map[string]Item sync.RWMutex } func (c *memCache) Get(ctx context.Context, key string) (any, error) { c.RWMutex.RLock() defer c.RWMutex.RUnlock() item, found := c.items[key] if !found { return nil, errors.ErrNotFound } if item.Expired() { return nil, errors.ErrExpired } return item.Value, nil } func (c *memCache) Put(ctx context.Context, key string, val any, d time.Duration) error { var e int64 if d == DefaultExpiration { d = c.opts.Expiration } if d > 0 { e = time.Now().Add(d).UnixNano() } c.RWMutex.Lock() defer c.RWMutex.Unlock() c.items[key] = Item{ Value: val, Expiration: e, } return nil } func (c *memCache) Delete(ctx context.Context, key string) error { c.RWMutex.Lock() defer c.RWMutex.Unlock() _, found := c.items[key] if !found { return errors.ErrNotFound } delete(c.items, key) return nil } func (m *memCache) String() string { return "memory" }