GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - charizer/lrucache: golang实现线程安全的可设置过期时间的lru缓存

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
README.md
README.md
 
 
lrucache.go
lrucache.go
 
 
lrucache_test.go
lrucache_test.go
 
 

Repository files navigation

lrucache

功能:golang实现的包含过期时间线程安全的lru缓存,具有以下特点:

1、新数据插入到list头部,每当缓存命中时,则将数据移动到头部
2、当数据达到最大长度时,则自动淘汰list尾部数据
3、数据可以设置过期时间,获取数据时,如果数据已经过期,则自动删除数据,并返回数据不命中
4、可以设置淘汰数据时的回调函数

基本数据结构:

type LruCache struct {
    //缓存容量
	size      int
	//缓存list节点
	evictList *list.List
	//缓存元素
	cache     map[interface{}]*list.Element
	//元素过期时间
	ttl       time.Duration
	//淘汰元素回调函数
	onEvict   EvictCallback
	//读写锁
	lock      sync.RWMutex
}

使用示例:

//过期时间
const (
	Expired = 60 * time.Second
)
//数据淘汰时的回调函数
onEvicted := func(k interface{}, v interface{}) {
		
}
//创建lrucache
l, err := NewLRUCache(16, Expired, onEvicted)
if err != nil {
	log.Errorf("err: %v", err)
	return
}
//存放32个数据
for i := 0; i < 32; i++ {
	l.Put(i, i, Expired)
}
//长度为16,0—15已经被淘汰
log.Infof("len:%d",l.Len())
l.Get(2)     //没有此元素,已经被淘汰
l.Get(30)    //有此元素

About

golang实现线程安全的可设置过期时间的lru缓存

Resources

Readme
Activity

Stars

1 star

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.