|
| 1 | +package memory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + gocache "github.com/patrickmn/go-cache" |
| 8 | + "github.com/pkg/errors" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + // ErrKeyNotFound indicates the requested key does not exist in the cache |
| 13 | + ErrKeyNotFound = errors.New("key not found") |
| 14 | + |
| 15 | + // ErrUnexpectedType occurs when attempting to retrieve data with unexpected type |
| 16 | + ErrUnexpectedType = errors.New("unknown data type") |
| 17 | +) |
| 18 | + |
| 19 | +type cache struct { |
| 20 | + db *gocache.Cache |
| 21 | +} |
| 22 | + |
| 23 | +// NewCache creates an in-memory cache instance with configurable expiration. |
| 24 | +// defaultExpiration: default TTL for cache entries (use time.Duration(0) for no expiration) |
| 25 | +// cleanupInterval: interval for automatic removal of expired entries (use time.Duration(0) to disable) |
| 26 | +func NewCache(defaultExpiration, cleanupInterval time.Duration) *cache { |
| 27 | + return &cache{ |
| 28 | + db: gocache.New(defaultExpiration, cleanupInterval), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Del removes multiple entries from the cache. |
| 33 | +// Returns number of deleted items and any potential error (currently always nil) |
| 34 | +func (c *cache) Del(ctx context.Context, keys ...string) (affected int64, err error) { |
| 35 | + for _, key := range keys { |
| 36 | + c.db.Delete(key) |
| 37 | + } |
| 38 | + return int64(len(keys)), nil |
| 39 | +} |
| 40 | + |
| 41 | +// Get retrieves a value as byte slice from the cache. |
| 42 | +// Returns ErrKeyNotFound if the key doesn't exist |
| 43 | +// Returns ErrUnexpectedType if value cannot be cast to []byte |
| 44 | +func (c *cache) Get(ctx context.Context, key string) (val []byte, err error) { |
| 45 | + v, ok := c.db.Get(key) |
| 46 | + if !ok { |
| 47 | + return nil, errors.WithStack(ErrKeyNotFound) |
| 48 | + } |
| 49 | + item := v.(gocache.Item) |
| 50 | + if val, ok = item.Object.([]byte); !ok { |
| 51 | + return nil, errors.WithStack(ErrUnexpectedType) |
| 52 | + } |
| 53 | + return val, nil |
| 54 | +} |
| 55 | + |
| 56 | +// GetAsUint64 retrieves a numeric value from the cache as uint64. |
| 57 | +// Returns ErrKeyNotFound if the key doesn't exist |
| 58 | +// Returns ErrUnexpectedType if value cannot be cast to uint64 |
| 59 | +func (c *cache) GetAsUint64(ctx context.Context, key string) (val uint64, err error) { |
| 60 | + v, ok := c.db.Get(key) |
| 61 | + if !ok { |
| 62 | + return 0, errors.WithStack(ErrKeyNotFound) |
| 63 | + } |
| 64 | + item := v.(gocache.Item) |
| 65 | + if val, ok = item.Object.(uint64); !ok { |
| 66 | + return 0, errors.WithStack(ErrUnexpectedType) |
| 67 | + } |
| 68 | + return val, nil |
| 69 | +} |
| 70 | + |
| 71 | +// Set stores a byte slice in the cache with expiration. |
| 72 | +// expire: 0 uses default expiration, <0 means no expiration |
| 73 | +func (c *cache) Set(ctx context.Context, key string, val []byte, expire time.Duration) error { |
| 74 | + c.db.Set(key, val, expire) |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// SetUint64 stores a numeric value in the cache with expiration. |
| 79 | +// expire: 0 uses default expiration, <0 means no expiration |
| 80 | +func (c *cache) SetUint64(ctx context.Context, key string, val uint64, expire time.Duration) error { |
| 81 | + c.db.Set(key, val, expire) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// IsKeyNotFound checks if an error indicates missing key |
| 86 | +// Helps determine error type without direct dependency on package errors |
| 87 | +func (c *cache) IsKeyNotFound(err error) bool { |
| 88 | + return errors.Is(err, ErrKeyNotFound) |
| 89 | +} |
0 commit comments