node build fixed
This commit is contained in:
199
seanime-2.9.10/internal/util/result/boundedcache.go
Normal file
199
seanime-2.9.10/internal/util/result/boundedcache.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BoundedCache implements an LRU cache with a maximum capacity
|
||||
type BoundedCache[K comparable, V any] struct {
|
||||
mu sync.RWMutex
|
||||
capacity int
|
||||
items map[K]*list.Element
|
||||
order *list.List
|
||||
}
|
||||
|
||||
type boundedCacheItem[K comparable, V any] struct {
|
||||
key K
|
||||
value V
|
||||
expiration time.Time
|
||||
}
|
||||
|
||||
// NewBoundedCache creates a new bounded cache with the specified capacity
|
||||
func NewBoundedCache[K comparable, V any](capacity int) *BoundedCache[K, V] {
|
||||
return &BoundedCache[K, V]{
|
||||
capacity: capacity,
|
||||
items: make(map[K]*list.Element),
|
||||
order: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// Set adds or updates an item in the cache with a default TTL
|
||||
func (c *BoundedCache[K, V]) Set(key K, value V) {
|
||||
c.SetT(key, value, time.Hour) // Default TTL of 1 hour
|
||||
}
|
||||
|
||||
// SetT adds or updates an item in the cache with a specific TTL
|
||||
func (c *BoundedCache[K, V]) SetT(key K, value V, ttl time.Duration) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
expiration := time.Now().Add(ttl)
|
||||
item := &boundedCacheItem[K, V]{
|
||||
key: key,
|
||||
value: value,
|
||||
expiration: expiration,
|
||||
}
|
||||
|
||||
// If key already exists, update it and move to front
|
||||
if elem, exists := c.items[key]; exists {
|
||||
elem.Value = item
|
||||
c.order.MoveToFront(elem)
|
||||
return
|
||||
}
|
||||
|
||||
// If at capacity, remove oldest item
|
||||
if len(c.items) >= c.capacity {
|
||||
c.evictOldest()
|
||||
}
|
||||
|
||||
// Add new item to front
|
||||
elem := c.order.PushFront(item)
|
||||
c.items[key] = elem
|
||||
|
||||
// Set up expiration cleanup
|
||||
go func() {
|
||||
<-time.After(ttl)
|
||||
c.Delete(key)
|
||||
}()
|
||||
}
|
||||
|
||||
// Get retrieves an item from the cache and marks it as recently used
|
||||
func (c *BoundedCache[K, V]) Get(key K) (V, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
var zero V
|
||||
elem, exists := c.items[key]
|
||||
if !exists {
|
||||
return zero, false
|
||||
}
|
||||
|
||||
item := elem.Value.(*boundedCacheItem[K, V])
|
||||
|
||||
// Check if expired
|
||||
if time.Now().After(item.expiration) {
|
||||
c.delete(key)
|
||||
return zero, false
|
||||
}
|
||||
|
||||
// Move to front (mark as recently used)
|
||||
c.order.MoveToFront(elem)
|
||||
return item.value, true
|
||||
}
|
||||
|
||||
// Has checks if a key exists in the cache without updating access time
|
||||
func (c *BoundedCache[K, V]) Has(key K) bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
elem, exists := c.items[key]
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
|
||||
item := elem.Value.(*boundedCacheItem[K, V])
|
||||
if time.Now().After(item.expiration) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Delete removes an item from the cache
|
||||
func (c *BoundedCache[K, V]) Delete(key K) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.delete(key)
|
||||
}
|
||||
|
||||
// delete removes an item from the cache (internal, assumes lock is held)
|
||||
func (c *BoundedCache[K, V]) delete(key K) {
|
||||
if elem, exists := c.items[key]; exists {
|
||||
c.order.Remove(elem)
|
||||
delete(c.items, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Clear removes all items from the cache
|
||||
func (c *BoundedCache[K, V]) Clear() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.items = make(map[K]*list.Element)
|
||||
c.order.Init()
|
||||
}
|
||||
|
||||
// GetOrSet retrieves an item or creates it if it doesn't exist
|
||||
func (c *BoundedCache[K, V]) GetOrSet(key K, createFunc func() (V, error)) (V, error) {
|
||||
// Try to get the value first
|
||||
value, ok := c.Get(key)
|
||||
if ok {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// Create new value
|
||||
newValue, err := createFunc()
|
||||
if err != nil {
|
||||
return newValue, err
|
||||
}
|
||||
|
||||
// Set the new value
|
||||
c.Set(key, newValue)
|
||||
return newValue, nil
|
||||
}
|
||||
|
||||
// Size returns the current number of items in the cache
|
||||
func (c *BoundedCache[K, V]) Size() int {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return len(c.items)
|
||||
}
|
||||
|
||||
// Capacity returns the maximum capacity of the cache
|
||||
func (c *BoundedCache[K, V]) Capacity() int {
|
||||
return c.capacity
|
||||
}
|
||||
|
||||
// evictOldest removes the least recently used item (assumes lock is held)
|
||||
func (c *BoundedCache[K, V]) evictOldest() {
|
||||
if c.order.Len() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
elem := c.order.Back()
|
||||
if elem != nil {
|
||||
item := elem.Value.(*boundedCacheItem[K, V])
|
||||
c.delete(item.key)
|
||||
}
|
||||
}
|
||||
|
||||
// Range iterates over all items in the cache
|
||||
func (c *BoundedCache[K, V]) Range(callback func(key K, value V) bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
for elem := c.order.Front(); elem != nil; elem = elem.Next() {
|
||||
item := elem.Value.(*boundedCacheItem[K, V])
|
||||
|
||||
// Skip expired items
|
||||
if time.Now().After(item.expiration) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !callback(item.key, item.value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
101
seanime-2.9.10/internal/util/result/resultcache.go
Normal file
101
seanime-2.9.10/internal/util/result/resultcache.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"seanime/internal/constants"
|
||||
"seanime/internal/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cache[K interface{}, V any] struct {
|
||||
store util.RWMutexMap
|
||||
}
|
||||
|
||||
type cacheItem[K interface{}, V any] struct {
|
||||
value V
|
||||
expiration time.Time
|
||||
}
|
||||
|
||||
func NewCache[K interface{}, V any]() *Cache[K, V] {
|
||||
return &Cache[K, V]{}
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Set(key K, value V) {
|
||||
ttl := constants.GcTime
|
||||
c.store.Store(key, &cacheItem[K, V]{value, time.Now().Add(ttl)})
|
||||
go func() {
|
||||
<-time.After(ttl)
|
||||
c.Delete(key)
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) SetT(key K, value V, ttl time.Duration) {
|
||||
c.store.Store(key, &cacheItem[K, V]{value, time.Now().Add(ttl)})
|
||||
go func() {
|
||||
<-time.After(ttl)
|
||||
c.Delete(key)
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Get(key K) (V, bool) {
|
||||
item, ok := c.store.Load(key)
|
||||
if !ok {
|
||||
return (&cacheItem[K, V]{}).value, false
|
||||
}
|
||||
ci := item.(*cacheItem[K, V])
|
||||
if time.Now().After(ci.expiration) {
|
||||
c.Delete(key)
|
||||
return (&cacheItem[K, V]{}).value, false
|
||||
}
|
||||
return ci.value, true
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Pop() (K, V, bool) {
|
||||
var key K
|
||||
var value V
|
||||
var ok bool
|
||||
c.store.Range(func(k, v interface{}) bool {
|
||||
key = k.(K)
|
||||
value = v.(*cacheItem[K, V]).value
|
||||
ok = true
|
||||
c.store.Delete(k)
|
||||
return false
|
||||
})
|
||||
return key, value, ok
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Has(key K) bool {
|
||||
_, ok := c.store.Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) GetOrSet(key K, createFunc func() (V, error)) (V, error) {
|
||||
value, ok := c.Get(key)
|
||||
if ok {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
newValue, err := createFunc()
|
||||
if err != nil {
|
||||
return newValue, err
|
||||
}
|
||||
c.Set(key, newValue)
|
||||
return newValue, nil
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Delete(key K) {
|
||||
c.store.Delete(key)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Clear() {
|
||||
c.store.Range(func(key interface{}, value interface{}) bool {
|
||||
c.store.Delete(key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Range(callback func(key K, value V) bool) {
|
||||
c.store.Range(func(key, value interface{}) bool {
|
||||
ci := value.(*cacheItem[K, V])
|
||||
return callback(key.(K), ci.value)
|
||||
})
|
||||
}
|
||||
97
seanime-2.9.10/internal/util/result/resultmap.go
Normal file
97
seanime-2.9.10/internal/util/result/resultmap.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"seanime/internal/util"
|
||||
)
|
||||
|
||||
type Map[K interface{}, V any] struct {
|
||||
store util.RWMutexMap
|
||||
}
|
||||
|
||||
type mapItem[K interface{}, V any] struct {
|
||||
value V
|
||||
}
|
||||
|
||||
func NewResultMap[K interface{}, V any]() *Map[K, V] {
|
||||
return &Map[K, V]{}
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Set(key K, value V) {
|
||||
c.store.Store(key, &mapItem[K, V]{value})
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Get(key K) (V, bool) {
|
||||
item, ok := c.store.Load(key)
|
||||
if !ok {
|
||||
return (&mapItem[K, V]{}).value, false
|
||||
}
|
||||
ci := item.(*mapItem[K, V])
|
||||
return ci.value, true
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Has(key K) bool {
|
||||
_, ok := c.store.Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) GetOrSet(key K, createFunc func() (V, error)) (V, error) {
|
||||
value, ok := c.Get(key)
|
||||
if ok {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
newValue, err := createFunc()
|
||||
if err != nil {
|
||||
return newValue, err
|
||||
}
|
||||
c.Set(key, newValue)
|
||||
return newValue, nil
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Delete(key K) {
|
||||
c.store.Delete(key)
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Clear() {
|
||||
c.store.Range(func(key interface{}, value interface{}) bool {
|
||||
c.store.Delete(key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
// ClearN clears the map and returns the number of items cleared
|
||||
func (c *Map[K, V]) ClearN() int {
|
||||
count := 0
|
||||
c.store.Range(func(key interface{}, value interface{}) bool {
|
||||
c.store.Delete(key)
|
||||
count++
|
||||
return true
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Range(callback func(key K, value V) bool) {
|
||||
c.store.Range(func(key, value interface{}) bool {
|
||||
ci := value.(*mapItem[K, V])
|
||||
return callback(key.(K), ci.value)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Values() []V {
|
||||
values := make([]V, 0)
|
||||
c.store.Range(func(key, value interface{}) bool {
|
||||
item := value.(*mapItem[K, V]) // Correct type assertion
|
||||
values = append(values, item.value)
|
||||
return true
|
||||
})
|
||||
return values
|
||||
}
|
||||
|
||||
func (c *Map[K, V]) Keys() []K {
|
||||
keys := make([]K, 0)
|
||||
c.store.Range(func(key, value interface{}) bool {
|
||||
keys = append(keys, key.(K))
|
||||
return true
|
||||
})
|
||||
return keys
|
||||
}
|
||||
Reference in New Issue
Block a user