智慧食堂
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

108 行
2.5 KiB

  1. package cache
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. var c Cache
  7. type Cache interface {
  8. // get cached value by key.
  9. Get(key string) interface{}
  10. // GetMulti is a batch version of Get.
  11. GetMulti(keys []string) []interface{}
  12. // set cached value with key and expire time.
  13. Put(key string, val interface{}, timeout time.Duration) error
  14. // delete cached value by key.
  15. Delete(key string) error
  16. // increase cached int value by key, as a counter.
  17. Incr(key string) error
  18. // decrease cached int value by key, as a counter.
  19. Decr(key string) error
  20. // check if cached value exists or not.
  21. IsExist(key string) bool
  22. // clear all cache.
  23. ClearAll() error
  24. // start gc routine based on config string settings.
  25. StartAndGC(config string) error
  26. }
  27. // Instance is a function create a new Cache Instance
  28. type Instance func() Cache
  29. var adapters = make(map[string]Instance)
  30. // Register makes a cache adapter available by the adapter name.
  31. // If Register is called twice with the same name or if driver is nil,
  32. // it panics.
  33. func Register(name string, adapter Instance) {
  34. if adapter == nil {
  35. panic("cache: Register adapter is nil")
  36. }
  37. if _, ok := adapters[name]; ok {
  38. panic("cache: Register called twice for adapter " + name)
  39. }
  40. adapters[name] = adapter
  41. }
  42. // NewCache Create a new cache driver by adapter name and config string.
  43. // config need to be correct JSON as string: {"interval":360}.
  44. // it will start gc automatically.
  45. func NewCache(adapterName, config string) (adapter Cache, err error) {
  46. instanceFunc, ok := adapters[adapterName]
  47. if !ok {
  48. err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
  49. return
  50. }
  51. adapter = instanceFunc()
  52. err = adapter.StartAndGC(config)
  53. if err != nil {
  54. adapter = nil
  55. }
  56. return
  57. }
  58. func InitCache(adapterName, config string) (err error) {
  59. instanceFunc, ok := adapters[adapterName]
  60. if !ok {
  61. err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
  62. return
  63. }
  64. c = instanceFunc()
  65. err = c.StartAndGC(config)
  66. if err != nil {
  67. c = nil
  68. }
  69. return
  70. }
  71. func Get(key string) interface{} {
  72. return c.Get(key)
  73. }
  74. func GetMulti(keys []string) []interface{} {
  75. return c.GetMulti(keys)
  76. }
  77. func Put(key string, val interface{}, ttl time.Duration) error {
  78. return c.Put(key, val, ttl)
  79. }
  80. func Delete(key string) error {
  81. return c.Delete(key)
  82. }
  83. func Incr(key string) error {
  84. return c.Incr(key)
  85. }
  86. func Decr(key string) error {
  87. return c.Decr(key)
  88. }
  89. func IsExist(key string) bool {
  90. return c.IsExist(key)
  91. }
  92. func ClearAll() error {
  93. return c.ClearAll()
  94. }
  95. func StartAndGC(cfg string) error {
  96. return c.StartAndGC(cfg)
  97. }