123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package cache
- import (
- "sync"
- )
- type Service struct {
- data map[string]interface{}
- sync.RWMutex
- }
- func Init() (*Service, error) {
- return &Service{
- data: make(map[string]interface{}),
- }, nil
- }
- func (c *Service) 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 *Service) Set(key string, value interface{}) {
- c.Lock()
- defer c.Unlock()
- c.data[key] = value
- }
- func (c *Service) FlushAll() {
- c.Lock()
- defer c.Unlock()
- c.data = make(map[string]interface{})
- }
|