feat: add cache

This commit is contained in:
骑着蜗牛追导弹 2024-11-09 01:27:32 +08:00
parent 27cca6de0c
commit fae02c0daf
5 changed files with 41 additions and 7 deletions

32
cache/resolve_record.go vendored Normal file
View File

@ -0,0 +1,32 @@
package cache
import (
"fmt"
"kenaito-dns/dao"
"sync"
)
var KeyResolveRecordMap sync.Map
var IdResolveRecordMap sync.Map
func ReloadCache() {
resolveRecords := dao.FindResolveRecordByVersion(dao.GetResolveVersion())
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 {
fmt.Println("读取缓存失败, key=" + cacheKey)
var tempRecords []dao.ResolveRecord
tempRecords = append(tempRecords, record)
KeyResolveRecordMap.Store(cacheKey, tempRecords)
} else {
fmt.Println("读取缓存成功, key=" + cacheKey)
var newRecords = records.([]dao.ResolveRecord)
records = append(newRecords, record)
KeyResolveRecordMap.Store(cacheKey, records)
}
}
}

View File

@ -14,7 +14,7 @@ type ResolveVersion struct {
CurrentVersion int `xorm:"not null integer 'curr_version'"` CurrentVersion int `xorm:"not null integer 'curr_version'"`
} }
func getResolveVersion() int { func GetResolveVersion() int {
var records []ResolveVersion var records []ResolveVersion
err := Engine.Table("resolve_config").Where("`id` = ?", 1).Find(&records) err := Engine.Table("resolve_config").Where("`id` = ?", 1).Find(&records)
if err != nil { if err != nil {

View File

@ -41,7 +41,7 @@ func FindResolveRecordByVersion(version int) []ResolveRecord {
func FindResolveRecordByNameType(name string, recordType string) []ResolveRecord { func FindResolveRecordByNameType(name string, recordType string) []ResolveRecord {
var records []ResolveRecord var records []ResolveRecord
err := Engine.Table("resolve_record").Where("`name` = ? and `record_type` = ? and `version` = ?", name, recordType, getResolveVersion()).Find(&records) err := Engine.Table("resolve_record").Where("`name` = ? and `record_type` = ? and `version` = ?", name, recordType, GetResolveVersion()).Find(&records)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -75,7 +75,7 @@ func FindResolveRecordPage(pageNo int, pageSize int, args *domain.QueryPageArgs)
session.And("`value` = ?", qs) session.And("`value` = ?", qs)
} }
} }
session.And("`version` = ?", getResolveVersion()) session.And("`version` = ?", GetResolveVersion())
err := session.Limit(pageSize, offset).Find(&records) err := session.Limit(pageSize, offset).Find(&records)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -94,8 +94,8 @@ func SaveResolveRecord(wrapper *ResolveRecord) (bool, error) {
func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) { func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) {
var backupRecords []*ResolveRecord var backupRecords []*ResolveRecord
oldVersion := getResolveVersion() oldVersion := GetResolveVersion()
newVersion := getResolveVersion() + 1 newVersion := GetResolveVersion() + 1
oldRecords := FindResolveRecordByVersion(oldVersion) oldRecords := FindResolveRecordByVersion(oldVersion)
for _, oldRecord := range oldRecords { for _, oldRecord := range oldRecords {
newRecord := new(ResolveRecord) newRecord := new(ResolveRecord)
@ -134,7 +134,7 @@ func RemoveResolveRecord(wrapper *ResolveRecord) (bool, error) {
} }
func IsResolveRecordExist(wrapper *ResolveRecord) bool { func IsResolveRecordExist(wrapper *ResolveRecord) bool {
wrapper.Version = getResolveVersion() wrapper.Version = GetResolveVersion()
count, err := Engine.Table("resolve_record").Count(wrapper) count, err := Engine.Table("resolve_record").Count(wrapper)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -154,7 +154,7 @@ func IsUpdResolveRecordExist(id int, wrapper *ResolveRecord) bool {
r.Name = wrapper.Name r.Name = wrapper.Name
r.RecordType = wrapper.RecordType r.RecordType = wrapper.RecordType
r.Value = wrapper.Value r.Value = wrapper.Value
r.Version = getResolveVersion() r.Version = GetResolveVersion()
count, err := Engine.Table("resolve_record").Where("id != ?", id).Count(r) count, err := Engine.Table("resolve_record").Where("id != ?", id).Count(r)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

Binary file not shown.

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/miekg/dns" "github.com/miekg/dns"
"kenaito-dns/cache"
"kenaito-dns/config" "kenaito-dns/config"
"kenaito-dns/controller" "kenaito-dns/controller"
"kenaito-dns/core" "kenaito-dns/core"
@ -18,6 +19,7 @@ import (
func main() { func main() {
fmt.Println("[app] [info] kenaito-dns version = " + config.AppVersion) fmt.Println("[app] [info] kenaito-dns version = " + config.AppVersion)
go cache.ReloadCache()
go initDNSServer() go initDNSServer()
initRestfulServer() initRestfulServer()
} }