Compare commits
No commits in common. 'master' and 'dev' have entirely different histories.
@ -1,9 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,18 @@
|
||||
package cache
|
||||
|
||||
type Cache interface {
|
||||
// Set 设置/添加一个缓存,如果 key 存在,用新值覆盖旧值
|
||||
Set(key string, value any)
|
||||
// Get 通过 key 获取一个缓存值
|
||||
Get(Key string) any
|
||||
// Del 通过 key 删除一个缓存值
|
||||
Del(Key string)
|
||||
// DelOldest 删除最“无用”的一个缓存值
|
||||
DelOldest()
|
||||
// Len 获取缓存已存在的记录数
|
||||
Len() int
|
||||
}
|
||||
|
||||
type Value interface {
|
||||
Len() int
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package fifo_test
|
||||
|
||||
import (
|
||||
"cache/fifo"
|
||||
"github.com/matryer/is"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestFifo
|
||||
//
|
||||
// @Description: 单元测试
|
||||
// @param t
|
||||
func TestFifo(t *testing.T) {
|
||||
is := is.New(t)
|
||||
cache := fifo.New(24, nil)
|
||||
cache.DelOldest()
|
||||
cache.Set("k1", 1)
|
||||
v := cache.Get("k1")
|
||||
is.Equal(v, 1)
|
||||
cache.Del("k1")
|
||||
is.Equal(0, cache.Len())
|
||||
}
|
||||
|
||||
// TestFifoWithCallBack
|
||||
//
|
||||
// @Description: 带删除回调
|
||||
// @param t
|
||||
func TestFifoWithCallBack(t *testing.T) {
|
||||
is := is.New(t)
|
||||
keys := make([]string, 8)
|
||||
onEvicted := func(key string, value any) {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
cache := fifo.New(24, onEvicted)
|
||||
cache.Set("k1", int8(1))
|
||||
cache.Set("k2", int8(2))
|
||||
cache.Set("k3", int8(3))
|
||||
cache.Set("k4", int8(4))
|
||||
expected := []string{"k1", "k2"}
|
||||
is.Equal(expected, keys)
|
||||
is.Equal(2, cache.Len())
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
module cache
|
||||
|
||||
go 1.21.5
|
||||
|
||||
require github.com/matryer/is v1.4.1
|
@ -0,0 +1,2 @@
|
||||
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
|
||||
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
|
@ -0,0 +1,41 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func CalcLen(value any) int {
|
||||
var n int
|
||||
switch v := value.(type) {
|
||||
case Value:
|
||||
n = v.Len()
|
||||
case string:
|
||||
if runtime.GOARCH == "amd64" {
|
||||
n = 16 + len(v)
|
||||
} else {
|
||||
n = 8 + len(v)
|
||||
}
|
||||
case bool, uint8, int8:
|
||||
n = 1
|
||||
case int16, uint16:
|
||||
n = 2
|
||||
case int32, uint32, float32:
|
||||
n = 4
|
||||
case int64, uint64, float64:
|
||||
n = 8
|
||||
case int, uint:
|
||||
if runtime.GOARCH == "amd64" {
|
||||
n = 8
|
||||
} else {
|
||||
n = 4
|
||||
}
|
||||
case complex64:
|
||||
n = 8
|
||||
case complex128:
|
||||
n = 16
|
||||
default:
|
||||
panic(fmt.Sprintf("%T is not implement cache.Value", value))
|
||||
}
|
||||
return n
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package lfu_test
|
||||
|
||||
import (
|
||||
"cache/lfu"
|
||||
"github.com/matryer/is"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
is := is.New(t)
|
||||
cache := lfu.New(24, nil)
|
||||
cache.DelOldest()
|
||||
cache.Set("k1", 1)
|
||||
v := cache.Get("k1")
|
||||
is.Equal(v, 1)
|
||||
cache.Del("k1")
|
||||
is.Equal(0, cache.Len())
|
||||
}
|
||||
|
||||
func TestOnEvicted(t *testing.T) {
|
||||
is := is.New(t)
|
||||
keys := make([]string, 0, 8)
|
||||
onEvicted := func(key string, value interface{}) {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
cache := lfu.New(32, onEvicted)
|
||||
cache.Set("k1", 1)
|
||||
cache.Set("k2", 2)
|
||||
cache.Set("k3", 3)
|
||||
cache.Set("k4", 4)
|
||||
expected := []string{"k1", "k3"}
|
||||
is.Equal(expected, keys)
|
||||
is.Equal(2, cache.Len())
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package lfu
|
||||
|
||||
import (
|
||||
"cache"
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
type entry struct {
|
||||
key string
|
||||
value any
|
||||
//权重或优先级
|
||||
weight int
|
||||
//下标
|
||||
index int
|
||||
}
|
||||
|
||||
func (e *entry) Len() int {
|
||||
return cache.CalcLen(e.value) + 8
|
||||
}
|
||||
|
||||
type queue []*entry
|
||||
|
||||
// Len
|
||||
//
|
||||
// @Description: 长度
|
||||
// @receiver e
|
||||
// @return int
|
||||
func (q queue) Len() int {
|
||||
return len(q)
|
||||
}
|
||||
|
||||
func (q queue) Less(i, j int) bool {
|
||||
return q[i].weight < q[j].weight
|
||||
}
|
||||
|
||||
func (q queue) Swap(i, j int) {
|
||||
q[i], q[j] = q[j], q[i]
|
||||
q[i].index = i
|
||||
q[j].index = j
|
||||
}
|
||||
|
||||
func (q *queue) Push(x any) {
|
||||
n := len(*q)
|
||||
en := x.(*entry)
|
||||
en.index = n
|
||||
*q = append(*q, en)
|
||||
}
|
||||
|
||||
func (q *queue) Pop() any {
|
||||
old := *q
|
||||
n := len(old)
|
||||
en := old[n-1]
|
||||
en.index = -1
|
||||
*q = old[0 : n-1]
|
||||
return en
|
||||
}
|
||||
|
||||
func (q *queue) update(en *entry, value any, weight int) {
|
||||
en.value = value
|
||||
en.weight = weight
|
||||
heap.Fix(q, en.index)
|
||||
}
|
Loading…
Reference in new issue