refl: add hono Context
This commit is contained in:
+119
-46
@@ -1,65 +1,138 @@
|
||||
module hono
|
||||
|
||||
// 简化的LRU缓存实现
|
||||
pub struct LRUCache {
|
||||
mut:
|
||||
capacity int
|
||||
cache map[string]RouteMatch
|
||||
keys []string // 维护访问顺序
|
||||
// Context LRU 缓存节点
|
||||
@[heap]
|
||||
struct ContextLRUCacheNode {
|
||||
pub mut:
|
||||
key string
|
||||
value ContextRouteMatch
|
||||
prev &ContextLRUCacheNode = unsafe { nil }
|
||||
next &ContextLRUCacheNode = unsafe { nil }
|
||||
}
|
||||
|
||||
// Context LRU 缓存
|
||||
pub struct ContextLRUCache {
|
||||
mut:
|
||||
capacity int
|
||||
size int
|
||||
cache map[string]&ContextLRUCacheNode
|
||||
head &ContextLRUCacheNode = unsafe { nil }
|
||||
tail &ContextLRUCacheNode = unsafe { nil }
|
||||
}
|
||||
|
||||
|
||||
pub fn new_lru_cache(capacity int) LRUCache {
|
||||
return LRUCache{
|
||||
// ContextLRUCache 构造函数
|
||||
pub fn ContextLRUCache.new(capacity int) ContextLRUCache {
|
||||
return ContextLRUCache{
|
||||
capacity: capacity
|
||||
cache: map[string]RouteMatch{}
|
||||
keys: []string{}
|
||||
size: 0
|
||||
cache: map[string]&ContextLRUCacheNode{}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut cache LRUCache) get(key string) ?RouteMatch {
|
||||
if key in cache.cache {
|
||||
// 移动到列表末尾(最近使用)
|
||||
idx := cache.keys.index(key)
|
||||
if idx != -1 {
|
||||
cache.keys.delete(idx)
|
||||
}
|
||||
cache.keys << key
|
||||
return cache.cache[key]
|
||||
// 获取缓存值
|
||||
pub fn (mut cache ContextLRUCache) get(key string) ?ContextRouteMatch {
|
||||
if mut node := cache.cache[key] {
|
||||
cache.move_to_front(mut node)
|
||||
return node.value
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
pub fn (mut cache LRUCache) put(key string, value RouteMatch) {
|
||||
if key in cache.cache {
|
||||
// 更新现有项
|
||||
cache.cache[key] = value
|
||||
// 移动到列表末尾
|
||||
idx := cache.keys.index(key)
|
||||
if idx != -1 {
|
||||
cache.keys.delete(idx)
|
||||
}
|
||||
cache.keys << key
|
||||
} else {
|
||||
// 添加新项
|
||||
cache.cache[key] = value
|
||||
cache.keys << key
|
||||
|
||||
// 如果超出容量,删除最久未使用的项
|
||||
if cache.cache.len > cache.capacity {
|
||||
oldest_key := cache.keys[0]
|
||||
cache.cache.delete(oldest_key)
|
||||
cache.keys.delete(0)
|
||||
}
|
||||
// 设置缓存值
|
||||
pub fn (mut cache ContextLRUCache) put(key string, value ContextRouteMatch) {
|
||||
if mut node := cache.cache[key] {
|
||||
node.value = value
|
||||
cache.move_to_front(mut node)
|
||||
return
|
||||
}
|
||||
|
||||
if cache.size >= cache.capacity {
|
||||
cache.remove_tail()
|
||||
}
|
||||
|
||||
mut new_node := &ContextLRUCacheNode{
|
||||
key: key
|
||||
value: value
|
||||
}
|
||||
|
||||
cache.cache[key] = new_node
|
||||
cache.add_to_front(mut new_node)
|
||||
cache.size++
|
||||
}
|
||||
|
||||
// 移动到链表头部
|
||||
fn (mut cache ContextLRUCache) move_to_front(mut node ContextLRUCacheNode) {
|
||||
if node == cache.head {
|
||||
return
|
||||
}
|
||||
|
||||
// 从当前位置移除
|
||||
if node.prev != unsafe { nil } {
|
||||
mut prev := node.prev
|
||||
prev.next = node.next
|
||||
}
|
||||
if node.next != unsafe { nil } {
|
||||
mut next := node.next
|
||||
next.prev = node.prev
|
||||
}
|
||||
|
||||
// 如果是尾节点
|
||||
if node == cache.tail {
|
||||
cache.tail = node.prev
|
||||
}
|
||||
|
||||
cache.add_to_front(mut node)
|
||||
}
|
||||
|
||||
// 添加到链表头部
|
||||
fn (mut cache ContextLRUCache) add_to_front(mut node ContextLRUCacheNode) {
|
||||
node.next = cache.head
|
||||
node.prev = unsafe { nil }
|
||||
|
||||
if cache.head != unsafe { nil } {
|
||||
mut head := cache.head
|
||||
head.prev = node
|
||||
}
|
||||
|
||||
cache.head = node
|
||||
|
||||
if cache.tail == unsafe { nil } {
|
||||
cache.tail = node
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut cache LRUCache) clear() {
|
||||
cache.cache.clear()
|
||||
cache.keys.clear()
|
||||
// 移除链表尾部
|
||||
fn (mut cache ContextLRUCache) remove_tail() {
|
||||
if cache.tail == unsafe { nil } {
|
||||
return
|
||||
}
|
||||
|
||||
key := cache.tail.key
|
||||
cache.cache.delete(key)
|
||||
|
||||
if cache.head == cache.tail {
|
||||
cache.head = unsafe { nil }
|
||||
cache.tail = unsafe { nil }
|
||||
} else {
|
||||
cache.tail = cache.tail.prev
|
||||
if cache.tail != unsafe { nil } {
|
||||
mut tail := cache.tail
|
||||
tail.next = unsafe { nil }
|
||||
}
|
||||
}
|
||||
|
||||
cache.size--
|
||||
}
|
||||
|
||||
pub fn (cache LRUCache) size() int {
|
||||
return cache.cache.len
|
||||
// 获取缓存统计信息
|
||||
pub fn (cache ContextLRUCache) get_stats() (int, int) {
|
||||
return cache.size, cache.capacity
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
pub fn (mut cache ContextLRUCache) clear() {
|
||||
cache.cache.clear()
|
||||
cache.head = unsafe { nil }
|
||||
cache.tail = unsafe { nil }
|
||||
cache.size = 0
|
||||
}
|
||||
Reference in New Issue
Block a user