go get -u github.com/negasus/cache
c := cache.New(ctx, cache.WithSizeLimit(1024*1024*5)
c.Put("foo", []byte("bar"))
value, err := c.Get("foo")
You should use a New
function for create the Cache instance.
You can use WithCancel
context for stop goroutines, which the Cache starts after creating.
You can use options for change some default values
default:
time.Hour
The time interval for clear expired values
default:
time.Hour
The time interval for full scan cache storage for expired values and mark it for delete
default:
1048576
(1 Mb)
Max cache size. If reached, compacting
will be running
Scan the cache storage, sort items by last used
field and delete items while storage size greater than cache.sizeLimit
Get a data from the cache.
If a data not found or expired, ErrNotFound
will be returned
Get a data from the cache or create new cache item with callback function (and return a result)
Get a data from the cache or create new cache item with callback function (and return a result)
Check key exists
Delete an item from the cache
Put the data to the cache
If data length greater or equal cache.sizeLimit
, data will not be stored.
If the cache storage reached the cache.sizeLimit
, compacting
will be running.
Put the data to the cache
If data length greater or equal cache.sizeLimit
, data will not be stored.
If the cache storage reached the cache.sizeLimit
, compacting
will be running.