RepositoryGo
unkn0wn-root

unkn0wn-root/kioshun

Fast(est), sharded Go cache with lock-free reads, self-adaptive SieveTinyLFU, batched async writes and HTTP middleware.

71stars
1forks
0issues
2views
Kioshun Logo

Kioshun - In-Memory Cache for Go

"kee-oh-shoon" /kiːoʊʃuːn/

Go Version License CI

Fast, sharded in-memory cache for Go

Index

Installation

go get github.com/unkn0wn-root/kioshun

Quick Start

package main

import (
    "fmt"
    "time"

    "github.com/unkn0wn-root/kioshun"
)

func main() {
    // Create cache with default configuration
    c := kioshun.NewDefault[string, string]()
    defer c.Close()

    // Set with default TTL (30 min)
    c.Set("user:123", "David", kioshun.DefaultExpiration)

    // Set commits the write before returning so the key is immediately readable.
    c.Set("user:456", "John", kioshun.NoExpiration)

    // SetAsync returns early. It may commit inline when the shard is idle
    // otherwise it queues the write for that shard's worker.
    c.SetAsync("user:789", "Paul", 5*time.Minute)

    // Optional: call Sync() when committed visibility is required.
    c.Sync()

    // Get value
    if value, found := c.Get("user:123"); found {
        fmt.Printf("User: %s\n", value)
    }

}

Configuration

For most caches, start with the built-in defaults:

c := kioshun.NewDefault[string, string]()
defer c.Close()

NewDefault uses DefaultConfig(): MaxSize=10000, DefaultTTL=30m, automatic shard count, CleanupInterval=5m, SieveTinyLFU eviction, stats disabled and the default async write buffer/batch sizes.

When you only need a few changes, start from DefaultConfig() and override the specific fields:

config := kioshun.DefaultConfig()
config.MaxSize = 100000
config.DefaultTTL = time.Hour

c, err := kioshun.New[string, string](config)
if err != nil {
    // handle error
}

Every Config field, weighted capacity (MaxCost + WithWeigher) and the named cache Manager are covered in configuration.md.

API

c.Set(key, value, ttl time.Duration) error
c.SetAsync(key, value, ttl time.Duration) error
c.SetWithCallback(key, value, ttl, callback func(key, value)) error
c.Get(key) (value, found bool)
c.GetWithTTL(key) (value, ttl time.Duration, found bool)
c.Keys() []K
c.Clear()
c.Sync() error
c.Delete(key) bool
c.Exists(key) bool
c.Size() int64
c.Stats() Stats
c.PolicyStats() PolicyStats
c.Cleanup()
c.Close() error

Set is synchronous and gives immediate 'read-after-write' visibility for the key. SetAsync is optional - it may have committed inline already or it may still be queued. Use Sync when committed visibility is required.

HTTP Middleware

import "github.com/unkn0wn-root/kioshun/httpcache"

config := httpcache.DefaultConfig()
config.DefaultTTL = 5 * time.Minute
config.MaxSize = 100000

middleware, err := httpcache.New(config)
if err != nil {
    // handle error
}
defer middleware.Close()

http.Handle("/api/users", middleware.Wrap(usersHandler))

See http.md for complete documentation.

Benchmarks

Hit ratio and throughput

Kioshun is compared against Ristretto, Otter and Theine by replaying ARC and LIRS request traces (P3, P8, S3, DS1, OLTP, LOOP) through every cache in the same harness at a 100,000 entry cap. Hit ratio is plotted as a percentage of the Belady optimum.

Hit ratio vs the Belady optimum

Hit ratio vs cache size

Throughput

Hit ratio vs throughput

The 100k entry cap.

traceOPTkioshuntheineotterristretto
p362.00%44.94%38.14%36.41%21.22%
p877.12%59.85%54.10%56.56%55.83%
s325.42%13.46%11.90%9.06%9.80%
ds15.16%1.97%1.51%1.57%1.36%
oltp79.56%79.29%76.67%76.41%28.30%
loop99.80%99.26%99.79%99.60%90.39%

Hit ratio as a percent of that ceiling:

tracekioshuntheineotterristretto
p371.361.558.734.2
p877.670.273.372.4
s349.346.835.638.6
ds136.829.330.426.4
oltp98.896.496.035.6
loop99.4100.099.890.6
geomean67.962.159.544.8

Microbenchmarks

A separate suite compares Kioshun with Ristretto, BigCache, FreeCache and go-cache using pre-generated workloads with async and strict write modes reported separately:

cd benchmarks
go test -run=TestBenchmarkComparisonGetSetup -count=1 .
go test -bench='BenchmarkCacheComparison' -benchmem -run=^$ -benchtime=1s .
go test -bench=. -benchmem -run=^$ -benchtime=1s -timeout=30m .

Similar Projects

Discover other similar projects and AI models.

RepositoryGo
VictoriaMetrics/fastcache

Fast thread-safe inmemory cache for big number of entries in Go. Minimizes GC overhead

2.4k
Jun 15, 2026
RepositoryGo
avelino/awesome-go

A curated list of awesome Go frameworks, libraries and software

197.2k
Dec 2, 2026
RepositoryGo
ollama/ollama

Get up and running with Inkling, Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

188.9k
Dec 2, 2026
RepositoryC#
MichaCo/CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.

2.4k
Nov 2, 2026
RepositoryC#
TurnerSoftware/CacheTower

An efficient multi-layered caching system for .NET

629
Nov 10, 2026
RepositoryGo
golang/go

The Go programming language

140.0k
Dec 1, 2026
RepositoryGo
rclone/rclone

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

61.4k
Nov 27, 2026
RepositoryGo
moby/moby

The Moby Project - a collaborative project for the container ecosystem to assemble container-based systems

72.3k
Nov 26, 2026
RepositoryGo
traefik/traefik

The Cloud Native Application Proxy

65.9k
Dec 2, 2026