cache.go 583 B

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