mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-11-25 03:15:19 +08:00
* feat(cache): improve cache management * feat(disk-usage): add cache * feat(disk-usage): add refresh * fix(disk-usage): cache with ttl * feat(cache): implement KeyedCache and TypedCache for improved caching mechanism * fix(copy): update object retrieval to use Get instead of GetUnwrap * refactor(cache): simplify DirectoryCache structure and improve object management * fix(cache): correct cache entry initialization and key deletion logic in TypedCache * refactor(driver): remove GetObjInfo interface and simplify Link function logic https://github.com/OpenListTeam/OpenList/pull/888/files#r2430925783 * fix(link): optimize link retrieval and caching logic * refactor(cache): consolidate cache management and improve directory cache handling * fix(cache): add cache control based on storage configuration in List function * . * refactor: replace fmt.Sprintf with strconv for integer conversions * refactor(cache): enhance cache entry management with Expirable interface * fix(cache): improve link reference acquisition logic to handle expiration * refactor: replace OnlyLinkMFile with NoLinkSF in driver configurations and logic * refactor(link): enhance link caching logic with dynamic type keys based on IP and User-Agent * feat(drivers): add LinkCacheType to driver configurations for enhanced caching * refactor(cache): streamline directory object management in cache operations * refactor(cache): remove unnecessary 'dirty' field from CacheEntry structure * refactor(cache): replace 'dirty' field with bitwise flags * refactor(io): 调高SyncClosers.AcquireReference的优先级 * refactor(link): 优化链接获取逻辑,增加重 * refactor(link): 添加RequireReference字段以增强链接管理 * refactor(link): 移除MFile字段,改用RangeReader * refactor: 移除不必要的NoLinkSF字段 * refactor(cache): 修改目录缓存的脏标志定义和更新逻辑 * feat(cache): add expiration gc --------- Co-authored-by: KirCute <951206789@qq.com> Co-authored-by: KirCute <kircute@foxmail.com> Co-authored-by: j2rong4cn <j2rong@qq.com>
102 lines
1.9 KiB
Go
102 lines
1.9 KiB
Go
package cache
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type KeyedCache[T any] struct {
|
|
entries map[string]*CacheEntry[T]
|
|
mu sync.RWMutex
|
|
ttl time.Duration
|
|
}
|
|
|
|
func NewKeyedCache[T any](ttl time.Duration) *KeyedCache[T] {
|
|
c := &KeyedCache[T]{
|
|
entries: make(map[string]*CacheEntry[T]),
|
|
ttl: ttl,
|
|
}
|
|
gcFuncs = append(gcFuncs, c.GC)
|
|
return c
|
|
}
|
|
|
|
func (c *KeyedCache[T]) Set(key string, value T) {
|
|
c.SetWithExpirable(key, value, ExpirationTime(time.Now().Add(c.ttl)))
|
|
}
|
|
|
|
func (c *KeyedCache[T]) SetWithTTL(key string, value T, ttl time.Duration) {
|
|
c.SetWithExpirable(key, value, ExpirationTime(time.Now().Add(ttl)))
|
|
}
|
|
|
|
func (c *KeyedCache[T]) SetWithExpirable(key string, value T, exp Expirable) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
c.entries[key] = &CacheEntry[T]{
|
|
data: value,
|
|
Expirable: exp,
|
|
}
|
|
}
|
|
|
|
func (c *KeyedCache[T]) Get(key string) (T, bool) {
|
|
c.mu.RLock()
|
|
entry, exists := c.entries[key]
|
|
if !exists {
|
|
c.mu.RUnlock()
|
|
return *new(T), false
|
|
}
|
|
|
|
expired := entry.Expired()
|
|
c.mu.RUnlock()
|
|
|
|
if !expired {
|
|
return entry.data, true
|
|
}
|
|
|
|
c.mu.Lock()
|
|
if c.entries[key] == entry {
|
|
delete(c.entries, key)
|
|
c.mu.Unlock()
|
|
return *new(T), false
|
|
}
|
|
c.mu.Unlock()
|
|
return *new(T), false
|
|
}
|
|
|
|
func (c *KeyedCache[T]) Delete(key string) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
delete(c.entries, key)
|
|
}
|
|
|
|
func (c *KeyedCache[T]) Take(key string) (T, bool) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if entry, exists := c.entries[key]; exists {
|
|
delete(c.entries, key)
|
|
return entry.data, true
|
|
}
|
|
return *new(T), false
|
|
}
|
|
|
|
func (c *KeyedCache[T]) Clear() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.entries = make(map[string]*CacheEntry[T])
|
|
}
|
|
|
|
func (c *KeyedCache[T]) GC() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
expiredKeys := make([]string, 0, len(c.entries))
|
|
for key, entry := range c.entries {
|
|
if entry.Expired() {
|
|
expiredKeys = append(expiredKeys, key)
|
|
}
|
|
}
|
|
for _, key := range expiredKeys {
|
|
delete(c.entries, key)
|
|
}
|
|
}
|