Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature add lru cache evict #30

Merged
merged 16 commits into from
Jan 11, 2024
Merged

feature add lru cache evict #30

merged 16 commits into from
Jan 11, 2024

Conversation

Stone-afk
Copy link
Collaborator

No description provided.

Copy link

codecov bot commented Dec 23, 2023

Codecov Report

Attention: 13 lines in your changes are missing coverage. Please review.

Comparison is base (8c6eedc) 97.94% compared to head (799b8a2) 96.92%.
Report is 1 commits behind head on main.

Files Patch % Lines
memory/lru/cache.go 90.15% 11 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #30      +/-   ##
==========================================
- Coverage   97.94%   96.92%   -1.02%     
==========================================
  Files           4        7       +3     
  Lines         632      845     +213     
==========================================
+ Hits          619      819     +200     
- Misses          9       20      +11     
- Partials        4        6       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要做这种抽象,因为之前在别的实现里面,已经有了一个优先级的淘汰的实现了。

这个抽象的意义不是很大,核心就在于 EvictStrategy 太多方法了,没有谁愿意提供新的实现。

所以直接整合为一个 lru cache 就可以。

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接整个实现就叫做 LRU,然后就只支持 LRU。LRU 和别的不太容易混合在一起,因为 LRU 需要考虑根据更新访问时间来调整整个结构。

cacheevict/lru/lru.go Outdated Show resolved Hide resolved
cacheevict/lru/lru.go Outdated Show resolved Hide resolved
cacheevict/lru/list.go Outdated Show resolved Hide resolved
cacheevict/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/lru.go Outdated Show resolved Hide resolved
memory/lru/lru.go Outdated Show resolved Hide resolved
memory/lru/lru.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/list.go Show resolved Hide resolved
memory/lru/list.go Show resolved Hide resolved
memory/lru/list.go Outdated Show resolved Hide resolved
memory/lru/cache_test.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Show resolved Hide resolved
memory/lru/list.go Outdated Show resolved Hide resolved
memory/lru/list.go Outdated Show resolved Hide resolved
@Stone-afk
Copy link
Collaborator Author

其次,通过在 element 中维护 list,能够依次遍历链表,使用 pushBackList 在链表尾部添加元素或pushFrontList头部添加元素,这样的设计使得链表的操作更加方便和高效。

func (e *element[T]) nextElem() *element[T] {
	if n := e.next; e.list != nil && n != &e.list.root {
		return n
	}
	return nil
}

func (l *linkedList[T]) pushBackList(other *linkedList[T]) {
	l.lazyInit()
	e := other.front()
	for i := other.len(); i > 0; i-- {
		l.insertValue(e.Value, l.root.prev)
		e = e.nextElem()
	}
}

@flycash
Copy link
Contributor

flycash commented Dec 29, 2023

其次,通过在 element 中维护 list,能够依次遍历链表,使用 pushBackList 在链表尾部添加元素或pushFrontList头部添加元素,这样的设计使得链表的操作更加方便和高效。

func (e *element[T]) nextElem() *element[T] {
	if n := e.next; e.list != nil && n != &e.list.root {
		return n
	}
	return nil
}

func (l *linkedList[T]) pushBackList(other *linkedList[T]) {
	l.lazyInit()
	e := other.front()
	for i := other.len(); i > 0; i-- {
		l.insertValue(e.Value, l.root.prev)
		e = e.nextElem()
	}
}

这个也没必要,因为正常我们 list 操作,都是在对应的 Add 或者 Remove 里面,沿着 Element 追溯或者回溯。

所以这里不要反向依赖 list。

memory/lru/cache.go Outdated Show resolved Hide resolved
memory/lru/cache.go Show resolved Hide resolved
memory/lru/cache.go Show resolved Hide resolved
memory/lru/cache_test.go Show resolved Hide resolved
memory/lru/list.go Show resolved Hide resolved
@flycash
Copy link
Contributor

flycash commented Jan 7, 2024

我草,GO build 都失败了。有 Data race

@flycash
Copy link
Contributor

flycash commented Jan 7, 2024

其余你也要修复了。

@flycash flycash merged commit 7567983 into ecodeclub:main Jan 11, 2024
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants