cache.go 2.5 KB

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