|
| 1 | +// Copyright 2019 The Xorm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package caches |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/gob" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "xorm.io/xorm/schemas" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // CacheExpired is default cache expired time |
| 20 | + CacheExpired = 60 * time.Minute |
| 21 | + // CacheMaxMemory is not use now |
| 22 | + CacheMaxMemory = 256 |
| 23 | + // CacheGcInterval represents interval time to clear all expired nodes |
| 24 | + CacheGcInterval = 10 * time.Minute |
| 25 | + // CacheGcMaxRemoved represents max nodes removed when gc |
| 26 | + CacheGcMaxRemoved = 20 |
| 27 | +) |
| 28 | + |
| 29 | +// list all the errors |
| 30 | +var ( |
| 31 | + ErrCacheMiss = errors.New("xorm/cache: key not found") |
| 32 | + ErrNotStored = errors.New("xorm/cache: not stored") |
| 33 | + // ErrNotExist record does not exist error |
| 34 | + ErrNotExist = errors.New("Record does not exist") |
| 35 | +) |
| 36 | + |
| 37 | +// CacheStore is a interface to store cache |
| 38 | +type CacheStore interface { |
| 39 | + // key is primary key or composite primary key |
| 40 | + // value is struct's pointer |
| 41 | + // key format : <tablename>-p-<pk1>-<pk2>... |
| 42 | + Put(key string, value interface{}) error |
| 43 | + Get(key string) (interface{}, error) |
| 44 | + Del(key string) error |
| 45 | +} |
| 46 | + |
| 47 | +// Cacher is an interface to provide cache |
| 48 | +// id format : u-<pk1>-<pk2>... |
| 49 | +type Cacher interface { |
| 50 | + GetIds(tableName, sql string) interface{} |
| 51 | + GetBean(tableName string, id string) interface{} |
| 52 | + PutIds(tableName, sql string, ids interface{}) |
| 53 | + PutBean(tableName string, id string, obj interface{}) |
| 54 | + DelIds(tableName, sql string) |
| 55 | + DelBean(tableName string, id string) |
| 56 | + ClearIds(tableName string) |
| 57 | + ClearBeans(tableName string) |
| 58 | +} |
| 59 | + |
| 60 | +func encodeIds(ids []schemas.PK) (string, error) { |
| 61 | + buf := new(bytes.Buffer) |
| 62 | + enc := gob.NewEncoder(buf) |
| 63 | + err := enc.Encode(ids) |
| 64 | + |
| 65 | + return buf.String(), err |
| 66 | +} |
| 67 | + |
| 68 | +func decodeIds(s string) ([]schemas.PK, error) { |
| 69 | + pks := make([]schemas.PK, 0) |
| 70 | + |
| 71 | + dec := gob.NewDecoder(strings.NewReader(s)) |
| 72 | + err := dec.Decode(&pks) |
| 73 | + |
| 74 | + return pks, err |
| 75 | +} |
| 76 | + |
| 77 | +// GetCacheSql returns cacher PKs via SQL |
| 78 | +func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]schemas.PK, error) { |
| 79 | + bytes := m.GetIds(tableName, GenSqlKey(sql, args)) |
| 80 | + if bytes == nil { |
| 81 | + return nil, errors.New("Not Exist") |
| 82 | + } |
| 83 | + return decodeIds(bytes.(string)) |
| 84 | +} |
| 85 | + |
| 86 | +// PutCacheSql puts cacher SQL and PKs |
| 87 | +func PutCacheSql(m Cacher, ids []schemas.PK, tableName, sql string, args interface{}) error { |
| 88 | + bytes, err := encodeIds(ids) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + m.PutIds(tableName, GenSqlKey(sql, args), bytes) |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// GenSqlKey generates cache key |
| 97 | +func GenSqlKey(sql string, args interface{}) string { |
| 98 | + return fmt.Sprintf("%v-%v", sql, args) |
| 99 | +} |
0 commit comments