123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package cache
- //go:generate mkdir -p mocks
- //go:generate rm -rf ./mocks/*_minimock.go
- //go:generate minimock -i ../../interfaces.Cache -o ./mocks/ -s "_minimock.go"
- import (
- "sync"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces"
- )
- type cache struct {
- data map[string]interface{}
- sync.RWMutex
- }
- func Init() (interfaces.Cache, error) {
- return &cache{
- data: make(map[string]interface{}),
- }, nil
- }
- func (c *cache) Get(key string) (interface{}, bool) {
- c.RLock()
- defer c.RUnlock()
- res, found := c.data[key]
- if !found {
- return nil, false
- }
- return res, true
- }
- func (c *cache) Set(key string, value interface{}) {
- c.Lock()
- defer c.Unlock()
- c.data[key] = value
- }
- func (c *cache) FlushAll() {
- c.Lock()
- defer c.Unlock()
- c.data = make(map[string]interface{})
- }
|