MCPcopy Index your code
hub / github.com/TwiN/gocache

github.com/TwiN/gocache @v2.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.4.0 ↗ · + Follow
116 symbols 725 edges 11 files 42 documented · 36% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

gocache

test Go Report Card codecov Go version Go Reference Follow TwiN

gocache is an easy-to-use, high-performance, lightweight and thread-safe (goroutine-safe) in-memory key-value cache with support for LRU and FIFO eviction policies as well as expiration, bulk operations and even retrieval of keys by pattern.

Table of Contents

Features

gocache supports the following cache eviction policies: - First in first out (FIFO) - Least recently used (LRU)

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.

Usage

go get -u github.com/TwiN/gocache/v2

Initializing the cache

cache := gocache.NewCache().WithMaxSize(1000).WithEvictionPolicy(gocache.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()

Functions

Function Description
WithMaxSize Sets the max size of the cache. gocache.NoMaxSize means there is no limit. If not set, the default max size is gocache.DefaultMaxSize.
WithMaxMemoryUsage Sets the max memory usage of the cache. gocache.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 gocache.FirstInFirstOut (FIFO).
WithDefaultTTL Sets the default TTL for each entry.
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 using the default TTL (which is gocache.NoExpiration, unless configured otherwise).
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
SetAll Same as Set, but in bulk.
SetAllWithTTL Same as SetWithTTL, but in bulk.
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.

For further documentation, please refer to Go Reference

Examples

Creating or updating an entry

cache.Set("key", "value") 
cache.Set("key", 1)
cache.Set("key", struct{ Text string }{Test: "value"})
cache.SetWithTTL("key", []byte("value"), 24*time.Hour)

Getting an entry

value, exists := cache.Get("key")

You can also get multiple entries by using cache.GetByKeys([]string{"key1", "key2"})

Deleting an entry

cache.Delete("key")

You can also delete multiple entries by using cache.DeleteAll([]string{"key1", "key2"})

Complex example

package main

import (
    "fmt"
    "time"

    "github.com/TwiN/gocache/v2"
)

func main() {
    cache := gocache.NewCache().WithEvictionPolicy(gocache.LeastRecentlyUsed).WithMaxSize(10000)
    cache.StartJanitor() // Passively manages expired entries
    defer cache.StopJanitor()

    cache.Set("key", "value")
    cache.SetWithTTL("key-with-ttl", "value", 60*time.Minute)
    cache.SetAll(map[string]any{"k1": "v1", "k2": "v2", "k3": "v3"})

    fmt.Println("[Count] Cache size:", cache.Count())

    value, exists := cache.Get("key")
    fmt.Printf("[Get] key=key; value=%s; exists=%v\n", value, exists)
    for key, value := range cache.GetByKeys([]string{"k1", "k2", "k3"}) {
        fmt.Printf("[GetByKeys] key=%s; value=%s\n", key, value)
    }
    for _, key := range cache.GetKeysByPattern("key*", 0) {
        fmt.Printf("[GetKeysByPattern] pattern=key*; key=%s\n", key)
    }

    cache.Expire("key", time.Hour)
    time.Sleep(500*time.Millisecond)
    timeUntilExpiration, _ := cache.TTL("key")
    fmt.Println("[TTL] Number of minutes before 'key' expires:", int(timeUntilExpiration.Seconds()))

    cache.Delete("key")
    cache.DeleteAll([]string{"k1", "k2", "k3"})

    cache.Clear()
    fmt.Println("[Count] Cache size after clearing the cache:", cache.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

Persistence

Prior to v2, gocache supported persistence out of the box.

After some thinking, I decided that persistence added too many dependencies, and given than this is a cache library and most people wouldn't be interested in persistence, I decided to get rid of it.

That being said, you can use the GetAll and SetAll methods of gocache.Cache to implement persistence yourself.

Eviction

MaxSize

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:

cache := gocache.NewCache().WithMaxSize(1000)

This means that whenever an operation causes the total size of the cache to go above 1000, the tail will be evicted.

MaxMemoryUsage

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:

cache := gocache.NewCache().WithMaxSize(0).WithMaxMemoryUsage(50*gocache.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

Core symbols most depended-on inside this repo

Set
called by 85
gocache.go
NewCache
called by 64
gocache.go
Get
called by 51
gocache.go
WithMaxSize
called by 49
gocache.go
MemoryUsage
called by 28
gocache.go
SetWithTTL
called by 22
gocache.go
Count
called by 16
gocache.go
WithMaxMemoryUsage
called by 12
gocache.go

Shape

Function 73
Method 36
Struct 6
TypeAlias 1

Languages

Go100%

Modules by API surface

gocache_test.go51 symbols
gocache.go33 symbols
gocache_bench_test.go13 symbols
janitor_test.go6 symbols
entry.go5 symbols
janitor.go2 symbols
entry_test.go2 symbols
statistics.go1 symbols
policy.go1 symbols
pattern_test.go1 symbols
pattern.go1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add gocache \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact