本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.4KB

  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. package core
  5. import (
  6. "bytes"
  7. "encoding/gob"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. // CacheExpired is default cache expired time
  15. CacheExpired = 60 * time.Minute
  16. // CacheMaxMemory is not use now
  17. CacheMaxMemory = 256
  18. // CacheGcInterval represents interval time to clear all expired nodes
  19. CacheGcInterval = 10 * time.Minute
  20. // CacheGcMaxRemoved represents max nodes removed when gc
  21. CacheGcMaxRemoved = 20
  22. )
  23. // list all the errors
  24. var (
  25. ErrCacheMiss = errors.New("xorm/cache: key not found")
  26. ErrNotStored = errors.New("xorm/cache: not stored")
  27. )
  28. // CacheStore is a interface to store cache
  29. type CacheStore interface {
  30. // key is primary key or composite primary key
  31. // value is struct's pointer
  32. // key format : <tablename>-p-<pk1>-<pk2>...
  33. Put(key string, value interface{}) error
  34. Get(key string) (interface{}, error)
  35. Del(key string) error
  36. }
  37. // Cacher is an interface to provide cache
  38. // id format : u-<pk1>-<pk2>...
  39. type Cacher interface {
  40. GetIds(tableName, sql string) interface{}
  41. GetBean(tableName string, id string) interface{}
  42. PutIds(tableName, sql string, ids interface{})
  43. PutBean(tableName string, id string, obj interface{})
  44. DelIds(tableName, sql string)
  45. DelBean(tableName string, id string)
  46. ClearIds(tableName string)
  47. ClearBeans(tableName string)
  48. }
  49. func encodeIds(ids []PK) (string, error) {
  50. buf := new(bytes.Buffer)
  51. enc := gob.NewEncoder(buf)
  52. err := enc.Encode(ids)
  53. return buf.String(), err
  54. }
  55. func decodeIds(s string) ([]PK, error) {
  56. pks := make([]PK, 0)
  57. dec := gob.NewDecoder(strings.NewReader(s))
  58. err := dec.Decode(&pks)
  59. return pks, err
  60. }
  61. // GetCacheSql returns cacher PKs via SQL
  62. func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error) {
  63. bytes := m.GetIds(tableName, GenSqlKey(sql, args))
  64. if bytes == nil {
  65. return nil, errors.New("Not Exist")
  66. }
  67. return decodeIds(bytes.(string))
  68. }
  69. // PutCacheSql puts cacher SQL and PKs
  70. func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error {
  71. bytes, err := encodeIds(ids)
  72. if err != nil {
  73. return err
  74. }
  75. m.PutIds(tableName, GenSqlKey(sql, args), bytes)
  76. return nil
  77. }
  78. // GenSqlKey generates cache key
  79. func GenSqlKey(sql string, args interface{}) string {
  80. return fmt.Sprintf("%v-%v", sql, args)
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1