Documentation
¶
Index ¶
- type Cache
- func (c *Cache) Clear()
- func (c *Cache) Delete(key string)
- func (c *Cache) Exists(key string) bool
- func (c *Cache) Get(key string) interface{}
- func (c *Cache) GetOrSet(key string, fn func() (interface{}, time.Duration)) interface{}
- func (c *Cache) Set(key string, data interface{}, ttl ...time.Duration)
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 a simple in memory cache that stores data in a map in memory. The cache is not persistent, so it will be lost when the application is restarted.
For the sake of speed and simplicity, try to store only necessary data in the cache to reduce the memory footprint and improve performance.
func New ¶
Use this function to create a new cache
You can opt out of specifying the reset time and by default it will be set to 1 second Reset time is the time between each check for expired data
func (*Cache) Exists ¶
Exists reports whether a non-expired entry for key is present in the cache. It performs the same expiry check as Get, so the two are always consistent.
func (*Cache) Get ¶
Gets the data from the cache using the key. If the data is not found, it returns nil
func (*Cache) GetOrSet ¶ added in v1.1.4
GetOrSet atomically gets an existing unexpired entry or creates a new one. fn is called only when the key is absent or expired; its return value and TTL are stored under a single write lock — no TOCTOU window. If fn returns a zero TTL the cache's default reset interval is used.
func (*Cache) Set ¶
Sets a new item to the cache specifying the key and data to store
You can opt out of specifying the time to live (ttl) and by default the cache will use the value specified when creating the cache using the New function
This will also start the cache if there was no items in the cache before.