github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/cache/cache.go (about) 1 package cache 2 3 import ( 4 "github.com/angenalZZZ/gofunc/data/cache/codec" 5 "github.com/angenalZZZ/gofunc/data/cache/store" 6 "time" 7 ) 8 9 const ( 10 // DefaultType represents the cache type as a string value 11 DefaultType = "cache" 12 ) 13 14 // Cache represents the configuration needed by a cache 15 type Cache struct { 16 codec codec.CodecInterface 17 } 18 19 // New create a new cache entry 20 func New(store store.Interface) *Cache { 21 return &Cache{ 22 codec: codec.New(store), 23 } 24 } 25 26 // Get returns the object stored in cache if it exists 27 func (c *Cache) Get(key string) (interface{}, error) { 28 return c.codec.Get(key) 29 } 30 31 // Set populates the cache item using the given key 32 func (c *Cache) Set(key string, object interface{}, options *store.Options) error { 33 return c.codec.Set(key, object, options) 34 } 35 36 // TTL returns an expiration time 37 func (c *Cache) TTL(key string) (time.Duration, error) { 38 return c.codec.TTL(key) 39 } 40 41 // Delete removes the cache item using the given key 42 func (c *Cache) Delete(key string) error { 43 return c.codec.Delete(key) 44 } 45 46 // Invalidate invalidates cache item from given options 47 func (c *Cache) Invalidate(options store.InvalidateOptions) error { 48 return c.codec.Invalidate(options) 49 } 50 51 // Clear resets all cache data 52 func (c *Cache) Clear() error { 53 return c.codec.Clear() 54 } 55 56 // GetCodec returns the current codec 57 func (c *Cache) GetCodec() codec.CodecInterface { 58 return c.codec 59 } 60 61 // GetType returns the cache type 62 func (c *Cache) GetType() string { 63 return DefaultType 64 }