From fae02c0daf24b63469d8e510285e401aca672b63 Mon Sep 17 00:00:00 2001 From: odboy Date: Sat, 9 Nov 2024 01:27:32 +0800 Subject: [PATCH] feat: add cache --- cache/resolve_record.go | 32 ++++++++++++++++++++++++++++++++ dao/resolve_config.go | 2 +- dao/resolve_record.go | 12 ++++++------ dns.sqlite3 | Bin 36864 -> 36864 bytes main.go | 2 ++ 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 cache/resolve_record.go diff --git a/cache/resolve_record.go b/cache/resolve_record.go new file mode 100644 index 0000000..fef0f11 --- /dev/null +++ b/cache/resolve_record.go @@ -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) + } + } +} diff --git a/dao/resolve_config.go b/dao/resolve_config.go index 242c462..2a18407 100644 --- a/dao/resolve_config.go +++ b/dao/resolve_config.go @@ -14,7 +14,7 @@ type ResolveVersion struct { CurrentVersion int `xorm:"not null integer 'curr_version'"` } -func getResolveVersion() int { +func GetResolveVersion() int { var records []ResolveVersion err := Engine.Table("resolve_config").Where("`id` = ?", 1).Find(&records) if err != nil { diff --git a/dao/resolve_record.go b/dao/resolve_record.go index a36ed64..8cb9ae6 100644 --- a/dao/resolve_record.go +++ b/dao/resolve_record.go @@ -41,7 +41,7 @@ func FindResolveRecordByVersion(version int) []ResolveRecord { func FindResolveRecordByNameType(name string, recordType string) []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 { fmt.Println(err) } @@ -75,7 +75,7 @@ func FindResolveRecordPage(pageNo int, pageSize int, args *domain.QueryPageArgs) session.And("`value` = ?", qs) } } - session.And("`version` = ?", getResolveVersion()) + session.And("`version` = ?", GetResolveVersion()) err := session.Limit(pageSize, offset).Find(&records) if err != nil { fmt.Println(err) @@ -94,8 +94,8 @@ func SaveResolveRecord(wrapper *ResolveRecord) (bool, error) { func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) { var backupRecords []*ResolveRecord - oldVersion := getResolveVersion() - newVersion := getResolveVersion() + 1 + oldVersion := GetResolveVersion() + newVersion := GetResolveVersion() + 1 oldRecords := FindResolveRecordByVersion(oldVersion) for _, oldRecord := range oldRecords { newRecord := new(ResolveRecord) @@ -134,7 +134,7 @@ func RemoveResolveRecord(wrapper *ResolveRecord) (bool, error) { } func IsResolveRecordExist(wrapper *ResolveRecord) bool { - wrapper.Version = getResolveVersion() + wrapper.Version = GetResolveVersion() count, err := Engine.Table("resolve_record").Count(wrapper) if err != nil { fmt.Println(err) @@ -154,7 +154,7 @@ func IsUpdResolveRecordExist(id int, wrapper *ResolveRecord) bool { r.Name = wrapper.Name r.RecordType = wrapper.RecordType r.Value = wrapper.Value - r.Version = getResolveVersion() + r.Version = GetResolveVersion() count, err := Engine.Table("resolve_record").Where("id != ?", id).Count(r) if err != nil { fmt.Println(err) diff --git a/dns.sqlite3 b/dns.sqlite3 index c252b45b7f6086a59081f8a2480aa6b85774c282..f4a09b8bf9597138b72327ade04254ae7a6546b1 100644 GIT binary patch delta 227 zcmZozz|^pSX@WGP?L--8M%#@E^YsP!co`TNSol*I_;2!W;_v29*{moK!e6h-$IhV6 zrn3$-SpOTbcsh6Ce>&RtjWUOaoWTa