cache.go 784 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cache
  2. //go:generate mkdir -p mocks
  3. //go:generate rm -rf ./mocks/*_minimock.go
  4. //go:generate minimock -i ../../interfaces.Cache -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "sync"
  7. "github.com/dmitriygnatenko/internal/interfaces"
  8. )
  9. type cache struct {
  10. data map[string]interface{}
  11. sync.RWMutex
  12. }
  13. func Init() (interfaces.Cache, error) {
  14. return &cache{
  15. data: make(map[string]interface{}),
  16. }, nil
  17. }
  18. func (c *cache) Get(key string) (interface{}, bool) {
  19. c.RLock()
  20. defer c.RUnlock()
  21. res, found := c.data[key]
  22. if !found {
  23. return nil, false
  24. }
  25. return res, true
  26. }
  27. func (c *cache) Set(key string, value interface{}) {
  28. c.Lock()
  29. defer c.Unlock()
  30. c.data[key] = value
  31. }
  32. func (c *cache) FlushAll() {
  33. c.Lock()
  34. defer c.Unlock()
  35. c.data = make(map[string]interface{})
  36. }