Commit 7aeab0a2 by bergquist

use `Get` instead of `Find`

parent b2967fbb
......@@ -47,24 +47,24 @@ func (dc *databaseCache) internalRunGC() {
}
func (dc *databaseCache) Get(key string) (interface{}, error) {
cacheHits := []CacheData{}
sess := dc.SQLStore.NewSession()
defer sess.Close()
err := sess.Where("cache_key= ?", key).Find(&cacheHits)
cacheHit := CacheData{}
session := dc.SQLStore.NewSession()
defer session.Close()
exist, err := session.Where("cache_key= ?", key).Get(&cacheHit)
if err != nil {
return nil, err
}
if len(cacheHits) == 0 {
if !exist {
return nil, ErrCacheItemNotFound
}
cacheHit := cacheHits[0]
if cacheHit.Expires > 0 {
existedButExpired := getTime().Unix()-cacheHit.CreatedAt >= cacheHit.Expires
if existedButExpired {
dc.Delete(key)
_ = dc.Delete(key) //ignore this error since we will return `ErrCacheItemNotFound` anyway
return nil, ErrCacheItemNotFound
}
}
......@@ -99,9 +99,11 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
// insert or update depending on if item already exist
if has {
_, err = session.Exec(`UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'`, data, getTime().Unix(), expiresAtEpoch, key)
sql := `UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'`
_, err = session.Exec(sql, data, getTime().Unix(), expiresAtEpoch, key)
} else {
_, err = session.Exec(`INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`, key, data, getTime().Unix(), expiresAtEpoch)
sql := `INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
_, err = session.Exec(sql, key, data, getTime().Unix(), expiresAtEpoch)
}
return err
......@@ -120,5 +122,3 @@ type CacheData struct {
Expires int64
CreatedAt int64
}
// func (cd CacheData) TableName() string { return "cache_data" }
......@@ -11,8 +11,6 @@ function exit_if_fail {
}
echo "running redis and memcache tests"
#set -e
#time for d in $(go list ./pkg/...); do
time exit_if_fail go test -tags=redis ./pkg/infra/remotecache/...
time exit_if_fail go test -tags=memcached ./pkg/infra/remotecache/...
#done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment