在go中实现一个常用的先进先出的缓存淘汰算法示例代码
Go  /  管理员 发布于 6小时前   4
在go中实现一个常用先进先出的缓存淘汰算法示例代码
在go + gin框架中开发缓存功能, 实现算法:缓存淘汰算法 / 先进先出算法;
代码示例
缓存工具文件D:\Go\zongscan-go\utils\fifo.go
package utils
//缓存工具
//缓存淘汰算法 / 先进先出算法
import (
"container/list"
"fmt"
"runtime"
"unsafe"
)
type Cashe interface {
Set(key string, value interface{})
Get(key string) interface{}
Del(key string)
DelOldest()
Len() int
UseBytes() int
}
type Value interface {
Len() int
}
type fifo struct {
// 缓存最大容量
maxBytes int
// 已使用的字节数,只包含值,key不算
usedBytes int
// 双链表
ll *list.List
// values是双链表对应的指针
cache map[string]*list.Element
}
type entry struct {
key string
value interface{}
}
func (e *entry) Len() int {
return CalcLen(e.value)
}
// 计算value占用的内存大小
func CalcLen(value interface{}) (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, int8, uint8:
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
case interface{}:
i := unsafe.Sizeof(value)
n = int(i)
default:
panic(fmt.Sprintf("%T is not implement cache.value", value))
}
return n
}
func NewFifoCache(maxBytes int) Cashe {
return &fifo{
maxBytes: maxBytes,
ll: list.New(),
cache: make(map[string]*list.Element),
}
}
// 增加一个元素(如果存在,则移到头部,并修改值)
func (f *fifo) Set(key string, value interface{}) {
if element, ok := f.cache[key]; ok {
f.ll.MoveToFront(element)
eVal := element.Value.(*entry)
f.usedBytes = f.usedBytes - CalcLen(eVal.value) + CalcLen(value)
element.Value = &entry{key, value}
} else {
element := &entry{key, value}
e := f.ll.PushFront(element)
f.cache[key] = e
//f.usedBytes += element.Len()
f.usedBytes += element.Len()
}
for f.maxBytes > 0 && f.maxBytes < f.usedBytes {
f.DelOldest()
}
}
// 获取指定元素
func (f *fifo) Get(key string) interface{} {
if e, ok := f.cache[key]; ok {
return e.Value.(*entry).value
}
return nil
}
// 删除指定元素
func (f *fifo) Del(key string) {
if e, ok := f.cache[key]; ok {
f.removeElement(e)
}
}
func (f *fifo) DelOldest() {
f.removeElement(f.ll.Back())
}
// 删除元素并更新内存占用大小
func (f *fifo) removeElement(e *list.Element) {
if e == nil {
return
}
f.ll.Remove(e)
en := e.Value.(*entry)
f.usedBytes -= en.Len()
delete(f.cache, en.key)
}
// 缓存中元素个数
func (f *fifo) Len() int {
return f.ll.Len()
}
// 缓存占用内存大小
func (f *fifo) UseBytes() int {
return f.usedBytes
}
调用使用
package v2
import (
"fmt"
"zongscan-go/utils"
"github.com/gin-gonic/gin"
)
func GetUserDetail(c *gin.Context) {
...
cache := utils.NewFifoCache(1024) // 创建最大1KB的缓存
fmt.Println(cache)
cache.Set("name", "https://www.zongscan.com") // 添加键值对
val := cache.Get("name") // 获取值
if val != nil {
fmt.Println(val.(string)) // 类型断言
}
fmt.Println(val)
...
}
效果:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号