|
@@ -39,10 +39,14 @@ func (dc *databaseCache) Run(ctx context.Context) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (dc *databaseCache) internalRunGC() {
|
|
func (dc *databaseCache) internalRunGC() {
|
|
|
- now := getTime().Unix()
|
|
|
|
|
- sql := `DELETE FROM cache_data WHERE (? - created_at) >= expires AND expires <> 0`
|
|
|
|
|
|
|
+ err := dc.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
|
|
|
+ now := getTime().Unix()
|
|
|
|
|
+ sql := `DELETE FROM cache_data WHERE (? - created_at) >= expires AND expires <> 0`
|
|
|
|
|
+
|
|
|
|
|
+ _, err := session.Exec(sql, now)
|
|
|
|
|
+ return err
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- _, err := dc.SQLStore.NewSession().Exec(sql, now)
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
dc.log.Error("failed to run garbage collect", "error", err)
|
|
dc.log.Error("failed to run garbage collect", "error", err)
|
|
|
}
|
|
}
|
|
@@ -80,44 +84,48 @@ func (dc *databaseCache) Get(key string) (interface{}, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration) error {
|
|
func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration) error {
|
|
|
- item := &cachedItem{Val: value}
|
|
|
|
|
- data, err := encodeGob(item)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- session := dc.SQLStore.NewSession()
|
|
|
|
|
|
|
+ return dc.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
|
|
|
+ item := &cachedItem{Val: value}
|
|
|
|
|
+ data, err := encodeGob(item)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var cacheHit CacheData
|
|
|
|
|
- has, err := session.Where("cache_key = ?", key).Get(&cacheHit)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var cacheHit CacheData
|
|
|
|
|
+ has, err := session.Where("cache_key = ?", key).Get(&cacheHit)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var expiresInSeconds int64
|
|
|
|
|
- if expire != 0 {
|
|
|
|
|
- expiresInSeconds = int64(expire) / int64(time.Second)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var expiresInSeconds int64
|
|
|
|
|
+ if expire != 0 {
|
|
|
|
|
+ expiresInSeconds = int64(expire) / int64(time.Second)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // insert or update depending on if item already exist
|
|
|
|
|
- if has {
|
|
|
|
|
- sql := `UPDATE cache_data SET data=?, created_at=?, expires=? WHERE cache_key=?`
|
|
|
|
|
- _, err = session.Exec(sql, data, getTime().Unix(), expiresInSeconds, key)
|
|
|
|
|
- } else {
|
|
|
|
|
- sql := `INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
|
|
|
|
|
- _, err = session.Exec(sql, key, data, getTime().Unix(), expiresInSeconds)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // insert or update depending on if item already exist
|
|
|
|
|
+ if has {
|
|
|
|
|
+ sql := `UPDATE cache_data SET data=?, created_at=?, expires=? WHERE cache_key=?`
|
|
|
|
|
+ _, err = session.Exec(sql, data, getTime().Unix(), expiresInSeconds, key)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sql := `INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
|
|
|
|
|
+ _, err = session.Exec(sql, key, data, getTime().Unix(), expiresInSeconds)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return err
|
|
|
|
|
|
|
+ return err
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (dc *databaseCache) Delete(key string) error {
|
|
func (dc *databaseCache) Delete(key string) error {
|
|
|
- sql := "DELETE FROM cache_data WHERE cache_key=?"
|
|
|
|
|
- _, err := dc.SQLStore.NewSession().Exec(sql, key)
|
|
|
|
|
|
|
+ return dc.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
|
|
|
+ sql := "DELETE FROM cache_data WHERE cache_key=?"
|
|
|
|
|
+ _, err := session.Exec(sql, key)
|
|
|
|
|
+
|
|
|
|
|
+ return err
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- return err
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// CacheData is the struct representing the table in the database
|
|
|
type CacheData struct {
|
|
type CacheData struct {
|
|
|
CacheKey string
|
|
CacheKey string
|
|
|
Data []byte
|
|
Data []byte
|