1- <p align =" center " >
2- <img src =" media/logo-small.png " alt =" bdcache " width =" 200 " />
3- </p >
4-
51# bdcache - Big Dumb Cache
62
7- Simple, fast, reliable Go cache built for Google Cloud Run and local development.
3+ [ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/codeGROOVE-dev/bdcache.svg )] ( https://pkg.go.dev/github.com/codeGROOVE-dev/bdcache )
4+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/codeGROOVE-dev/bdcache )] ( https://goreportcard.com/report/github.com/codeGROOVE-dev/bdcache )
5+ [ ![ License] ( https://img.shields.io/badge/License-Apache%202.0-blue.svg )] ( https://opensource.org/licenses/Apache-2.0 )
6+
7+ Simple, fast, reliable Go cache with [ S3-FIFO eviction] ( https://s3fifo.com/ ) - better hit rates than LRU.
88
99## Why?
1010
11- - ** Actually Simple** - Not 47 configuration options. Just cache stuff.
12- - ** Actually Fast** - ~ 19ns per operation. Zero allocations.
13- - ** Actually Reliable** - Gracefully degrades when things break (and they will).
14- - ** Smart Persistence** - Local files for dev/persistent processes. Cloud Datastore for stateless Cloud Run.
11+ - ** S3-FIFO Algorithm** - [ Superior cache hit rates] ( https://s3fifo.com/ ) compared to LRU/LFU
12+ - ** Fast** - ~ 19ns per operation, zero allocations
13+ - ** Reliable** - Memory cache always works, even if persistence fails
14+ - ** Smart Persistence** - Local files for dev. Cloud Datastore for Cloud Run
15+ - ** Minimal Dependencies** - Only one optional dependency (Cloud Datastore)
1516
1617## Install
1718
1819``` bash
19- go get github.com/tstromberg /bdcache
20+ go get github.com/codeGROOVE-dev /bdcache
2021```
2122
22- ## Use It
23+ ## Use
2324
2425``` go
2526// Memory only
26- cache , _ := bdcache.New [string , int ](ctx)
27- cache.Set (ctx, " answer" , 42 , 0 )
28- val , found , _ := cache.Get (ctx, " answer" )
29-
30- // Smart default (recommended)
31- // - Uses Cloud Datastore on Cloud Run (stateless, shared cache)
32- // - Uses local files elsewhere (dev, persistent processes)
33- cache , _ := bdcache.New [string , User ](ctx, bdcache.WithBestStore (" myapp" ))
34-
35- // Explicit local files (for dev/persistent processes)
36- cache , _ := bdcache.New [string , Data ](ctx, bdcache.WithLocalStore (" myapp" ))
37-
38- // Explicit Cloud Datastore (for Cloud Run/stateless)
39- cache , _ := bdcache.New [string , Thing ](ctx, bdcache.WithCloudDatastore (" myapp" ))
27+ cache , err := bdcache.New [string , int ](ctx)
28+ if err != nil {
29+ panic (err)
30+ }
31+ if err := cache.Set (ctx, " answer" , 42 , 0 ); err != nil {
32+ panic (err)
33+ }
34+ val , found , err := cache.Get (ctx, " answer" )
35+
36+ // With smart persistence (files for dev, Datastore for Cloud Run)
37+ cache , err := bdcache.New [string , User ](ctx, bdcache.WithBestStore (" myapp" ))
4038```
4139
4240## Features
4341
44- - ** S3-FIFO eviction** - Better hit rates than your LRU
45- - ** Type safe** - Go generics, not ` interface{} `
46- - ** Dual persistence** - Local files (gob) for dev. Cloud Datastore for Cloud Run.
47- - ** Graceful** - Persistence broken? Fine. You get memory-only.
48- - ** On-demand loading** - Loads from persistence as needed. Optional warmup available.
49- - ** Per-item TTL** - Or don't. Whatever.
50-
51- ## Persistence
52-
53- ** Local files** (gob-encoded) for local development and persistent processes:
54- - Fast, simple, no external dependencies
55- - Survives restarts on VMs or local machines
56- - Stored in OS-appropriate cache directory with squid-style subdirectories (first 2 chars of key)
57- - Keys limited to 127 characters: alphanumeric, dash, underscore, period, colon (a-z A-Z 0-9 - _ . :)
58-
59- ** Cloud Datastore** (JSON-encoded) for Google Cloud Run:
60- - Shared cache across stateless instances
61- - Survives cold starts and redeployments
62- - Auto-detects project ID, uses database namespacing
63- - Keys limited to 1500 characters (Datastore constraint)
42+ - ** S3-FIFO eviction** - Better than LRU ([ learn more] ( https://s3fifo.com/ ) )
43+ - ** Type safe** - Go generics
44+ - ** Persistence** - Local files (gob) or Cloud Datastore (JSON)
45+ - ** Graceful degradation** - Cache works even if persistence fails
46+ - ** Per-item TTL** - Optional expiration
6447
6548## Performance
6649
@@ -69,45 +52,6 @@ BenchmarkCache_Get_Hit-16 66M ops/sec 18.5 ns/op 0 allocs
6952BenchmarkCache_Set-16 61M ops/sec 19.3 ns/op 0 allocs
7053```
7154
72- ## Options
73-
74- - ` WithMemorySize(n) ` - Max items in RAM (default: 10k)
75- - ` WithDefaultTTL(d) ` - Default expiration (default: never)
76- - ` WithLocalStore(id) ` - Enable file persistence
77- - ` WithCloudDatastore(id) ` - Enable Datastore persistence
78- - ` WithBestStore(id) ` - Auto-select based on environment
79- - ` WithWarmup(n) ` - Pre-load N most recent items on startup (default: disabled)
80-
81- ## API
82-
83- ### Core Methods
84-
85- - ` Get(ctx, key) (V, bool, error) ` - Retrieve value, returns (value, found, error)
86- - ` Set(ctx, key, value, ttl) error ` - Store value with optional TTL. ttl=0 uses DefaultTTL if configured
87- - ** Reliability guarantee** : Value is ALWAYS stored in memory, even if persistence fails
88- - Returns error if key violates persistence constraints or if persistence fails
89- - Even when error is returned, value is cached in memory
90- - ` Delete(ctx, key) ` - Remove value (void)
91- - ` Cleanup() int ` - Remove expired entries, returns count
92- - ` Len() int ` - Get memory cache size
93- - ` Close() error ` - Release resources
94-
95- ### Key Constraints
96-
97- ** File persistence** :
98- - Maximum 127 characters
99- - Only alphanumeric, dash, underscore, period, colon (a-z A-Z 0-9 - _ . :)
100- - Keys violating these constraints return an error from Set()
101-
102- ** Datastore persistence** :
103- - Maximum 1500 characters
104- - Any non-empty string allowed
105- - Keys violating these constraints return an error from Set()
106-
107- ** Memory-only** (no persistence):
108- - No key constraints
109- - Any comparable type works
110-
11155## License
11256
113- MIT
57+ Apache 2.0
0 commit comments