feat: 添加定时清理图像缓存的功能,以优化内存使用

This commit is contained in:
lixiangwuxian 2025-05-14 00:39:02 +08:00
parent 73ed9bedc9
commit b9dabbfcb4

View File

@ -15,6 +15,7 @@ import (
"sync"
"time"
"git.lxtend.com/qqbot/util"
"github.com/fogleman/gg"
"github.com/nfnt/resize"
"golang.org/x/image/font/opentype"
@ -25,6 +26,16 @@ var (
imageCacheLock sync.RWMutex
)
func init() {
util.AddCycleTask("bs50_image_cache_cleaner", 10*time.Minute, 10*time.Minute, func() {
imageCacheLock.Lock()
defer imageCacheLock.Unlock()
for k := range imageCache {
delete(imageCache, k)
}
})
}
func loadImageFromCache(path string) (image.Image, error) {
imageCacheLock.RLock()
if img, exists := imageCache[path]; exists {