12345678910111213141516171819202122232425262728293031323334353637 |
- package cache
- import (
- "context"
- "github.com/patrickmn/go-cache"
- "time"
- )
- type memory struct {
- c *cache.Cache
- }
- func (c *memory) Set(ctx context.Context, key, val string, t time.Duration) (bool, error) {
- c.c.Set(key, val, t)
- return true, nil
- }
- func (c *memory) Get(ctx context.Context, key string) (string, error) {
- v, ok := c.c.Get(key)
- if !ok {
- return "", ErrorCacheNotFound
- }
- vv, ok := v.(string)
- if !ok {
- return "", ErrorCache
- }
- return vv, nil
- }
- func (c *memory) Delete(ctx context.Context, key string) (bool, error) {
- c.c.Delete(key)
- return true, nil
- }
- func NewMemory() Cache {
- return &memory{c: cache.New(5*time.Minute, 10*time.Minute)}
- }
|