cache.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package cache provide a Cache interface and some implemetn engine
  15. // Usage:
  16. //
  17. // import(
  18. // "github.com/astaxie/beego/cache"
  19. // )
  20. //
  21. // bm, err := cache.NewCache("memory", `{"interval":60}`)
  22. //
  23. // Use it like this:
  24. //
  25. // bm.Put("astaxie", 1, 10 * time.Second)
  26. // bm.Get("astaxie")
  27. // bm.IsExist("astaxie")
  28. // bm.Delete("astaxie")
  29. //
  30. // more docs http://beego.me/docs/module/cache.md
  31. package cache
  32. import (
  33. "fmt"
  34. "time"
  35. )
  36. // Cache interface contains all behaviors for cache adapter.
  37. // usage:
  38. // cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
  39. // c,err := cache.NewCache("file","{....}")
  40. // c.Put("key",value, 3600 * time.Second)
  41. // v := c.Get("key")
  42. //
  43. // c.Incr("counter") // now is 1
  44. // c.Incr("counter") // now is 2
  45. // count := c.Get("counter").(int)
  46. type Cache interface {
  47. // get cached value by key.
  48. Get(key string) interface{}
  49. // GetMulti is a batch version of Get.
  50. GetMulti(keys []string) []interface{}
  51. // set cached value with key and expire time.
  52. Put(key string, val interface{}, timeout time.Duration) error
  53. // delete cached value by key.
  54. Delete(key string) error
  55. // increase cached int value by key, as a counter.
  56. Incr(key string) error
  57. // decrease cached int value by key, as a counter.
  58. Decr(key string) error
  59. // check if cached value exists or not.
  60. IsExist(key string) bool
  61. // clear all cache.
  62. ClearAll() error
  63. // start gc routine based on config string settings.
  64. StartAndGC(config string) error
  65. }
  66. // Instance is a function create a new Cache Instance
  67. type Instance func() Cache
  68. var adapters = make(map[string]Instance)
  69. // Register makes a cache adapter available by the adapter name.
  70. // If Register is called twice with the same name or if driver is nil,
  71. // it panics.
  72. func Register(name string, adapter Instance) {
  73. if adapter == nil {
  74. panic("cache: Register adapter is nil")
  75. }
  76. if _, ok := adapters[name]; ok {
  77. panic("cache: Register called twice for adapter " + name)
  78. }
  79. adapters[name] = adapter
  80. }
  81. // NewCache Create a new cache driver by adapter name and config string.
  82. // config need to be correct JSON as string: {"interval":360}.
  83. // it will start gc automatically.
  84. func NewCache(adapterName, config string) (adapter Cache, err error) {
  85. instanceFunc, ok := adapters[adapterName]
  86. if !ok {
  87. err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
  88. return
  89. }
  90. adapter = instanceFunc()
  91. err = adapter.StartAndGC(config)
  92. if err != nil {
  93. adapter = nil
  94. }
  95. return
  96. }