Skip to content

Commit

Permalink
feat<priority_cache> 对于set行为更新priority
Browse files Browse the repository at this point in the history
  • Loading branch information
xkond authored and xkond committed Jul 19, 2024
1 parent 5c4ce69 commit 5827dc8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion memory/priority/rbtree_priority_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *RBTreePriorityCache) Set(_ context.Context, key string, val any, expira
node := r.findOrCreateNode(key, func() any { return val })

node.replace(val, expiration)
r.tryUpdateNodePriority(node)
return nil
}

Expand All @@ -115,6 +116,14 @@ func (r *RBTreePriorityCache) deleteNode(node *rbTreeCacheNode) {
r.deleteNodeFromPriority(node)
}

// tryUpdateNodePriority 更新缓存节点的优先级【调用该方法必须先获得锁】
func (r *RBTreePriorityCache) tryUpdateNodePriority(node *rbTreeCacheNode) {
if node.priority != r.calculatePriority(node) {
r.deleteNodeFromPriority(node)
r.addNodeToPriority(node)
}
}

func (r *RBTreePriorityCache) SetNX(ctx context.Context, key string, val any, expiration time.Duration) (bool, error) {
r.globalLock.Lock()
defer r.globalLock.Unlock()
Expand All @@ -129,7 +138,7 @@ func (r *RBTreePriorityCache) SetNX(ctx context.Context, key string, val any, ex

if !node.beforeDeadline(time.Now()) {
node.replace(val, expiration) //过期的,key一样,直接覆盖

r.tryUpdateNodePriority(node)
return true, nil
}

Expand Down Expand Up @@ -193,6 +202,7 @@ func (r *RBTreePriorityCache) GetSet(ctx context.Context, key string, val string
//这里不需要判断缓存过期没有,取出旧值放入新值就完事了
retVal.Val = node.value
node.value = val
r.tryUpdateNodePriority(node)

return retVal
}
Expand Down Expand Up @@ -313,6 +323,7 @@ func (r *RBTreePriorityCache) IncrBy(ctx context.Context, key string, value int6

newVal := nodeVal + value
node.value = newVal
r.tryUpdateNodePriority(node)

return newVal, nil
}
Expand All @@ -334,6 +345,7 @@ func (r *RBTreePriorityCache) IncrByFloat(ctx context.Context, key string, value

newVal := nodeVal + value
node.value = newVal
r.tryUpdateNodePriority(node)

return newVal, nil
}
Expand Down Expand Up @@ -383,6 +395,7 @@ func (r *RBTreePriorityCache) DecrBy(ctx context.Context, key string, value int6

newVal := nodeVal - value
node.value = newVal
r.tryUpdateNodePriority(node)

return newVal, nil
}
Expand Down
30 changes: 30 additions & 0 deletions memory/priority/rbtree_priority_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,36 @@ func TestRBTreePriorityCache_Set(t *testing.T) {
return cache
},
},
{
name: "limit 2,cache 2, update 1 to big priority, add 1,evict small node",
startCache: func() *RBTreePriorityCache {
cache, _ := NewRBTreePriorityCache(WithCacheLimit(2))
cache.globalLock.Lock()
defer cache.globalLock.Unlock()
cache.addNode(newKVRBTreeCacheNode("key1", testStructForPriority{priority: 1}, 0))
cache.addNode(newKVRBTreeCacheNode("key3", testStructForPriority{priority: 3}, 0))
node1 := cache.findOrCreateNode("key1", func() any { return nil })

//更新key1优先级
value, ok := node1.value.(testStructForPriority)
require.True(t, ok)
value.priority = 4
node1.value = value
cache.tryUpdateNodePriority(node1)

return cache
},
key: "key2",
value: testStructForPriority{priority: 2},
wantCache: func() *RBTreePriorityCache {
cache, _ := NewRBTreePriorityCache(WithCacheLimit(2))
cache.globalLock.Lock()
defer cache.globalLock.Unlock()
cache.addNode(newKVRBTreeCacheNode("key2", testStructForPriority{priority: 2}, 0))
cache.addNode(newKVRBTreeCacheNode("key1", testStructForPriority{priority: 4}, 0))
return cache
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 5827dc8

Please sign in to comment.