8
"github.com/cubefs/cubefs/sdk/meta"
12
MinSummaryCacheEvictNum = 10
13
MaxSummaryCacheEvictNum = 200000
14
SummaryBgEvictionInterval = 2 * time.Minute
15
DefaultSummaryExpiration = 2 * time.Minute
16
MaxSummaryCache = 1000000
19
// SummaryCache defines the structure of the content-summary cache.
20
type SummaryCache struct {
22
cache map[uint64]*list.Element
24
expiration time.Duration
28
// summaryCacheElement defines the structure of the content-summary cache's element.
29
type summaryCacheElement struct {
31
info *meta.SummaryInfo
35
// NewSummaryCache returns a new content-summary cache.
36
func NewSummaryCache(exp time.Duration, maxElement int) *SummaryCache {
38
cache: make(map[uint64]*list.Element),
41
maxElements: maxElement,
43
go sc.backgroundEviction()
47
// Put puts the given summary info into the content-summary cache.
48
func (sc *SummaryCache) Put(inode uint64, summaryInfo *meta.SummaryInfo) {
50
old, ok := sc.cache[inode]
52
sc.lruList.Remove(old)
53
delete(sc.cache, inode)
55
if sc.lruList.Len() >= sc.maxElements {
58
element := sc.lruList.PushFront(&summaryCacheElement{
61
expiration: time.Now().Add(sc.expiration).UnixNano(),
63
sc.cache[inode] = element
67
// Get returns the content-summary info based on the given inode number.
68
func (sc *SummaryCache) Get(inode uint64) *meta.SummaryInfo {
70
element, ok := sc.cache[inode]
75
info := element.Value.(*summaryCacheElement)
76
if cacheExpired(info) {
84
// Delete deletes the content-summary info based on the given inode number.
85
func (sc *SummaryCache) Delete(inode uint64) {
87
element, ok := sc.cache[inode]
89
sc.lruList.Remove(element)
90
delete(sc.cache, inode)
95
func (sc *SummaryCache) evict(foreground bool) {
96
for i := 0; i < MinSummaryCacheEvictNum; i++ {
97
element := sc.lruList.Back()
101
info := element.Value.(*summaryCacheElement)
102
if !foreground && !cacheExpired(info) {
105
sc.lruList.Remove(element)
106
delete(sc.cache, info.ino)
112
for i := 0; i < MaxSummaryCacheEvictNum; i++ {
113
element := sc.lruList.Back()
117
info := element.Value.(*summaryCacheElement)
118
if !cacheExpired(info) {
121
sc.lruList.Remove(element)
122
delete(sc.cache, info.ino)
126
func (sc *SummaryCache) backgroundEviction() {
127
t := time.NewTicker(SummaryBgEvictionInterval)
139
func cacheExpired(info *summaryCacheElement) bool {
140
if time.Now().UnixNano() > info.expiration {