memory_storage.go 622 B

1234567891011121314151617181920212223242526272829303132333435
  1. package distcache
  2. import (
  3. "time"
  4. gocache "github.com/patrickmn/go-cache"
  5. )
  6. type memoryStorage struct {
  7. c *gocache.Cache
  8. }
  9. func newMemoryStorage() *memoryStorage {
  10. return &memoryStorage{
  11. c: gocache.New(time.Minute*30, time.Minute*30),
  12. }
  13. }
  14. func (s *memoryStorage) Put(key string, val interface{}, expires time.Duration) error {
  15. return s.c.Add(key, val, expires)
  16. }
  17. func (s *memoryStorage) Get(key string) (interface{}, error) {
  18. val, exist := s.c.Get(key)
  19. if !exist {
  20. return nil, ErrCacheItemNotFound
  21. }
  22. return val, nil
  23. }
  24. func (s *memoryStorage) Delete(key string) error {
  25. s.c.Delete(key)
  26. return nil
  27. }