1
0

cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package runtime
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/chanxuehong/wechat/oauth2"
  6. "gogs.baozhida.cn/zoie/OAuth-core/storage"
  7. )
  8. const (
  9. intervalTenant = "/"
  10. )
  11. // NewCache 创建对应上下文缓存
  12. func NewCache(prefix string, store storage.AdapterCache, wxTokenStoreKey string) storage.AdapterCache {
  13. if wxTokenStoreKey == "" {
  14. wxTokenStoreKey = "wx_token_store_key"
  15. }
  16. return &Cache{
  17. prefix: prefix,
  18. store: store,
  19. wxTokenStoreKey: wxTokenStoreKey,
  20. }
  21. }
  22. type Cache struct {
  23. prefix string
  24. store storage.AdapterCache
  25. wxTokenStoreKey string
  26. }
  27. // String string输出
  28. func (e *Cache) String() string {
  29. if e.store == nil {
  30. return ""
  31. }
  32. return e.store.String()
  33. }
  34. // SetPrefix 设置前缀
  35. func (e *Cache) SetPrefix(prefix string) {
  36. e.prefix = prefix
  37. }
  38. // Connect 初始化
  39. func (e Cache) Connect() error {
  40. return nil
  41. //return e.store.Connect()
  42. }
  43. // Get val in cache
  44. func (e Cache) Get(key string) (string, error) {
  45. return e.store.Get(e.prefix + intervalTenant + key)
  46. }
  47. // Set val in cache
  48. func (e Cache) Set(key string, val interface{}, expire int) error {
  49. return e.store.Set(e.prefix+intervalTenant+key, val, expire)
  50. }
  51. // Del delete key in cache
  52. func (e Cache) Del(key string) error {
  53. return e.store.Del(e.prefix + intervalTenant + key)
  54. }
  55. // HashGet get val in hashtable cache
  56. func (e Cache) HashGet(hk, key string) (string, error) {
  57. return e.store.HashGet(hk, e.prefix+intervalTenant+key)
  58. }
  59. // HashDel delete one key:value pair in hashtable cache
  60. func (e Cache) HashDel(hk, key string) error {
  61. return e.store.HashDel(hk, e.prefix+intervalTenant+key)
  62. }
  63. // Increase value
  64. func (e Cache) Increase(key string) error {
  65. return e.store.Increase(e.prefix + intervalTenant + key)
  66. }
  67. func (e Cache) Decrease(key string) error {
  68. return e.store.Decrease(e.prefix + intervalTenant + key)
  69. }
  70. func (e Cache) Expire(key string, dur time.Duration) error {
  71. return e.store.Expire(e.prefix+intervalTenant+key, dur)
  72. }
  73. // Token 获取微信oauth2 token
  74. func (e Cache) Token() (token *oauth2.Token, err error) {
  75. var str string
  76. str, err = e.store.Get(e.prefix + intervalTenant + e.wxTokenStoreKey)
  77. if err != nil {
  78. return
  79. }
  80. err = json.Unmarshal([]byte(str), token)
  81. return
  82. }
  83. // PutToken 设置微信oauth2 token
  84. func (e Cache) PutToken(token *oauth2.Token) error {
  85. rb, err := json.Marshal(token)
  86. if err != nil {
  87. return err
  88. }
  89. return e.store.Set(e.prefix+intervalTenant+e.wxTokenStoreKey, string(rb), int(token.ExpiresIn)-200)
  90. }