memory.go 671 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package cache
  2. import (
  3. "context"
  4. "github.com/patrickmn/go-cache"
  5. "time"
  6. )
  7. type memory struct {
  8. c *cache.Cache
  9. }
  10. func (c *memory) Set(ctx context.Context, key, val string, t time.Duration) (bool, error) {
  11. c.c.Set(key, val, t)
  12. return true, nil
  13. }
  14. func (c *memory) Get(ctx context.Context, key string) (string, error) {
  15. v, ok := c.c.Get(key)
  16. if !ok {
  17. return "", ErrorCacheNotFound
  18. }
  19. vv, ok := v.(string)
  20. if !ok {
  21. return "", ErrorCache
  22. }
  23. return vv, nil
  24. }
  25. func (c *memory) Delete(ctx context.Context, key string) (bool, error) {
  26. c.c.Delete(key)
  27. return true, nil
  28. }
  29. func NewMemory() Cache {
  30. return &memory{c: cache.New(5*time.Minute, 10*time.Minute)}
  31. }