Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Nov 10, 2023
1 parent ee494eb commit 4d4ac3c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
vendor/
examples/
bin/
go.work
go.work*
.DS_Store
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# MemoryCache

[中文](README_CN.md)

[![Build Status][1]][2] [![codecov][3]][4]

[1]: https://github.com/lxzan/memorycache/workflows/Go%20Test/badge.svg?branch=main
Expand All @@ -9,18 +11,18 @@

### Description

Minimalist in-memory KV storage, powered by `HashMap` and minimal `Quad Heap`, without optimizations for GC.
Minimalist in-memory KV storage, powered by `HashMap` and `Minimal Quad Heap`, without optimizations for GC.

**Cache deprecation policy:**
**Cache Elimination Policy:**

1. Set method cleans up overflowed keys
2. Active cycle cleans up expired keys.
2. Active cycle cleans up expired keys

### Principle

- Storage Data Limit: Limited by maximum capacity
- Expiration Time: Supported
- Cache Elimination Policy: LRU-Like, Set method and Cycle Cleanup
- Cache Elimination Policy: LRU-Like
- GC Optimization: None
- Persistent: None
- Locking Mechanism: Slicing + Mutual Exclusion Locking
Expand All @@ -31,7 +33,7 @@ Minimalist in-memory KV storage, powered by `HashMap` and minimal `Quad Heap`, w
- No third-party dependencies
- High performance
- Low memory usage
- Quad Heap reduced LRU algorithm from `O(n) to O(logn)`
- Use Quad Heap to reduce height and increase write speeds

### Methods

Expand Down Expand Up @@ -60,7 +62,7 @@ func main() {
memorycache.WithBucketSize(1000, 10000), // Bucket size, initial size and maximum capacity.
memorycache.WithInterval(5*time.Second, 30*time.Second), // Active cycle cleanup interval and expiration time.
)
defer mc.Stop() // Stop memorycache.
defer mc.Stop()

mc.Set("xxx", 1, 10*time.Second)

Expand All @@ -79,14 +81,13 @@ func main() {
- 1,000,000 elements

```
goos: windows
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-12 14058852 73.00 ns/op 14 B/op 0 allocs/op
BenchmarkMemoryCache_Get-12 30767100 34.70 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-12 15583969 218.4 ns/op 114 B/op 2 allocs/op
BenchmarkRistretto_Get-12 27272788 42.05 ns/op 16 B/op 1 allocs/op
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 11106261 100.6 ns/op 18 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 635988 77.30 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 7933663 491.8 ns/op 170 B/op 2 allocs/op
BenchmarkRistretto_Get-4 11085688 98.92 ns/op 18 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 17.232s
```
47 changes: 23 additions & 24 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

### 简介:

这段文字介绍了一种内存中的极简键值(KV)存储系统,其核心由 HashMap 和最小化的 Quad Heap 构成,没有进行垃圾回收(GC)优化。
极简的内存键值(KV)存储系统,其核心由 哈希表(HashMap) 和 最小四叉堆(Minimal Quad Heap) 构成,没有进行垃圾回收(GC)优化。

**缓存过期策略**
**缓存淘汰策略**

1. 使用 Set 方法用于清理溢出的键值对
2. 主动周期清理过期的键值对。
1. 使用 Set 方法用于清理溢出的键值对
2. 周期清理过期的键值对

### 原则:

1. 存储数据限制:受最大容量限制
2. 过期时间:支持为键值对设置过期时间。
3. 缓存淘汰策略:类似于 LRU,使用 Set 方法和周期清理来维护缓存的新鲜度。
4. GC 优化:未进行任何优化。
5. 持久性:不支持数据持久化到磁盘。
6. 锁定机制:采用切片和互斥锁确保线程安全。
1. 存储数据限制:受最大容量限制
2. 过期时间:支持
3. 缓存淘汰策略:类似LRU
4. GC 优化:
5. 持久化:无
6. 锁定机制:分片和互斥锁

### 优势:

1. 简单易用
2. 无需第三方依赖
3. 高性能。
4. 内存占用低
5. 通过 Quad Heap 将 LRU 算法的时间复杂度从 O(n) 降至 O(logn)。
1. 简单易用
2. 无需第三方依赖
3. 高速读写
4. 内存占用低
5. 使用四叉堆, 有效降低数高度, 提高插入性能

### 方法:

Expand All @@ -43,7 +43,7 @@
- [x] **GetOrCreate** : 根据键获取值。如果键不存在,将创建该值。
- [x] **GetOrCreateWithCallback** : 根据键获取值。如果键不存在,将创建该值,并可调用回调函数。

### 举例
### 使用

```go
package main
Expand All @@ -60,7 +60,7 @@ func main() {
memorycache.WithBucketSize(1000, 10000), // Bucket size, initial size and maximum capacity.
memorycache.WithInterval(5*time.Second, 30*time.Second), // Active cycle cleanup interval and expiration time.
)
defer mc.Stop() // Stop memorycache.
defer mc.Stop()

mc.Set("xxx", 1, 10*time.Second)

Expand All @@ -79,14 +79,13 @@ func main() {
- 1,000,000 元素

```
goos: windows
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-12 14058852 73.00 ns/op 14 B/op 0 allocs/op
BenchmarkMemoryCache_Get-12 30767100 34.70 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-12 15583969 218.4 ns/op 114 B/op 2 allocs/op
BenchmarkRistretto_Get-12 27272788 42.05 ns/op 16 B/op 1 allocs/op
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 11106261 100.6 ns/op 18 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 635988 77.30 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 7933663 491.8 ns/op 170 B/op 2 allocs/op
BenchmarkRistretto_Get-4 11085688 98.92 ns/op 18 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 17.232s
```
14 changes: 0 additions & 14 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ func BenchmarkMemoryCache_Get(b *testing.B) {
})
}

func BenchmarkMemoryCache_GetOrCreate(b *testing.B) {
var mc = memorycache.New(
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
)
var i = atomic.Int64{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
index := i.Add(1) % benchcount
mc.GetOrCreate(benchkeys[index], 1, time.Hour)
}
})
}

func BenchmarkRistretto_Set(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
Expand Down
1 change: 1 addition & 0 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
)

Expand Down
9 changes: 8 additions & 1 deletion benchmark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4d4ac3c

Please sign in to comment.