mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
- Added new dependency on github.com/mattn/go-sqlite3 for improved metrics storage. - Updated main.go to initialize metrics collector with a new database path and configuration settings. - Enhanced config.json to include additional metrics settings such as alert configurations and latency thresholds. - Refactored internal metrics handling to support new metrics structures and improve data retrieval. - Introduced a new metrics history endpoint for retrieving historical data, enhancing monitoring capabilities. - Improved UI for metrics dashboard to include historical data visualization options.
83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
package cache
|
|
|
|
import (
|
|
"proxy-go/internal/constants"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Cache struct {
|
|
data sync.RWMutex
|
|
items map[string]*cacheItem
|
|
ttl time.Duration
|
|
maxSize int
|
|
}
|
|
|
|
type cacheItem struct {
|
|
value interface{}
|
|
timestamp time.Time
|
|
}
|
|
|
|
func NewCache(ttl time.Duration) *Cache {
|
|
c := &Cache{
|
|
items: make(map[string]*cacheItem),
|
|
ttl: ttl,
|
|
maxSize: constants.MaxCacheSize,
|
|
}
|
|
go c.cleanup()
|
|
return c
|
|
}
|
|
|
|
func (c *Cache) Set(key string, value interface{}) {
|
|
c.data.Lock()
|
|
if len(c.items) >= c.maxSize {
|
|
oldest := time.Now()
|
|
var oldestKey string
|
|
for k, v := range c.items {
|
|
if v.timestamp.Before(oldest) {
|
|
oldest = v.timestamp
|
|
oldestKey = k
|
|
}
|
|
}
|
|
delete(c.items, oldestKey)
|
|
}
|
|
c.items[key] = &cacheItem{
|
|
value: value,
|
|
timestamp: time.Now(),
|
|
}
|
|
c.data.Unlock()
|
|
}
|
|
|
|
func (c *Cache) Get(key string) (interface{}, bool) {
|
|
c.data.RLock()
|
|
item, exists := c.items[key]
|
|
c.data.RUnlock()
|
|
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
|
|
if time.Since(item.timestamp) > c.ttl {
|
|
c.data.Lock()
|
|
delete(c.items, key)
|
|
c.data.Unlock()
|
|
return nil, false
|
|
}
|
|
|
|
return item.value, true
|
|
}
|
|
|
|
func (c *Cache) cleanup() {
|
|
ticker := time.NewTicker(c.ttl)
|
|
for range ticker.C {
|
|
now := time.Now()
|
|
c.data.Lock()
|
|
for key, item := range c.items {
|
|
if now.Sub(item.timestamp) > c.ttl {
|
|
delete(c.items, key)
|
|
}
|
|
}
|
|
c.data.Unlock()
|
|
}
|
|
}
|