ssdb.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package ssdb
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/ssdb/gossdb/ssdb"
  9. "github.com/astaxie/beego/cache"
  10. )
  11. // Cache SSDB adapter
  12. type Cache struct {
  13. conn *ssdb.Client
  14. conninfo []string
  15. }
  16. //NewSsdbCache create new ssdb adapter.
  17. func NewSsdbCache() cache.Cache {
  18. return &Cache{}
  19. }
  20. // Get get value from memcache.
  21. func (rc *Cache) Get(key string) interface{} {
  22. if rc.conn == nil {
  23. if err := rc.connectInit(); err != nil {
  24. return nil
  25. }
  26. }
  27. value, err := rc.conn.Get(key)
  28. if err == nil {
  29. return value
  30. }
  31. return nil
  32. }
  33. // GetMulti get value from memcache.
  34. func (rc *Cache) GetMulti(keys []string) []interface{} {
  35. size := len(keys)
  36. var values []interface{}
  37. if rc.conn == nil {
  38. if err := rc.connectInit(); err != nil {
  39. for i := 0; i < size; i++ {
  40. values = append(values, err)
  41. }
  42. return values
  43. }
  44. }
  45. res, err := rc.conn.Do("multi_get", keys)
  46. resSize := len(res)
  47. if err == nil {
  48. for i := 1; i < resSize; i += 2 {
  49. values = append(values, string(res[i+1]))
  50. }
  51. return values
  52. }
  53. for i := 0; i < size; i++ {
  54. values = append(values, err)
  55. }
  56. return values
  57. }
  58. // DelMulti get value from memcache.
  59. func (rc *Cache) DelMulti(keys []string) error {
  60. if rc.conn == nil {
  61. if err := rc.connectInit(); err != nil {
  62. return err
  63. }
  64. }
  65. _, err := rc.conn.Do("multi_del", keys)
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. // Put put value to memcache. only support string.
  72. func (rc *Cache) Put(key string, value interface{}, timeout time.Duration) error {
  73. if rc.conn == nil {
  74. if err := rc.connectInit(); err != nil {
  75. return err
  76. }
  77. }
  78. v, ok := value.(string)
  79. if !ok {
  80. return errors.New("value must string")
  81. }
  82. var resp []string
  83. var err error
  84. ttl := int(timeout / time.Second)
  85. if ttl < 0 {
  86. resp, err = rc.conn.Do("set", key, v)
  87. } else {
  88. resp, err = rc.conn.Do("setx", key, v, ttl)
  89. }
  90. if err != nil {
  91. return err
  92. }
  93. if len(resp) == 2 && resp[0] == "ok" {
  94. return nil
  95. }
  96. return errors.New("bad response")
  97. }
  98. // Delete delete value in memcache.
  99. func (rc *Cache) Delete(key string) error {
  100. if rc.conn == nil {
  101. if err := rc.connectInit(); err != nil {
  102. return err
  103. }
  104. }
  105. _, err := rc.conn.Del(key)
  106. if err != nil {
  107. return err
  108. }
  109. return nil
  110. }
  111. // Incr increase counter.
  112. func (rc *Cache) Incr(key string) error {
  113. if rc.conn == nil {
  114. if err := rc.connectInit(); err != nil {
  115. return err
  116. }
  117. }
  118. _, err := rc.conn.Do("incr", key, 1)
  119. return err
  120. }
  121. // Decr decrease counter.
  122. func (rc *Cache) Decr(key string) error {
  123. if rc.conn == nil {
  124. if err := rc.connectInit(); err != nil {
  125. return err
  126. }
  127. }
  128. _, err := rc.conn.Do("incr", key, -1)
  129. return err
  130. }
  131. // IsExist check value exists in memcache.
  132. func (rc *Cache) IsExist(key string) bool {
  133. if rc.conn == nil {
  134. if err := rc.connectInit(); err != nil {
  135. return false
  136. }
  137. }
  138. resp, err := rc.conn.Do("exists", key)
  139. if err != nil {
  140. return false
  141. }
  142. if len(resp) == 2 && resp[1] == "1" {
  143. return true
  144. }
  145. return false
  146. }
  147. // ClearAll clear all cached in memcache.
  148. func (rc *Cache) ClearAll() error {
  149. if rc.conn == nil {
  150. if err := rc.connectInit(); err != nil {
  151. return err
  152. }
  153. }
  154. keyStart, keyEnd, limit := "", "", 50
  155. resp, err := rc.Scan(keyStart, keyEnd, limit)
  156. for err == nil {
  157. size := len(resp)
  158. if size == 1 {
  159. return nil
  160. }
  161. keys := []string{}
  162. for i := 1; i < size; i += 2 {
  163. keys = append(keys, string(resp[i]))
  164. }
  165. _, e := rc.conn.Do("multi_del", keys)
  166. if e != nil {
  167. return e
  168. }
  169. keyStart = resp[size-2]
  170. resp, err = rc.Scan(keyStart, keyEnd, limit)
  171. }
  172. return err
  173. }
  174. // Scan key all cached in ssdb.
  175. func (rc *Cache) Scan(keyStart string, keyEnd string, limit int) ([]string, error) {
  176. if rc.conn == nil {
  177. if err := rc.connectInit(); err != nil {
  178. return nil, err
  179. }
  180. }
  181. resp, err := rc.conn.Do("scan", keyStart, keyEnd, limit)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return resp, nil
  186. }
  187. // StartAndGC start memcache adapter.
  188. // config string is like {"conn":"connection info"}.
  189. // if connecting error, return.
  190. func (rc *Cache) StartAndGC(config string) error {
  191. var cf map[string]string
  192. json.Unmarshal([]byte(config), &cf)
  193. if _, ok := cf["conn"]; !ok {
  194. return errors.New("config has no conn key")
  195. }
  196. rc.conninfo = strings.Split(cf["conn"], ";")
  197. if rc.conn == nil {
  198. if err := rc.connectInit(); err != nil {
  199. return err
  200. }
  201. }
  202. return nil
  203. }
  204. // connect to memcache and keep the connection.
  205. func (rc *Cache) connectInit() error {
  206. conninfoArray := strings.Split(rc.conninfo[0], ":")
  207. host := conninfoArray[0]
  208. port, e := strconv.Atoi(conninfoArray[1])
  209. if e != nil {
  210. return e
  211. }
  212. var err error
  213. rc.conn, err = ssdb.Connect(host, port)
  214. if err != nil {
  215. return err
  216. }
  217. return nil
  218. }
  219. func init() {
  220. cache.Register("ssdb", NewSsdbCache)
  221. }