Deep Dive into FastCache: High‑Performance Local Cache for Go
FastCache is an open‑source Go local‑cache library that delivers extreme speed, zero GC pressure, thread safety, and a five‑method API, making it ideal for high‑concurrency services while providing clear usage guidelines, pitfalls, and suitable scenarios.
What is FastCache?
FastCache is an open‑source Go local‑cache library from the VictoriaMetrics team, targeting “high performance, low overhead, zero GC”. It solves the performance bottleneck and GC pressure of traditional caches such as sync.Map or a map protected by a lock in large‑data, high‑concurrency scenarios.
In short, it is a lightweight, ultra‑fast, hassle‑free local cache that can store massive key‑value data without adding GC load, keeping high‑concurrency services stable.
Why choose FastCache? Core advantages
Extreme speed : sharded bucket + local lock design enables multi‑core parallelism, linear throughput scaling, and can handle tens of millions of QPS.
Zero GC pressure : data resides in pre‑allocated memory blocks without extra pointers, resulting in stable latency and no “GC pause” nightmares.
Ultra‑simple API : only five methods ( New, Set, Get, Del, Has) – a newcomer can start in five minutes, no configuration required.
Thread‑safety : internal concurrency control is encapsulated, allowing any number of goroutines to read/write without additional locking.
Automatic eviction : set a maximum memory size; when full, old entries are evicted automatically, preventing memory explosion.
Pitfall guide
✅ Key/Value must be []byte → serialize strings/structs first
✅ No built‑in expiration → store a timestamp in the value and check on Get
✅ Value > 64KB → use SetBig for more stable performance
✅ Max memory is fixed after initialization → suitable for long‑running servicesPractical code (copy‑and‑paste)
1️⃣ Install dependency
go get github.com/VictoriaMetrics/fastcache2️⃣ Basic usage (≈20 lines)
package main
import (
"fmt"
"github.com/VictoriaMetrics/fastcache"
)
func main() {
// Init cache with 100 MB
cache := fastcache.New(100 * 1024 * 1024)
// Set: key/value must be []byte
cache.Set([]byte("user:1001"), []byte(`{"name":"张三","age":25}`))
// Get
var dst []byte
if val := cache.Get(dst, []byte("user:1001")); len(val) > 0 {
fmt.Println("✅ Get success:", string(val))
}
// Check existence and delete
if cache.Has([]byte("user:1001")) {
cache.Del([]byte("user:1001"))
fmt.Println("🗑️ Cache deleted")
}
}3️⃣ Advanced: storing structs (serialization tip)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize
user := User{Name: "李四", Age: 30}
value, _ := json.Marshal(user)
cache.Set([]byte("user:1002"), value)
// Deserialize
if val := cache.Get(dst, []byte("user:1002")); len(val) > 0 {
var u User
json.Unmarshal(val, &u)
fmt.Println("👤 User info:", u) // Output: {李四 30}
}Suitable scenarios quick reference
✅ Strongly recommended: high‑concurrency API gateways, recommendation systems (million‑plus QPS, low latency), monitoring metrics, IoT device status (time‑series, large data).
✅ Simple cache needs: basic set/get/delete without fancy features.
❌ Use with caution: precise expiration requirements, complex eviction policies (LRU/LFU), frequent storage of very large values (> 1 MB).
Conclusion
FastCache’s core value is fast + light + stable . It achieves speed with sharded buckets that outperform sync.Map, lightness with a minimal five‑method API that can be mastered in five minutes, and stability with zero GC pressure and automatic memory control, making it an ideal choice for high‑concurrency Go services that need a high‑performance local cache.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
