From b672014e483f10871ea137a5bc9a7091fddf2231 Mon Sep 17 00:00:00 2001 From: odboy Date: Fri, 8 Nov 2024 22:01:15 +0800 Subject: [PATCH 1/4] release: version 1.0.0 --- dao.go | 20 -------------------- database.go | 26 -------------------------- restful.go | 19 ------------------- 3 files changed, 65 deletions(-) delete mode 100644 dao.go delete mode 100644 database.go delete mode 100644 restful.go diff --git a/dao.go b/dao.go deleted file mode 100644 index 19a5c7d..0000000 --- a/dao.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import "fmt" - -type ResolveRecord struct { - Id int32 `xorm:"pk not null integer 'id' autoincr"` - Name string `xorm:"not null text 'name'"` - RecordType string `xorm:"not null text 'record_type'"` - Ttl int32 `xorm:"not null integer 'ttl'"` - Value string `xorm:"not null text 'value'"` -} - -func selectResolveRecords() []*ResolveRecord { - records := make([]*ResolveRecord, 0) - err := Engine.Table("resolve_record").Find(&records) - if err != nil { - fmt.Println(err) - } - return records -} diff --git a/database.go b/database.go deleted file mode 100644 index a964fd3..0000000 --- a/database.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "fmt" - "github.com/go-xorm/xorm" - _ "github.com/mattn/go-sqlite3" -) - -var ( - Engine *xorm.Engine -) - -func init() { - var err error - Engine, err = xorm.NewEngine("sqlite3", "dns.sqlite3") - if err != nil { - fmt.Println(err) - } - fmt.Println("数据库引擎创建成功") - - err = Engine.Ping() - if err != nil { - fmt.Println(err) - } - fmt.Println("数据库连接成功") -} diff --git a/restful.go b/restful.go deleted file mode 100644 index b01aaff..0000000 --- a/restful.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "github.com/gin-gonic/gin" -) - -func initRestFunc(r *gin.Engine) { - //records := selectResolveRecords() - //jsonData, err := json.Marshal(records) - //if err != nil { - // log.Fatal(err) - //} - //fmt.Println(string(jsonData)) - r.GET("/ping", func(c *gin.Context) { - c.JSON(200, gin.H{ - "message": "pong", - }) - }) -} From 26a3e376a5316a3b9d0013cd1feccb1debf266e7 Mon Sep 17 00:00:00 2001 From: odboy Date: Fri, 8 Nov 2024 22:02:01 +0800 Subject: [PATCH 2/4] release: version 1.0.0 --- dns.sqlite3 | Bin 36864 -> 36864 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/dns.sqlite3 b/dns.sqlite3 index 5b88830a04bd59f4b89f2d16ce598bacedd343df..c252b45b7f6086a59081f8a2480aa6b85774c282 100644 GIT binary patch delta 19 acmZozz|^pSX@WGP#Y7orMvILJ3;F>+-v*KZ delta 19 acmZozz|^pSX@WGP`9v9KM)Qpc3;F>+&<2nI From 27cca6de0c9dbe51f25b5e1ef939b243be8b4c2e Mon Sep 17 00:00:00 2001 From: odboy Date: Fri, 8 Nov 2024 22:08:45 +0800 Subject: [PATCH 3/4] release: version 1.0.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fb76c3..cd64aeb 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ nslookup example.com 192.168.1.103 - [DNS解析 - miekg/dns](https://github.com/miekg/dns) - [Web - gin](https://gin-gonic.com/zh-cn/docs/quickstart/) -## 代码托管 +## 代码托管(以私人仓库Gitea为准) - Gitea: [https://gitea.odboy.cn/odboy/kenaito-dns](https://gitea.odboy.cn/odboy/kenaito-dns) - Github: [https://github.com/odboy-tianjun/kenaito-dns](https://github.com/odboy-tianjun/kenaito-dns) From fae02c0daf24b63469d8e510285e401aca672b63 Mon Sep 17 00:00:00 2001 From: odboy Date: Sat, 9 Nov 2024 01:27:32 +0800 Subject: [PATCH 4/4] 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