memcache.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013 Beego Authors
  2. // Copyright 2014 The Macaron Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package cache
  16. import (
  17. "strings"
  18. "github.com/Unknwon/com"
  19. "github.com/bradfitz/gomemcache/memcache"
  20. "github.com/go-macaron/cache"
  21. )
  22. // MemcacheCacher represents a memcache cache adapter implementation.
  23. type MemcacheCacher struct {
  24. c *memcache.Client
  25. }
  26. func NewItem(key string, data []byte, expire int32) *memcache.Item {
  27. return &memcache.Item{
  28. Key: key,
  29. Value: data,
  30. Expiration: expire,
  31. }
  32. }
  33. // Put puts value into cache with key and expire time.
  34. // If expired is 0, it lives forever.
  35. func (c *MemcacheCacher) Put(key string, val interface{}, expire int64) error {
  36. return c.c.Set(NewItem(key, []byte(com.ToStr(val)), int32(expire)))
  37. }
  38. // Get gets cached value by given key.
  39. func (c *MemcacheCacher) Get(key string) interface{} {
  40. item, err := c.c.Get(key)
  41. if err != nil {
  42. return nil
  43. }
  44. return string(item.Value)
  45. }
  46. // Delete deletes cached value by given key.
  47. func (c *MemcacheCacher) Delete(key string) error {
  48. return c.c.Delete(key)
  49. }
  50. // Incr increases cached int-type value by given key as a counter.
  51. func (c *MemcacheCacher) Incr(key string) error {
  52. _, err := c.c.Increment(key, 1)
  53. return err
  54. }
  55. // Decr decreases cached int-type value by given key as a counter.
  56. func (c *MemcacheCacher) Decr(key string) error {
  57. _, err := c.c.Decrement(key, 1)
  58. return err
  59. }
  60. // IsExist returns true if cached value exists.
  61. func (c *MemcacheCacher) IsExist(key string) bool {
  62. _, err := c.c.Get(key)
  63. return err == nil
  64. }
  65. // Flush deletes all cached data.
  66. func (c *MemcacheCacher) Flush() error {
  67. return c.c.FlushAll()
  68. }
  69. // StartAndGC starts GC routine based on config string settings.
  70. // AdapterConfig: 127.0.0.1:9090;127.0.0.1:9091
  71. func (c *MemcacheCacher) StartAndGC(opt cache.Options) error {
  72. c.c = memcache.New(strings.Split(opt.AdapterConfig, ";")...)
  73. return nil
  74. }
  75. func init() {
  76. cache.Register("memcache", &MemcacheCacher{})
  77. }