Documentation ¶
Overview ¶
Package cache provides a simple in-memory key:value cache
Index ¶
- type Cache
- func (s *Cache) Delete(key string)
- func (s *Cache) Expired() int
- func (s *Cache) Flush()
- func (s *Cache) Get(key string) interface{}
- func (s *Cache) GetWithExpiration(key string) (interface{}, time.Time)
- func (s *Cache) Has(key string) bool
- func (s *Cache) Set(key string, data interface{})
- func (s *Cache) Size() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is cache instance
func New ¶
New creates new cache instance
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") fmt.Println(cache.Get("test"))
Output: ABCD
func (*Cache) Delete ¶
Delete removes item from cache
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") cache.Delete("test") fmt.Println(cache.Get("test"))
Output: <nil>
func (*Cache) Expired ¶
Expired returns number of exipred items in cache
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") fmt.Println(cache.Expired())
Output: 0
func (*Cache) Flush ¶
func (s *Cache) Flush()
Flush removes all data from cache
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") cache.Flush() fmt.Println(cache.Get("test"))
Output: <nil>
func (*Cache) Get ¶
Get returns item from cache or nil
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") fmt.Println(cache.Get("test"))
Output: ABCD
func (*Cache) GetWithExpiration ¶
GetWithExpiration returns item from cache and expiration date or nil
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") item, exp := cache.GetWithExpiration("test") fmt.Println(item, exp.String())
Output:
func (*Cache) Has ¶
Has returns true if cache contains data for given key
Example ¶
cache := New(time.Second, time.Minute) cache.Set("test", "ABCD") fmt.Println(cache.Has("test"))
Output: true
Click to show internal directories.
Click to hide internal directories.