updated 'config.ini'
This commit is contained in:
parent
28f71271d7
commit
be4f86580e
13 changed files with 635 additions and 208 deletions
31
cache.go
31
cache.go
|
@ -8,11 +8,6 @@ import (
|
|||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
|
||||
var (
|
||||
resultsCache = NewResultsCache(6 * time.Hour) // Cache with 6-hour expiration
|
||||
maxMemoryUsage = 90.0 // Maximum memory usage in %
|
||||
)
|
||||
|
||||
// SearchResult is a generic interface for all types of search results.
|
||||
type SearchResult interface{}
|
||||
|
||||
|
@ -89,11 +84,14 @@ type ResultsCache struct {
|
|||
expiration time.Duration
|
||||
}
|
||||
|
||||
var resultsCache *ResultsCache
|
||||
|
||||
// NewResultsCache creates a new ResultsCache with a specified expiration duration.
|
||||
func NewResultsCache(expiration time.Duration) *ResultsCache {
|
||||
func NewResultsCache() *ResultsCache {
|
||||
printDebug("Initializing results cache with expiration: %s and max usage: %d bytes", config.RamCache.Duration, config.RamCache.MaxUsageBytes)
|
||||
return &ResultsCache{
|
||||
results: make(map[string]CachedItem),
|
||||
expiration: expiration,
|
||||
expiration: config.RamCache.Duration,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,8 +106,9 @@ func (rc *ResultsCache) Get(key CacheKey) ([]SearchResult, bool) {
|
|||
}
|
||||
|
||||
// Check if the item has expired
|
||||
if time.Since(item.StoredTime) > rc.expiration {
|
||||
if time.Since(item.StoredTime) > config.RamCache.Duration {
|
||||
delete(rc.results, rc.keyToString(key))
|
||||
printDebug("Cache expired for key: %s", rc.keyToString(key))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
|
@ -135,12 +134,24 @@ func (rc *ResultsCache) keyToString(key CacheKey) string {
|
|||
return fmt.Sprintf("%s|%d|%t|%s|%s", key.Query, key.Page, key.Safe, key.Lang, key.Type)
|
||||
}
|
||||
|
||||
// checkAndCleanCache removes items if memory usage exceeds the limit.
|
||||
func (rc *ResultsCache) checkAndCleanCache() {
|
||||
if rc.memoryUsage() > maxMemoryUsage {
|
||||
for rc.currentMemoryUsage() > config.RamCache.MaxUsageBytes {
|
||||
rc.cleanOldestItems()
|
||||
}
|
||||
}
|
||||
|
||||
// currentMemoryUsage calculates the current memory usage in bytes.
|
||||
func (rc *ResultsCache) currentMemoryUsage() uint64 {
|
||||
v, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
printErr("Failed to get memory info: %v", err)
|
||||
return 0
|
||||
}
|
||||
return v.Used // Used memory in bytes
|
||||
}
|
||||
|
||||
// memoryUsage calculates the current memory usage as a percentage.
|
||||
func (rc *ResultsCache) memoryUsage() float64 {
|
||||
v, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
|
@ -155,7 +166,7 @@ func (rc *ResultsCache) cleanOldestItems() {
|
|||
rc.mu.Lock()
|
||||
defer rc.mu.Unlock()
|
||||
|
||||
for rc.memoryUsage() > maxMemoryUsage {
|
||||
for rc.currentMemoryUsage() > config.RamCache.MaxUsageBytes {
|
||||
var oldestKey string
|
||||
var oldestTime time.Time = time.Now()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue