Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

redis.go 11 KiB

il y a 1 semaine
il y a 1 semaine
il y a 1 semaine
il y a 1 semaine
il y a 1 semaine
il y a 1 semaine
il y a 1 semaine
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package cache
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "strings"
  7. "time"
  8. redigo "github.com/gomodule/redigo/redis"
  9. )
  10. // configuration
  11. type Config struct {
  12. Server string
  13. Password string
  14. MaxIdle int // Maximum number of idle connections in the pool.
  15. // Maximum number of connections allocated by the pool at a given time.
  16. // When zero, there is no limit on the number of connections in the pool.
  17. MaxActive int
  18. // Close connections after remaining idle for this duration. If the value
  19. // is zero, then idle connections are not closed. Applications should set
  20. // the timeout to a value less than the server's timeout.
  21. IdleTimeout time.Duration
  22. // If Wait is true and the pool is at the MaxActive limit, then Get() waits
  23. // for a connection to be returned to the pool before returning.
  24. Wait bool
  25. KeyPrefix string // prefix to all keys; example is "dev environment name"
  26. KeyDelimiter string // delimiter to be used while appending keys; example is ":"
  27. KeyPlaceholder string // placeholder to be parsed using given arguments to obtain a final key; example is "?"
  28. }
  29. var pool *redigo.Pool
  30. var redisConn redigo.Conn
  31. var conf *Config
  32. func NewRedis(addr string) {
  33. if addr == "" {
  34. panic("\nredis connect string cannot be empty\n")
  35. }
  36. pool = &redigo.Pool{
  37. MaxIdle: redisMaxIdleConn,
  38. IdleTimeout: redisIdleTTL,
  39. MaxActive: redisMaxActive,
  40. // MaxConnLifetime: redisDialTTL,
  41. Wait: true,
  42. Dial: func() (redigo.Conn, error) {
  43. c, err := redigo.Dial("tcp", addr,
  44. redigo.DialConnectTimeout(redisDialTTL),
  45. redigo.DialReadTimeout(redisReadTTL),
  46. redigo.DialWriteTimeout(redisWriteTTL),
  47. )
  48. if err != nil {
  49. log.Println("Redis Dial failed: ", err)
  50. return nil, err
  51. }
  52. return c, err
  53. },
  54. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  55. _, err := c.Do("PING")
  56. if err != nil {
  57. log.Println("Unable to ping to redis server:", err)
  58. }
  59. return err
  60. },
  61. }
  62. conn := pool.Get()
  63. defer conn.Close()
  64. if conn.Err() != nil {
  65. println("\nredis connect " + addr + " error: " + conn.Err().Error())
  66. } else {
  67. println("\nredis connect " + addr + " success!\n")
  68. }
  69. }
  70. func NewRedisConn(conn redigo.Conn) {
  71. redisConn = conn
  72. }
  73. //
  74. //func Do(cmd string, args ...interface{}) (reply interface{}, err error) {
  75. // conn := pool.Get()
  76. // defer conn.Close()
  77. // return conn.Do(cmd, args...)
  78. //}
  79. func Do(cmd string, args ...interface{}) (reply interface{}, err error) {
  80. var conn = redisConn
  81. if redisConn == nil {
  82. conn = pool.Get()
  83. defer conn.Close()
  84. }
  85. return conn.Do(cmd, args...)
  86. }
  87. func GetPool() *redigo.Pool {
  88. return pool
  89. }
  90. func ParseKey(key string, vars []string) (string, error) {
  91. arr := strings.Split(key, conf.KeyPlaceholder)
  92. actualKey := ""
  93. if len(arr) != len(vars)+1 {
  94. return "", errors.New("redis/connection.go: Insufficient arguments to parse key")
  95. } else {
  96. for index, val := range arr {
  97. if index == 0 {
  98. actualKey = arr[index]
  99. } else {
  100. actualKey += vars[index-1] + val
  101. }
  102. }
  103. }
  104. return getPrefixedKey(actualKey), nil
  105. }
  106. func getPrefixedKey(key string) string {
  107. return conf.KeyPrefix + conf.KeyDelimiter + key
  108. }
  109. func StripEnvKey(key string) string {
  110. return strings.TrimLeft(key, conf.KeyPrefix+conf.KeyDelimiter)
  111. }
  112. func SplitKey(key string) []string {
  113. return strings.Split(key, conf.KeyDelimiter)
  114. }
  115. func Expire(key string, ttl int) (interface{}, error) {
  116. return Do("EXPIRE", key, ttl)
  117. }
  118. func Persist(key string) (interface{}, error) {
  119. return Do("PERSIST", key)
  120. }
  121. func Del(key string) (interface{}, error) {
  122. return Do("DEL", key)
  123. }
  124. func Set(key string, data interface{}) (interface{}, error) {
  125. // set
  126. return Do("SET", key, data)
  127. }
  128. func SetNX(key string, data interface{}) (interface{}, error) {
  129. return Do("SETNX", key, data)
  130. }
  131. func SetEx(key string, data interface{}, ttl int) (interface{}, error) {
  132. return Do("SETEX", key, ttl, data)
  133. }
  134. func SetJson(key string, data interface{}, ttl int) bool {
  135. c, err := json.Marshal(data)
  136. if err != nil {
  137. return false
  138. }
  139. if ttl < 1 {
  140. _, err = Set(key, c)
  141. } else {
  142. _, err = SetEx(key, c, ttl)
  143. }
  144. if err != nil {
  145. return false
  146. }
  147. return true
  148. }
  149. func GetJson(key string, dst interface{}) error {
  150. b, err := GetBytes(key)
  151. if err != nil {
  152. return err
  153. }
  154. if err = json.Unmarshal(b, dst); err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. func Get(key string) (interface{}, error) {
  160. // get
  161. return Do("GET", key)
  162. }
  163. func GetTTL(key string) (time.Duration, error) {
  164. ttl, err := redigo.Int64(Do("TTL", key))
  165. return time.Duration(ttl) * time.Second, err
  166. }
  167. func GetBytes(key string) ([]byte, error) {
  168. return redigo.Bytes(Do("GET", key))
  169. }
  170. func GetString(key string) (string, error) {
  171. return redigo.String(Do("GET", key))
  172. }
  173. func GetStringMap(key string) (map[string]string, error) {
  174. return redigo.StringMap(Do("GET", key))
  175. }
  176. func GetInt(key string) (int, error) {
  177. return redigo.Int(Do("GET", key))
  178. }
  179. func GetInt64(key string) (int64, error) {
  180. return redigo.Int64(Do("GET", key))
  181. }
  182. func GetStringLength(key string) (int, error) {
  183. return redigo.Int(Do("STRLEN", key))
  184. }
  185. func ZAdd(key string, score float64, data interface{}) (interface{}, error) {
  186. return Do("ZADD", key, score, data)
  187. }
  188. func ZAddNX(key string, score float64, data interface{}) (interface{}, error) {
  189. return Do("ZADD", key, "NX", score, data)
  190. }
  191. func ZRem(key string, data interface{}) (interface{}, error) {
  192. return Do("ZREM", key, data)
  193. }
  194. func ZRange(key string, start int, end int, withScores bool) ([]interface{}, error) {
  195. if withScores {
  196. return redigo.Values(Do("ZRANGE", key, start, end, "WITHSCORES"))
  197. }
  198. return redigo.Values(Do("ZRANGE", key, start, end))
  199. }
  200. func ZRemRangeByScore(key string, start int64, end int64) ([]interface{}, error) {
  201. return redigo.Values(Do("ZREMRANGEBYSCORE", key, start, end))
  202. }
  203. func ZCard(setName string) (int64, error) {
  204. return redigo.Int64(Do("ZCARD", setName))
  205. }
  206. func ZScan(setName string) (int64, error) {
  207. return redigo.Int64(Do("ZCARD", setName))
  208. }
  209. func SAdd(setName string, data interface{}) (interface{}, error) {
  210. return Do("SADD", setName, data)
  211. }
  212. func SCard(setName string) (int64, error) {
  213. return redigo.Int64(Do("SCARD", setName))
  214. }
  215. func SIsMember(setName string, data interface{}) (bool, error) {
  216. return redigo.Bool(Do("SISMEMBER", setName, data))
  217. }
  218. func SMembers(setName string) ([]string, error) {
  219. return redigo.Strings(Do("SMEMBERS", setName))
  220. }
  221. func SRem(setName string, data interface{}) (interface{}, error) {
  222. return Do("SREM", setName, data)
  223. }
  224. func HSet(key string, HKey string, data interface{}) (interface{}, error) {
  225. return Do("HSET", key, HKey, data)
  226. }
  227. func HGet(key string, HKey string) (interface{}, error) {
  228. return Do("HGET", key, HKey)
  229. }
  230. func HMGet(key string, hashKeys ...string) ([]interface{}, error) {
  231. ret, err := Do("HMGET", key, hashKeys)
  232. if err != nil {
  233. return nil, err
  234. }
  235. reta, ok := ret.([]interface{})
  236. if !ok {
  237. return nil, errors.New("result not an array")
  238. }
  239. return reta, nil
  240. }
  241. func HMSet(key string, hashKeys []string, vals []interface{}) (interface{}, error) {
  242. if len(hashKeys) == 0 || len(hashKeys) != len(vals) {
  243. var ret interface{}
  244. return ret, errors.New("bad length")
  245. }
  246. input := []interface{}{key}
  247. for i, v := range hashKeys {
  248. input = append(input, v, vals[i])
  249. }
  250. return Do("HMSET", input...)
  251. }
  252. func HGetString(key string, HKey string) (string, error) {
  253. return redigo.String(Do("HGET", key, HKey))
  254. }
  255. func HGetFloat(key string, HKey string) (float64, error) {
  256. f, err := redigo.Float64(Do("HGET", key, HKey))
  257. return f, err
  258. }
  259. func HGetInt(key string, HKey string) (int, error) {
  260. return redigo.Int(Do("HGET", key, HKey))
  261. }
  262. func HGetInt64(key string, HKey string) (int64, error) {
  263. return redigo.Int64(Do("HGET", key, HKey))
  264. }
  265. func HGetBool(key string, HKey string) (bool, error) {
  266. return redigo.Bool(Do("HGET", key, HKey))
  267. }
  268. func HDel(key string, HKey string) (interface{}, error) {
  269. return Do("HDEL", key, HKey)
  270. }
  271. func HGetAll(key string) (map[string]interface{}, error) {
  272. vals, err := redigo.Values(Do("HGETALL", key))
  273. if err != nil {
  274. return nil, err
  275. }
  276. num := len(vals) / 2
  277. result := make(map[string]interface{}, num)
  278. for i := 0; i < num; i++ {
  279. key, _ := redigo.String(vals[2*i], nil)
  280. result[key] = vals[2*i+1]
  281. }
  282. return result, nil
  283. }
  284. func FlushAll() bool {
  285. res, _ := redigo.String(Do("FLUSHALL"))
  286. if res == "" {
  287. return false
  288. }
  289. return true
  290. }
  291. // NOTE: Use this in production environment with extreme care.
  292. // Read more here:https://redigo.io/commands/keys
  293. func Keys(pattern string) ([]string, error) {
  294. return redigo.Strings(Do("KEYS", pattern))
  295. }
  296. func HKeys(key string) ([]string, error) {
  297. return redigo.Strings(Do("HKEYS", key))
  298. }
  299. func Exists(key string) bool {
  300. count, err := redigo.Int(Do("EXISTS", key))
  301. if count == 0 || err != nil {
  302. return false
  303. }
  304. return true
  305. }
  306. func Incr(key string) (int64, error) {
  307. return redigo.Int64(Do("INCR", key))
  308. }
  309. func Decr(key string) (int64, error) {
  310. return redigo.Int64(Do("DECR", key))
  311. }
  312. func IncrBy(key string, incBy int64) (int64, error) {
  313. return redigo.Int64(Do("INCRBY", key, incBy))
  314. }
  315. func DecrBy(key string, decrBy int64) (int64, error) {
  316. return redigo.Int64(Do("DECRBY", key))
  317. }
  318. func IncrByFloat(key string, incBy float64) (float64, error) {
  319. return redigo.Float64(Do("INCRBYFLOAT", key, incBy))
  320. }
  321. func DecrByFloat(key string, decrBy float64) (float64, error) {
  322. return redigo.Float64(Do("DECRBYFLOAT", key, decrBy))
  323. }
  324. // use for message queue
  325. func LPush(key string, data interface{}) (interface{}, error) {
  326. // set
  327. return Do("LPUSH", key, data)
  328. }
  329. func LPop(key string) (interface{}, error) {
  330. return Do("LPOP", key)
  331. }
  332. func LPopString(key string) (string, error) {
  333. return redigo.String(Do("LPOP", key))
  334. }
  335. func LPopFloat(key string) (float64, error) {
  336. f, err := redigo.Float64(Do("LPOP", key))
  337. return f, err
  338. }
  339. func LPopInt(key string) (int, error) {
  340. return redigo.Int(Do("LPOP", key))
  341. }
  342. func LPopInt64(key string) (int64, error) {
  343. return redigo.Int64(Do("LPOP", key))
  344. }
  345. func RPush(key string, data interface{}) (interface{}, error) {
  346. // set
  347. return Do("RPUSH", key, data)
  348. }
  349. func RPop(key string) (interface{}, error) {
  350. return Do("RPOP", key)
  351. }
  352. func RPopString(key string) (string, error) {
  353. return redigo.String(Do("RPOP", key))
  354. }
  355. func RPopFloat(key string) (float64, error) {
  356. f, err := redigo.Float64(Do("RPOP", key))
  357. return f, err
  358. }
  359. func RPopInt(key string) (int, error) {
  360. return redigo.Int(Do("RPOP", key))
  361. }
  362. func RPopInt64(key string) (int64, error) {
  363. return redigo.Int64(Do("RPOP", key))
  364. }
  365. func Scan(cursor int64, pattern string, count int64) (int64, []string, error) {
  366. var items []string
  367. var newCursor int64
  368. values, err := redigo.Values(Do("SCAN", cursor, "MATCH", pattern, "COUNT", count))
  369. if err != nil {
  370. return 0, nil, err
  371. }
  372. values, err = redigo.Scan(values, &newCursor, &items)
  373. if err != nil {
  374. return 0, nil, err
  375. }
  376. return newCursor, items, nil
  377. }
  378. func LPushMax(key string, data ...interface{}) (interface{}, error) {
  379. // set
  380. return Do("LPUSH", key, data)
  381. }
  382. func SelectDb(db int) (interface{}, error) {
  383. return Do("SELECT", db)
  384. }