github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/pkg/storage/storage_cache.go (about)

     1  package storage
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  var (
     8  	once  sync.Once
     9  	cache *Cache
    10  )
    11  
    12  // Cache defines a simple memory cache for key-value
    13  type Cache struct {
    14  	cacheStorage map[string]interface{}
    15  }
    16  
    17  // InitCache to init a cache
    18  func InitCache() {
    19  	once.Do(func() {
    20  		cache = &Cache{
    21  			make(map[string]interface{}),
    22  		}
    23  	})
    24  }
    25  
    26  // Get returns value with a key
    27  func (c *Cache) Get(key string) interface{} {
    28  	return c.cacheStorage[key]
    29  }
    30  
    31  // Set to store a key-val to cache
    32  func (c *Cache) Set(key string, val interface{}) {
    33  	c.cacheStorage[key] = val
    34  }
    35  
    36  // GetCache return data directory
    37  func GetCache() *Cache {
    38  	return cache
    39  }