From 28ad2e074b54b18c0cd6162a26ada6844d4e2f15 Mon Sep 17 00:00:00 2001 From: cp-20 Date: Mon, 3 Feb 2025 21:39:31 +0900 Subject: [PATCH] fix reviews --- template/cache.go | 2 +- template/cache.tmpl | 2 +- template/driver.go | 19 ++++++++----------- template/driver.tmpl | 20 +++++++++----------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/template/cache.go b/template/cache.go index 68a28ed..83e3e44 100644 --- a/template/cache.go +++ b/template/cache.go @@ -43,7 +43,7 @@ func (c *cacheWithInfo) isNewerThan(key string, t int64) bool { } func (c *cacheWithInfo) RecordReplaceTime(time time.Duration) { - c.replaceTime.Add(int64(time)) + c.replaceTime.Add(time.Nanoseconds()) } type ( diff --git a/template/cache.tmpl b/template/cache.tmpl index fcacbd2..11e58b1 100644 --- a/template/cache.tmpl +++ b/template/cache.tmpl @@ -41,7 +41,7 @@ func (c *cacheWithInfo) isNewerThan(key string, t int64) bool { } func (c *cacheWithInfo) RecordReplaceTime(time time.Duration) { - c.replaceTime.Add(int64(time)) + c.replaceTime.Add(time.Nanoseconds()) } type ( diff --git a/template/driver.go b/template/driver.go index 2be44e6..7b2ba75 100644 --- a/template/driver.go +++ b/template/driver.go @@ -8,7 +8,6 @@ import ( "io" "log" "strings" - "sync/atomic" "time" "github.com/go-sql-driver/mysql" @@ -53,20 +52,18 @@ func init() { conditions := query.Select.Conditions if isSingleUniqueCondition(conditions, query.Select.Table) { caches[normalized] = &cacheWithInfo{ - Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), - query: normalized, - info: *query.Select, - uniqueOnly: true, - replaceTime: atomic.Int64{}, + Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), + query: normalized, + info: *query.Select, + uniqueOnly: true, } continue } caches[query.Query] = &cacheWithInfo{ - Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), - query: query.Query, - info: *query.Select, - uniqueOnly: false, - replaceTime: atomic.Int64{}, + Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), + query: query.Query, + info: *query.Select, + uniqueOnly: false, } // TODO: if query is like "SELECT * FROM WHERE pk IN (?, ?, ...)", generate cache with query "SELECT * FROM table WHERE pk = ?" diff --git a/template/driver.tmpl b/template/driver.tmpl index c7f48b9..e3d4afc 100644 --- a/template/driver.tmpl +++ b/template/driver.tmpl @@ -49,22 +49,20 @@ func init() { } conditions := query.Select.Conditions - if isSingleUniqueCondition(conditions, query.Select.Table) { + if isSingleUniqueCondition(conditions, query.Select.Table) { caches[normalized] = &cacheWithInfo{ - Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), - query: normalized, - info: *query.Select, - uniqueOnly: true, - replaceTime: atomic.Int64{}, + Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), + query: normalized, + info: *query.Select, + uniqueOnly: true, } continue } caches[query.Query] = &cacheWithInfo{ - Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), - query: query.Query, - info: *query.Select, - uniqueOnly: false, - replaceTime: atomic.Int64{}, + Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute), + query: query.Query, + info: *query.Select, + uniqueOnly: false, } // TODO: if query is like "SELECT * FROM WHERE pk IN (?, ?, ...)", generate cache with query "SELECT * FROM table WHERE pk = ?"