aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/go-xorm/core/cache.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2017-02-09 19:48:13 -0500
committerUnknwon <u@gogs.io>2017-02-09 19:48:13 -0500
commit2fd69f13d9599a6c58b47225565163fd7d87889f (patch)
treefd19e868e1c2e95a5fb83a268f6e393669d6ee79 /vendor/github.com/go-xorm/core/cache.go
parenteb66060cd7b9bce996b1d75ae80ce1ef31d5ce62 (diff)
vendor: check in vendors
Bye bye glide...
Diffstat (limited to 'vendor/github.com/go-xorm/core/cache.go')
-rw-r--r--vendor/github.com/go-xorm/core/cache.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/vendor/github.com/go-xorm/core/cache.go b/vendor/github.com/go-xorm/core/cache.go
new file mode 100644
index 00000000..bf81bd52
--- /dev/null
+++ b/vendor/github.com/go-xorm/core/cache.go
@@ -0,0 +1,87 @@
+package core
+
+import (
+ "errors"
+ "fmt"
+ "time"
+ "bytes"
+ "encoding/gob"
+)
+
+const (
+ // default cache expired time
+ CacheExpired = 60 * time.Minute
+ // not use now
+ CacheMaxMemory = 256
+ // evey ten minutes to clear all expired nodes
+ CacheGcInterval = 10 * time.Minute
+ // each time when gc to removed max nodes
+ CacheGcMaxRemoved = 20
+)
+
+var (
+ ErrCacheMiss = errors.New("xorm/cache: key not found.")
+ ErrNotStored = errors.New("xorm/cache: not stored.")
+)
+
+// CacheStore is a interface to store cache
+type CacheStore interface {
+ // key is primary key or composite primary key
+ // value is struct's pointer
+ // key format : <tablename>-p-<pk1>-<pk2>...
+ Put(key string, value interface{}) error
+ Get(key string) (interface{}, error)
+ Del(key string) error
+}
+
+// Cacher is an interface to provide cache
+// id format : u-<pk1>-<pk2>...
+type Cacher interface {
+ GetIds(tableName, sql string) interface{}
+ GetBean(tableName string, id string) interface{}
+ PutIds(tableName, sql string, ids interface{})
+ PutBean(tableName string, id string, obj interface{})
+ DelIds(tableName, sql string)
+ DelBean(tableName string, id string)
+ ClearIds(tableName string)
+ ClearBeans(tableName string)
+}
+
+func encodeIds(ids []PK) (string, error) {
+ buf := new(bytes.Buffer)
+ enc := gob.NewEncoder(buf)
+ err := enc.Encode(ids)
+
+ return buf.String(), err
+}
+
+
+func decodeIds(s string) ([]PK, error) {
+ pks := make([]PK, 0)
+
+ dec := gob.NewDecoder(bytes.NewBufferString(s))
+ err := dec.Decode(&pks)
+
+ return pks, err
+}
+
+func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error) {
+ bytes := m.GetIds(tableName, GenSqlKey(sql, args))
+ if bytes == nil {
+ return nil, errors.New("Not Exist")
+ }
+ return decodeIds(bytes.(string))
+}
+
+func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error {
+ bytes, err := encodeIds(ids)
+ if err != nil {
+ return err
+ }
+ m.PutIds(tableName, GenSqlKey(sql, args), bytes)
+ return nil
+}
+
+func GenSqlKey(sql string, args interface{}) string {
+ return fmt.Sprintf("%v-%v", sql, args)
+}