Documentation ¶
Overview ¶
Package cache simplifies caching with GC
Index ¶
- type Cache
- func (cache *Cache) Add(key string, value interface{}) bool
- func (cache *Cache) Clear()
- func (cache *Cache) Delete(key string)
- func (cache *Cache) DeleteAllWithFunc(fn func(key string, value interface{}))
- func (cache *Cache) DeleteExpired()
- func (cache *Cache) DeleteExpiredWithFunc(fn func(key string, value interface{}))
- func (cache *Cache) Export(w io.Writer) error
- func (cache *Cache) Get(key string) interface{}
- func (cache *Cache) Import(r io.Reader) error
- func (cache *Cache) Set(key string, value interface{})
- func (cache *Cache) SetWithDuration(key string, value interface{}, creation time.Time, duration time.Duration)
- func (cache *Cache) Size() int
- func (cache *Cache) String() string
- func (cache *Cache) Time(key string) time.Time
- func (cache *Cache) Update(key string, value interface{}) bool
- type Item
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
Cache is a storage for all Item items
Example ¶
package main import ( "fmt" "time" "simonwaldherr.de/go/golibs/cache" ) func main() { c := cache.New(5*time.Second, 1*time.Second) c.Add("0", 23) c.Add("1", 1*time.Second) c.Add("2", "foobar") c.Add("3", false) c.Add("4", []byte("test")) fmt.Println(c.String()) }
Output: 0 23 1 1s 2 foobar 3 false 4 [116 101 115 116]
func New2 ¶
func New2(expirationTime, cleanupInterval time.Duration, fn func(key string, value interface{})) *Cache
New2 creates a new Cache with a cleaner function
func (*Cache) DeleteAllWithFunc ¶
DeleteAllWithFunc does the same like DeleteExpiredWithFunc but not just for the expired items, also the non expired
func (*Cache) DeleteExpired ¶
func (cache *Cache) DeleteExpired()
DeleteExpired checks all cache items and deletes the expired items
func (*Cache) DeleteExpiredWithFunc ¶
DeleteExpiredWithFunc does the same like DeleteExpired but also calls a function for each deleted item
func (*Cache) Set ¶
Set creates an Item in the cache, if there is already an item with that name it get overwritten
func (*Cache) SetWithDuration ¶
func (cache *Cache) SetWithDuration(key string, value interface{}, creation time.Time, duration time.Duration)
SetWithDuration does the same as Set but with an specific expiration date
func (*Cache) Size ¶
Size returns the number of cached items it does not check for expired items, so run DeleteExpired before