仓库地址: https://github.com/lxzan/memorycache
轻量级的内存 KV 存储, 由哈希图和最小堆实现, 没有特别为 GC 做优化. 拥有 O(1)的读取效率, O(logN)的写入效率. 缓存淘汰策略: 检测到过期或者容量溢出时, 堆顶元素会被清除一部分, 每次至多删除 16*1000(默认值, 后面省略)个 key, 30s 检查一次.
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
goos: darwin
goarch: arm64
pkg: github.com/lxzan/memorycache/benchmark
BenchmarkSet/10000-8 13830640 87.25 ns/op 0 B/op 0 allocs/op
BenchmarkSet/1000000-8 3615801 326.6 ns/op 58 B/op 0 allocs/op
BenchmarkGet/10000-8 14347058 82.28 ns/op 0 B/op 0 allocs/op
BenchmarkGet/1000000-8 3899768 262.6 ns/op 54 B/op 0 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 13.037s
1,000,000个key, 并行测试 (RunParallel)
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
goos: darwin
goarch: arm64
pkg: github.com/lxzan/memorycache/benchmark
BenchmarkMemoryCache_Set-8 7295934 153.1 ns/op 25 B/op 0 allocs/op
BenchmarkMemoryCache_Get-8 23053038 50.43 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-8 12036328 217.1 ns/op 123 B/op 2 allocs/op
BenchmarkRistretto_Get-8 16294044 75.40 ns/op 17 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 13.268s
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-12 6762400 150.7 ns/op 27 B/op 0 allocs/op
BenchmarkMemoryCache_Get-12 28543896 43.41 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-12 11597167 264.8 ns/op 121 B/op 2 allocs/op
BenchmarkRistretto_Get-12 22389213 48.94 ns/op 16 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 14.818s
1
rainboat 2023-10-27 15:17:44 +08:00 via iPhone
有没有和现有的主流方法进行比较实验呢
|
3
Nazz OP |
4
Nazz OP |
5
matrix1010 2023-10-27 16:41:13 +08:00
看到你的对比图我立刻想到了以前看到的鹅厂 tcache: https://zhuanlan.zhihu.com/p/624248354
|
6
Nazz OP @matrix1010 我转发的
|
7
GrayXu 2023-10-27 20:53:59 +08:00
KV cache 的性能挺好对比的,建议跑个 benchmark 。
感觉主贴是不是把 eviction 和 GC 混在一起说了。 |
9
Nazz OP @GrayXu 使用默认参数做个简单测试
``` go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark goos: darwin goarch: arm64 pkg: github.com/lxzan/memorycache/benchmark BenchmarkMemoryCacheSet/10000-8 13762460 86.71 ns/op 0 B/op 0 allocs/op BenchmarkMemoryCacheSet/1000000-8 3560780 320.7 ns/op 59 B/op 0 allocs/op BenchmarkMemoryCacheGet/10000-8 14570577 81.50 ns/op 0 B/op 0 allocs/op BenchmarkMemoryCacheGet/1000000-8 4752176 240.3 ns/op 44 B/op 0 allocs/op BenchmarkRistrettoSet/10000-8 3887132 312.3 ns/op 110 B/op 2 allocs/op BenchmarkRistrettoSet/1000000-8 3760880 493.4 ns/op 242 B/op 2 allocs/op BenchmarkRistrettoGet/10000-8 9499491 123.7 ns/op 29 B/op 1 allocs/op BenchmarkRistrettoGet/1000000-8 3122341 365.6 ns/op 148 B/op 1 allocs/op PASS ok github.com/lxzan/memorycache/benchmark 35.356s ``` |