Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	cache.go
  • Loading branch information
lxzan committed Dec 24, 2023
2 parents d0d607b + c51e6b2 commit f752915
Show file tree
Hide file tree
Showing 19 changed files with 685 additions and 246 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.go]
indent_style = tab
indent_size = 4

[Makefile]
indent_style = tab

[*.{yml,yaml}]
indent_style = space
indent_size = 2
20 changes: 10 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Test
run: go test -v ./...
- name: Bench
run: make bench
- name: Test
run: make test

- name: Bench
run: make bench
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test:
go test -count 1 -timeout 30s -run ^Test ./...
go test -count 1 -timeout 30s -run ^Test github.com/lxzan/memorycache/benchmark

bench:
go test -benchmem -run=^$$ -bench . github.com/lxzan/memorycache/benchmark
Expand Down
62 changes: 35 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
[![Build Status][1]][2] [![codecov][3]][4]

[1]: https://github.com/lxzan/memorycache/workflows/Go%20Test/badge.svg?branch=main

[2]: https://github.com/lxzan/memorycache/actions?query=branch%3Amain

[3]: https://codecov.io/gh/lxzan/memorycache/graph/badge.svg?token=OHD6918OPT

[4]: https://codecov.io/gh/lxzan/memorycache

### 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`.

**Cache Elimination Policy:**

Expand All @@ -25,29 +28,34 @@ Minimalist in-memory KV storage, powered by `HashMap` and `Minimal Quad Heap`, w

### Principle

- Storage Data Limit: Limited by maximum capacity
- Expiration Time: Supported
- Cache Eviction Policy: LRU
- GC Optimization: None
- Persistent: None
- Locking Mechanism: Slicing + Mutual Exclusion Locking
- Storage Data Limit: Limited by maximum capacity
- Expiration Time: Supported
- Cache Eviction Policy: LRU
- Persistent: None
- Locking Mechanism: Slicing + Mutual Exclusion Locking
- HashMap, Heap and LinkedList (excluding user KVs) implemented in pointerless technology

### Advantage

- Simple and easy to use
- High performance
- Low memory usage
- Use quadruple heap to maintain the expiration time, effectively reduce the height of the tree, and improve the insertion performance
- Simple and easy to use
- High performance
- Low memory usage
- Use quadruple heap to maintain the expiration time, effectively reduce the height of the tree, and improve the
insertion performance

### Methods

- [x] **Set** : Set key-value pair with expiring time. If the key already exists, the value will be updated. Also the expiration time will be updated.
- [x] **SetWithCallback** : Set key-value pair with expiring time and callback function. If the key already exists, the value will be updated. Also the expiration time will be updated.
- [x] **Set** : Set key-value pair with expiring time. If the key already exists, the value will be updated. Also the
expiration time will be updated.
- [x] **SetWithCallback** : Set key-value pair with expiring time and callback function. If the key already exists,
the value will be updated. Also the expiration time will be updated.
- [x] **Get** : Get value by key. If the key does not exist, the second return value will be false.
- [x] **GetWithTTL** : Get value by key. If the key does not exist, the second return value will be false. When return value, method will refresh the expiration time.
- [x] **GetWithTTL** : Get value by key. If the key does not exist, the second return value will be false. When return
value, method will refresh the expiration time.
- [x] **Delete** : Delete key-value pair by key.
- [x] **GetOrCreate** : Get value by key. If the key does not exist, the value will be created.
- [x] **GetOrCreateWithCallback** : Get value by key. If the key does not exist, the value will be created. Also the callback function will be called.
- [x] **GetOrCreateWithCallback** : Get value by key. If the key does not exist, the value will be created. Also the
callback function will be called.

### Example

Expand Down Expand Up @@ -84,22 +92,22 @@ func main() {

### Benchmark

- 1,000,000 elements
- 1,000,000 elements

```
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 10949929 99.34 ns/op 27 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 19481263 61.18 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-4 18691801 64.24 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 10051786 448.1 ns/op 152 B/op 2 allocs/op
BenchmarkRistretto_Get-4 12461653 85.71 ns/op 18 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-4 7832054 159.4 ns/op 46 B/op 1 allocs/op
BenchmarkTheine_Set-4 4692495 274.3 ns/op 51 B/op 0 allocs/op
BenchmarkTheine_Get-4 14084695 85.59 ns/op 0 B/op 0 allocs/op
BenchmarkTheine_SetAndGet-4 6135094 199.9 ns/op 0 B/op 0 allocs/op
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-8 16107153 74.85 ns/op 15 B/op 0 allocs/op
BenchmarkMemoryCache_Get-8 28859542 42.34 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-8 27317874 63.02 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-8 13343023 272.6 ns/op 120 B/op 2 allocs/op
BenchmarkRistretto_Get-8 19799044 55.06 ns/op 17 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-8 11212923 119.6 ns/op 30 B/op 1 allocs/op
BenchmarkTheine_Set-8 3775975 322.5 ns/op 30 B/op 0 allocs/op
BenchmarkTheine_Get-8 21579301 54.94 ns/op 0 B/op 0 allocs/op
BenchmarkTheine_SetAndGet-8 6265330 224.6 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 60.259s
ok github.com/lxzan/memorycache/benchmark 53.498s
```
31 changes: 17 additions & 14 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
[![Build Status][1]][2] [![codecov][3]][4]

[1]: https://github.com/lxzan/memorycache/workflows/Go%20Test/badge.svg?branch=main

[2]: https://github.com/lxzan/memorycache/actions?query=branch%3Amain

[3]: https://codecov.io/gh/lxzan/memorycache/graph/badge.svg?token=OHD6918OPT

[4]: https://codecov.io/gh/lxzan/memorycache

### 简介:

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

**缓存淘汰策略:**

Expand All @@ -25,9 +28,9 @@
- 存储数据限制:受最大容量限制
- 过期时间:支持
- 缓存驱逐策略:LRU
- GC 优化:无
- 持久化:无
- 锁定机制:分片和互斥锁
- GC 优化:无指针技术实现的哈希表, 最小堆和链表(不包括用户KV)

### 优势:

Expand Down Expand Up @@ -81,22 +84,22 @@ func main() {

### 基准测试

- 1,000,000 元素
- 1,000,000 元素

```
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 10949929 99.34 ns/op 27 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 19481263 61.18 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-4 18691801 64.24 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 10051786 448.1 ns/op 152 B/op 2 allocs/op
BenchmarkRistretto_Get-4 12461653 85.71 ns/op 18 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-4 7832054 159.4 ns/op 46 B/op 1 allocs/op
BenchmarkTheine_Set-4 4692495 274.3 ns/op 51 B/op 0 allocs/op
BenchmarkTheine_Get-4 14084695 85.59 ns/op 0 B/op 0 allocs/op
BenchmarkTheine_SetAndGet-4 6135094 199.9 ns/op 0 B/op 0 allocs/op
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-8 16107153 74.85 ns/op 15 B/op 0 allocs/op
BenchmarkMemoryCache_Get-8 28859542 42.34 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-8 27317874 63.02 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-8 13343023 272.6 ns/op 120 B/op 2 allocs/op
BenchmarkRistretto_Get-8 19799044 55.06 ns/op 17 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-8 11212923 119.6 ns/op 30 B/op 1 allocs/op
BenchmarkTheine_Set-8 3775975 322.5 ns/op 30 B/op 0 allocs/op
BenchmarkTheine_Get-8 21579301 54.94 ns/op 0 B/op 0 allocs/op
BenchmarkTheine_SetAndGet-8 6265330 224.6 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 60.259s
ok github.com/lxzan/memorycache/benchmark 53.498s
```
38 changes: 37 additions & 1 deletion benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/Yiling-J/theine-go"
"github.com/dgraph-io/ristretto"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/lxzan/memorycache"
"github.com/lxzan/memorycache/internal/utils"
"github.com/stretchr/testify/assert"
)

const (
Expand All @@ -23,7 +25,6 @@ var (
memorycache.WithBucketNum(sharding),
memorycache.WithBucketSize(capacity/10, capacity),
memorycache.WithSwissTable(false),
memorycache.WithLRU(true),
}
)

Expand Down Expand Up @@ -198,3 +199,38 @@ func BenchmarkTheine_SetAndGet(b *testing.B) {
}
})
}

// 测试LRU算法实现的正确性
func TestLRU_Impl(t *testing.T) {
var f = func() {
var count = 10000
var capacity = 5000
var mc = memorycache.New[string, int](
memorycache.WithBucketNum(1),
memorycache.WithBucketSize(capacity, capacity),
)
var cache, _ = lru.New[string, int](capacity)
for i := 0; i < count; i++ {
key := string(utils.AlphabetNumeric.Generate(16))
val := utils.AlphabetNumeric.Intn(capacity)
mc.Set(key, val, time.Hour)
cache.Add(key, val)
}

keys := cache.Keys()
assert.Equal(t, mc.Len(), capacity)
assert.Equal(t, mc.Len(), cache.Len())
assert.Equal(t, mc.Len(), len(keys))

for _, key := range keys {
v1, ok1 := mc.Get(key)
v2, _ := cache.Peek(key)
assert.True(t, ok1)
assert.Equal(t, v1, v2)
}
}

for i := 0; i < 10; i++ {
f()
}
}
6 changes: 6 additions & 0 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ go 1.19
require (
github.com/Yiling-J/theine-go v0.3.1
github.com/dgraph-io/ristretto v0.1.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/lxzan/memorycache v1.0.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dolthub/maphash v0.1.0 // indirect
github.com/dolthub/swiss v0.2.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gammazero/deque v0.2.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lxzan/dao v1.1.2 // indirect
github.com/ncw/directio v1.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/lxzan/memorycache v1.0.0 => ../
10 changes: 10 additions & 0 deletions benchmark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/Yiling-J/theine-go v0.3.1/go.mod h1:9HtlXa6gjwnqdhqW0R/0BDHxGF4CNmZdV
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -20,9 +21,15 @@ github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0
github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lxzan/dao v1.1.2 h1:ATYm6yBE117iQetDTmCZeHB4mBFjO43NbsEt7wf/JBo=
github.com/lxzan/dao v1.1.2/go.mod h1:5ChTIo7RSZ4upqRo16eicJ3XdJWhGwgMIsyuGLMUofM=
github.com/ncw/directio v1.0.5 h1:JSUBhdjEvVaJvOoyPAbcW0fnd0tvRXD76wEfZ1KcQz4=
github.com/ncw/directio v1.0.5/go.mod h1:rX/pKEYkOXBGOggmcyJeJGloCkleSvphPx2eV3t6ROk=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -32,6 +39,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
Expand All @@ -40,5 +48,7 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit f752915

Please sign in to comment.