cache.go 619 B

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