2024-11-09 01:27:32 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-09 02:06:10 +08:00
|
|
|
"kenaito-dns/config"
|
2024-11-09 01:27:32 +08:00
|
|
|
"kenaito-dns/dao"
|
|
|
|
"sync"
|
2024-11-09 02:06:10 +08:00
|
|
|
"time"
|
2024-11-09 01:27:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var KeyResolveRecordMap sync.Map
|
|
|
|
var IdResolveRecordMap sync.Map
|
|
|
|
|
|
|
|
func ReloadCache() {
|
2024-11-09 02:06:10 +08:00
|
|
|
fmt.Println("[app] [info] " + time.Now().Format(config.AppTimeFormat) + " [Cache] Reload cache start")
|
2024-11-09 16:08:16 +08:00
|
|
|
KeyResolveRecordMap.Range(cleanKeyCache)
|
|
|
|
IdResolveRecordMap.Range(cleanIdCache)
|
2024-11-11 20:19:57 +08:00
|
|
|
resolveRecords := dao.FindResolveRecordByVersion(dao.GetResolveVersion(), false)
|
2024-11-09 01:27:32 +08:00
|
|
|
for _, record := range resolveRecords {
|
|
|
|
// id -> resolveRecord
|
|
|
|
IdResolveRecordMap.Store(record.Id, record)
|
|
|
|
// key -> resolveRecord
|
|
|
|
cacheKey := fmt.Sprintf("%s-%s", record.Name, record.RecordType)
|
|
|
|
records, ok := KeyResolveRecordMap.Load(cacheKey)
|
|
|
|
if !ok {
|
|
|
|
var tempRecords []dao.ResolveRecord
|
|
|
|
tempRecords = append(tempRecords, record)
|
|
|
|
KeyResolveRecordMap.Store(cacheKey, tempRecords)
|
|
|
|
} else {
|
|
|
|
var newRecords = records.([]dao.ResolveRecord)
|
|
|
|
records = append(newRecords, record)
|
|
|
|
KeyResolveRecordMap.Store(cacheKey, records)
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 02:06:10 +08:00
|
|
|
fmt.Println("[app] [info] " + time.Now().Format(config.AppTimeFormat) + " [Cache] Reload cache end")
|
2024-11-09 01:27:32 +08:00
|
|
|
}
|
2024-11-09 16:08:16 +08:00
|
|
|
|
|
|
|
func cleanKeyCache(key any, value any) bool {
|
|
|
|
KeyResolveRecordMap.Delete(key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanIdCache(key any, value any) bool {
|
|
|
|
IdResolveRecordMap.Delete(key)
|
|
|
|
return true
|
|
|
|
}
|