feat: update dependencies and improve caching mechanism
Some checks failed
PR Check / check-and-test (pull_request) Failing after 2m11s
Some checks failed
PR Check / check-and-test (pull_request) Failing after 2m11s
- Added Prometheus client library for metrics collection. - Refactored garbage collection strategy from random deletion to LRU (Least Recently Used) deletion. - Introduced per-key locking in cache to prevent race conditions. - Enhanced logging with structured log messages for cache hits and misses. - Implemented a retry mechanism for upstream requests with exponential backoff. - Updated Go modules and indirect dependencies for better compatibility and performance. - Removed unused sync filesystem implementation. - Added version initialization to ensure a default version string.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"s1d3sw1ped/SteamCache2/steamcache/logger"
|
||||
"s1d3sw1ped/SteamCache2/vfs"
|
||||
"s1d3sw1ped/SteamCache2/vfs/vfserror"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
// Ensure MemoryFS implements VFS.
|
||||
@@ -21,6 +24,8 @@ type MemoryFS struct {
|
||||
files map[string]*file
|
||||
capacity int64
|
||||
mu sync.Mutex
|
||||
|
||||
bytePool sync.Pool // Pool for []byte slices
|
||||
}
|
||||
|
||||
// New creates a new MemoryFS.
|
||||
@@ -29,10 +34,18 @@ func New(capacity int64) *MemoryFS {
|
||||
panic("memory capacity must be greater than 0") // panic if the capacity is less than or equal to 0
|
||||
}
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("name", "MemoryFS").
|
||||
Str("capacity", units.HumanSize(float64(capacity))).
|
||||
Msg("init")
|
||||
|
||||
return &MemoryFS{
|
||||
files: make(map[string]*file),
|
||||
capacity: capacity,
|
||||
mu: sync.Mutex{},
|
||||
bytePool: sync.Pool{
|
||||
New: func() interface{} { return make([]byte, 0) }, // Initial capacity for pooled slices
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,13 +80,22 @@ func (m *MemoryFS) Set(key string, src []byte) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
// Use pooled slice
|
||||
data := m.bytePool.Get().([]byte)
|
||||
if cap(data) < len(src) {
|
||||
data = make([]byte, len(src)) // expand the slice if the pool slice is too small
|
||||
} else {
|
||||
data = data[:len(src)] // reuse the pool slice, but resize it to fit
|
||||
}
|
||||
copy(data, src)
|
||||
|
||||
m.files[key] = &file{
|
||||
fileinfo: vfs.NewFileInfo(
|
||||
key,
|
||||
int64(len(src)),
|
||||
time.Now(),
|
||||
),
|
||||
data: src,
|
||||
data: data,
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -88,6 +110,11 @@ func (m *MemoryFS) Delete(key string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
// Return data to pool
|
||||
if f, ok := m.files[key]; ok {
|
||||
m.bytePool.Put(f.data)
|
||||
}
|
||||
|
||||
delete(m.files, key)
|
||||
|
||||
return nil
|
||||
@@ -106,6 +133,12 @@ func (m *MemoryFS) Get(key string) ([]byte, error) {
|
||||
dst := make([]byte, len(m.files[key].data))
|
||||
copy(dst, m.files[key].data)
|
||||
|
||||
logger.Logger.Debug().
|
||||
Str("name", key).
|
||||
Str("status", "GET").
|
||||
Int64("size", int64(len(dst))).
|
||||
Msg("get file from memory")
|
||||
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user