cache was created to learn about cache eviction, heavily extracted from gocache and adding LFU policy for more eviction, if you want to use it please test it carefully, cache is easy-to-use, high-performance, lightweight and thread-safe (goroutine-safe) in-memory key-value cache with support for LFU, LRU and FIFO eviction policies as well as expiration, bulk operations and even retrieval of keys by pattern.
cache supports the following cache eviction policies:
- First in first out (FIFO)
- Least recently used (LRU)
- Least frequent used (LFU)
It also supports cache entry TTL, which is both active and passive. Active expiration means that if you attempt to retrieve a cache key that has already expired, it will delete it on the spot and the behavior will be as if the cache key didn't exist. As for passive expiration, there's a background task that will take care of deleting expired keys.
It also includes what you'd expect from a cache, like GET/SET, bulk operations and get by pattern.
go get -u github.com/arham09/cache
c := cache.NewCache(WithMaxSize(1000), WithEvictionPolicy(cache.LeastRecentlyUsed))
If you're planning on using expiration (SetWithTTL
or Expire
) and you want expired entries to be automatically deleted
in the background, make sure to start the janitor when you instantiate the cache:
cache.StartJanitor()
Function | Description |
---|---|
WithMaxSize | Sets the max size of the cache. cache.NoMaxSize means there is no limit. If not set, the default max size is cache.DefaultMaxSize . |
WithMaxMemoryUsage | Sets the max memory usage of the cache. cache.NoMaxMemoryUsage means there is no limit. The default behavior is to not evict based on memory usage. |
WithEvictionPolicy | Sets the eviction algorithm to be used when the cache reaches the max size. If not set, the default eviction policy is cache.FirstInFirstOut (FIFO). |
WithForceNilInterfaceOnNilPointer | Configures whether values with a nil pointer passed to write functions should be forcefully set to nil. Defaults to true. |
StartJanitor | Starts the janitor, which is in charge of deleting expired cache entries in the background. |
StopJanitor | Stops the janitor. |
Set | Same as SetWithTTL , but with no expiration (cache.NoExpiration ) |
SetAll | Same as Set , but in bulk |
SetWithTTL | Creates or updates a cache entry with the given key, value and expiration time. If the max size after the aforementioned operation is above the configured max size, the tail will be evicted. Depending on the eviction policy, the tail is defined as the oldest |
Get | Gets a cache entry by its key. |
GetByKeys | Gets a map of entries by their keys. The resulting map will contain all keys, even if some of the keys in the slice passed as parameter were not present in the cache. |
GetAll | Gets all cache entries. |
GetKeysByPattern | Retrieves a slice of keys that matches a given pattern. |
Delete | Removes a key from the cache. |
DeleteAll | Removes multiple keys from the cache. |
DeleteKeysByPattern | Removes all keys that that matches a given pattern. |
Count | Gets the size of the cache. This includes cache keys which may have already expired, but have not been removed yet. |
Clear | Wipes the cache. |
TTL | Gets the time until a cache key expires. |
Expire | Sets the expiration time of an existing cache key. |
cache.Set("key", "value")
cache.Set("key", 1)
cache.Set("key", struct{ Text string }{Test: "value"})
cache.SetWithTTL("key", []byte("value"), 24*time.Hour)
value, exists := cache.Get("key")
You can also get multiple entries by using cache.GetByKeys([]string{"key1", "key2"})
cache.Delete("key")
You can also delete multiple entries by using cache.DeleteAll([]string{"key1", "key2"})
package main
import (
"fmt"
"time"
"github.com/arham09/cache"
)
func main() {
c := cache.NewCache(cache.WithEvictionPolicy(cache.LeastRecentlyUsed), cache.WithMaxSize(10000))
cache.StartJanitor() // Passively manages expired entries
defer cache.StopJanitor()
c.Set("key", "value")
c.SetWithTTL("key-with-ttl", "value", 60*time.Minute)
c.SetAll(map[string]interface{}{"k1": "v1", "k2": "v2", "k3": "v3"})
fmt.Println("[Count] Cache size:", cache.Count())
value, exists := c.Get("key")
fmt.Printf("[Get] key=key; value=%s; exists=%v\n", value, exists)
for key, value := range c.GetByKeys([]string{"k1", "k2", "k3"}) {
fmt.Printf("[GetByKeys] key=%s; value=%s\n", key, value)
}
for _, key := range c.GetKeysByPattern("key*", 0) {
fmt.Printf("[GetKeysByPattern] pattern=key*; key=%s\n", key)
}
c.Expire("key", time.Hour)
time.Sleep(500*time.Millisecond)
timeUntilExpiration, _ := c.TTL("key")
fmt.Println("[TTL] Number of minutes before 'key' expires:", int(timeUntilExpiration.Seconds()))
c.Delete("key")
c.DeleteAll([]string{"k1", "k2", "k3"})
c.Clear()
fmt.Println("[Count] Cache size after clearing the cache:", c.Count())
}
Output
[Count] Cache size: 5
[Get] key=key; value=value; exists=true
[GetByKeys] key=k1; value=v1
[GetByKeys] key=k2; value=v2
[GetByKeys] key=k3; value=v3
[GetKeysByPattern] pattern=key*; key=key-with-ttl
[GetKeysByPattern] pattern=key*; key=key
[TTL] Number of minutes before 'key' expires: 3599
[Count] Cache size after clearing the cache: 0
Eviction by MaxSize is the default behavior, and is also the most efficient.
The code below will create a cache that has a maximum size of 1000:
c := cache.NewCache(cache.WithMaxSize(1000))
This means that whenever an operation causes the total size of the cache to go above 1000, the tail will be evicted.
Eviction by MaxMemoryUsage is disabled by default, and is in alpha.
The code below will create a cache that has a maximum memory usage of 50MB:
c := cache.NewCache(cache.WithMaxSize(0), cache.WithMaxMemoryUsage(50*cache.Megabyte))
This means that whenever an operation causes the total memory usage of the cache to go above 50MB, one or more tails will be evicted.
Unlike evictions caused by reaching the MaxSize, evictions triggered by MaxMemoryUsage may lead to multiple entries being evicted in a row. The reason for this is that if, for instance, you had 100 entries of 0.1MB each and you suddenly added a single entry of 10MB, 100 entries would need to be evicted to make enough space for that new big entry.
It's very important to keep in mind that eviction by MaxMemoryUsage is approximate.
The only memory taken into consideration is the size of the cache, not the size of the entire application. If you pass along 100MB worth of data in a matter of seconds, even though the cache's memory usage will remain under 50MB (or whatever you configure the MaxMemoryUsage to), the memory footprint generated by that 100MB will still exist until the next GC cycle.
As previously mentioned, this is a work in progress, and here's a list of the things you should keep in mind:
- The memory usage of structs are a gross estimation and may not reflect the actual memory usage.
- Native types (string, int, bool, []byte, etc.) are the most accurate for calculating the memory usage.
- Adding an entry bigger than the configured MaxMemoryUsage will work, but it will evict all other entries.
There are two ways that the deletion of expired keys can take place:
- Active
- Passive
Active deletion of expired keys happens when an attempt is made to access the value of a cache entry that expired.
Get
, GetByKeys
and GetAll
are the only functions that can trigger active deletion of expired keys.
Passive deletion of expired keys runs in the background and is managed by the janitor. If you do not start the janitor, there will be no passive deletion of expired keys.
- Set: Both map and cache have the same performance.
- Get: Map is faster than cache.
This is because cache keeps track of the head and the tail for eviction and expiration/TTL.
Ultimately, the difference is negligible.
We could add a way to disable eviction or disable expiration altogether just to match the map's performance, but if you're looking into using a library like cache, odds are, you want more than just a map.
key | value |
---|---|
goos | windows |
goarch | amd64 |
cpu | i7-9700K |
mem | 32G DDR4 |