feat: add cache
This commit is contained in:
commit
72353369cd
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
BIN
dns.sqlite3
BIN
dns.sqlite3
Binary file not shown.
2
main.go
2
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/miekg/dns"
|
||||
"kenaito-dns/cache"
|
||||
"kenaito-dns/config"
|
||||
"kenaito-dns/controller"
|
||||
"kenaito-dns/core"
|
||||
|
@ -18,6 +19,7 @@ import (
|
|||
|
||||
func main() {
|
||||
fmt.Println("[app] [info] kenaito-dns version = " + config.AppVersion)
|
||||
go cache.ReloadCache()
|
||||
go initDNSServer()
|
||||
initRestfulServer()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue