memoryfix
This commit is contained in:
parent
6e158529f9
commit
b29bedc522
3 changed files with 65 additions and 7 deletions
60
cache.go
60
cache.go
|
@ -1,13 +1,18 @@
|
|||
// common_cache.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
|
||||
var resultsCache = NewResultsCache(6 * time.Hour) // Cache with 6-hour expiration
|
||||
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{}
|
||||
|
@ -114,9 +119,13 @@ func (rc *ResultsCache) Get(key CacheKey) ([]SearchResult, bool) {
|
|||
func (rc *ResultsCache) Set(key CacheKey, results []SearchResult) {
|
||||
rc.mu.Lock()
|
||||
defer rc.mu.Unlock()
|
||||
rc.results[rc.keyToString(key)] = CachedItem{
|
||||
Results: results,
|
||||
StoredTime: time.Now(),
|
||||
|
||||
if _, exists := rc.results[rc.keyToString(key)]; !exists {
|
||||
rc.results[rc.keyToString(key)] = CachedItem{
|
||||
Results: results,
|
||||
StoredTime: time.Now(),
|
||||
}
|
||||
go rc.checkAndCleanCache()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,7 +134,46 @@ 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)
|
||||
}
|
||||
|
||||
// Helper functions to convert between generic SearchResult and specific ImageSearchResult
|
||||
func (rc *ResultsCache) checkAndCleanCache() {
|
||||
if rc.memoryUsage() > maxMemoryUsage {
|
||||
rc.cleanOldestItems()
|
||||
}
|
||||
}
|
||||
|
||||
func (rc *ResultsCache) memoryUsage() float64 {
|
||||
v, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get memory info: %v", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
return v.UsedPercent
|
||||
}
|
||||
|
||||
func (rc *ResultsCache) cleanOldestItems() {
|
||||
rc.mu.Lock()
|
||||
defer rc.mu.Unlock()
|
||||
|
||||
for rc.memoryUsage() > maxMemoryUsage {
|
||||
var oldestKey string
|
||||
var oldestTime time.Time = time.Now()
|
||||
|
||||
for key, item := range rc.results {
|
||||
if item.StoredTime.Before(oldestTime) {
|
||||
oldestTime = item.StoredTime
|
||||
oldestKey = key
|
||||
}
|
||||
}
|
||||
|
||||
if oldestKey != "" {
|
||||
delete(rc.results, oldestKey)
|
||||
log.Printf("Removed oldest cache item: %s", oldestKey)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func convertToSearchResults(results interface{}) []SearchResult {
|
||||
switch res := results.(type) {
|
||||
case []TextSearchResult:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue