智盟项目
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.

423 lignes
8.1 KiB

  1. package cache
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. )
  8. const (
  9. redisPassword = "sanhu"
  10. redisDialTTL = 10 * time.Second
  11. redisReadTTL = 3 * time.Second
  12. redisWriteTTL = 3 * time.Second
  13. redisIdleTTL = 10 * time.Second
  14. redisPoolTTL = 10 * time.Second
  15. redisPoolSize int = 512
  16. redisMaxIdleConn int = 64
  17. redisMaxActive int = 512
  18. )
  19. var (
  20. ErrNil = errors.New("nil return")
  21. ErrWrongArgsNum = errors.New("args num error")
  22. ErrNegativeInt = errors.New("redis cluster: unexpected value for Uint64")
  23. )
  24. // 以下为提供类型转换
  25. func Int(reply interface{}, err error) (int, error) {
  26. if err != nil {
  27. return 0, err
  28. }
  29. switch reply := reply.(type) {
  30. case int:
  31. return reply, nil
  32. case int8:
  33. return int(reply), nil
  34. case int16:
  35. return int(reply), nil
  36. case int32:
  37. return int(reply), nil
  38. case int64:
  39. x := int(reply)
  40. if int64(x) != reply {
  41. return 0, strconv.ErrRange
  42. }
  43. return x, nil
  44. case uint:
  45. n := int(reply)
  46. if n < 0 {
  47. return 0, strconv.ErrRange
  48. }
  49. return n, nil
  50. case uint8:
  51. return int(reply), nil
  52. case uint16:
  53. return int(reply), nil
  54. case uint32:
  55. n := int(reply)
  56. if n < 0 {
  57. return 0, strconv.ErrRange
  58. }
  59. return n, nil
  60. case uint64:
  61. n := int(reply)
  62. if n < 0 {
  63. return 0, strconv.ErrRange
  64. }
  65. return n, nil
  66. case []byte:
  67. data := string(reply)
  68. if len(data) == 0 {
  69. return 0, ErrNil
  70. }
  71. n, err := strconv.ParseInt(data, 10, 0)
  72. return int(n), err
  73. case string:
  74. if len(reply) == 0 {
  75. return 0, ErrNil
  76. }
  77. n, err := strconv.ParseInt(reply, 10, 0)
  78. return int(n), err
  79. case nil:
  80. return 0, ErrNil
  81. case error:
  82. return 0, reply
  83. }
  84. return 0, fmt.Errorf("redis cluster: unexpected type for Int, got type %T", reply)
  85. }
  86. func Int64(reply interface{}, err error) (int64, error) {
  87. if err != nil {
  88. return 0, err
  89. }
  90. switch reply := reply.(type) {
  91. case int:
  92. return int64(reply), nil
  93. case int8:
  94. return int64(reply), nil
  95. case int16:
  96. return int64(reply), nil
  97. case int32:
  98. return int64(reply), nil
  99. case int64:
  100. return reply, nil
  101. case uint:
  102. n := int64(reply)
  103. if n < 0 {
  104. return 0, strconv.ErrRange
  105. }
  106. return n, nil
  107. case uint8:
  108. return int64(reply), nil
  109. case uint16:
  110. return int64(reply), nil
  111. case uint32:
  112. return int64(reply), nil
  113. case uint64:
  114. n := int64(reply)
  115. if n < 0 {
  116. return 0, strconv.ErrRange
  117. }
  118. return n, nil
  119. case []byte:
  120. data := string(reply)
  121. if len(data) == 0 {
  122. return 0, ErrNil
  123. }
  124. n, err := strconv.ParseInt(data, 10, 64)
  125. return n, err
  126. case string:
  127. if len(reply) == 0 {
  128. return 0, ErrNil
  129. }
  130. n, err := strconv.ParseInt(reply, 10, 64)
  131. return n, err
  132. case nil:
  133. return 0, ErrNil
  134. case error:
  135. return 0, reply
  136. }
  137. return 0, fmt.Errorf("redis cluster: unexpected type for Int64, got type %T", reply)
  138. }
  139. func Uint64(reply interface{}, err error) (uint64, error) {
  140. if err != nil {
  141. return 0, err
  142. }
  143. switch reply := reply.(type) {
  144. case uint:
  145. return uint64(reply), nil
  146. case uint8:
  147. return uint64(reply), nil
  148. case uint16:
  149. return uint64(reply), nil
  150. case uint32:
  151. return uint64(reply), nil
  152. case uint64:
  153. return reply, nil
  154. case int:
  155. if reply < 0 {
  156. return 0, ErrNegativeInt
  157. }
  158. return uint64(reply), nil
  159. case int8:
  160. if reply < 0 {
  161. return 0, ErrNegativeInt
  162. }
  163. return uint64(reply), nil
  164. case int16:
  165. if reply < 0 {
  166. return 0, ErrNegativeInt
  167. }
  168. return uint64(reply), nil
  169. case int32:
  170. if reply < 0 {
  171. return 0, ErrNegativeInt
  172. }
  173. return uint64(reply), nil
  174. case int64:
  175. if reply < 0 {
  176. return 0, ErrNegativeInt
  177. }
  178. return uint64(reply), nil
  179. case []byte:
  180. data := string(reply)
  181. if len(data) == 0 {
  182. return 0, ErrNil
  183. }
  184. n, err := strconv.ParseUint(data, 10, 64)
  185. return n, err
  186. case string:
  187. if len(reply) == 0 {
  188. return 0, ErrNil
  189. }
  190. n, err := strconv.ParseUint(reply, 10, 64)
  191. return n, err
  192. case nil:
  193. return 0, ErrNil
  194. case error:
  195. return 0, reply
  196. }
  197. return 0, fmt.Errorf("redis cluster: unexpected type for Uint64, got type %T", reply)
  198. }
  199. func Float64(reply interface{}, err error) (float64, error) {
  200. if err != nil {
  201. return 0, err
  202. }
  203. var value float64
  204. err = nil
  205. switch v := reply.(type) {
  206. case float32:
  207. value = float64(v)
  208. case float64:
  209. value = v
  210. case int:
  211. value = float64(v)
  212. case int8:
  213. value = float64(v)
  214. case int16:
  215. value = float64(v)
  216. case int32:
  217. value = float64(v)
  218. case int64:
  219. value = float64(v)
  220. case uint:
  221. value = float64(v)
  222. case uint8:
  223. value = float64(v)
  224. case uint16:
  225. value = float64(v)
  226. case uint32:
  227. value = float64(v)
  228. case uint64:
  229. value = float64(v)
  230. case []byte:
  231. data := string(v)
  232. if len(data) == 0 {
  233. return 0, ErrNil
  234. }
  235. value, err = strconv.ParseFloat(string(v), 64)
  236. case string:
  237. if len(v) == 0 {
  238. return 0, ErrNil
  239. }
  240. value, err = strconv.ParseFloat(v, 64)
  241. case nil:
  242. err = ErrNil
  243. case error:
  244. err = v
  245. default:
  246. err = fmt.Errorf("redis cluster: unexpected type for Float64, got type %T", v)
  247. }
  248. return value, err
  249. }
  250. func Bool(reply interface{}, err error) (bool, error) {
  251. if err != nil {
  252. return false, err
  253. }
  254. switch reply := reply.(type) {
  255. case bool:
  256. return reply, nil
  257. case int64:
  258. return reply != 0, nil
  259. case []byte:
  260. data := string(reply)
  261. if len(data) == 0 {
  262. return false, ErrNil
  263. }
  264. return strconv.ParseBool(data)
  265. case string:
  266. if len(reply) == 0 {
  267. return false, ErrNil
  268. }
  269. return strconv.ParseBool(reply)
  270. case nil:
  271. return false, ErrNil
  272. case error:
  273. return false, reply
  274. }
  275. return false, fmt.Errorf("redis cluster: unexpected type for Bool, got type %T", reply)
  276. }
  277. func Bytes(reply interface{}, err error) ([]byte, error) {
  278. if err != nil {
  279. return nil, err
  280. }
  281. switch reply := reply.(type) {
  282. case []byte:
  283. if len(reply) == 0 {
  284. return nil, ErrNil
  285. }
  286. return reply, nil
  287. case string:
  288. data := []byte(reply)
  289. if len(data) == 0 {
  290. return nil, ErrNil
  291. }
  292. return data, nil
  293. case nil:
  294. return nil, ErrNil
  295. case error:
  296. return nil, reply
  297. }
  298. return nil, fmt.Errorf("redis cluster: unexpected type for Bytes, got type %T", reply)
  299. }
  300. func String(reply interface{}, err error) (string, error) {
  301. if err != nil {
  302. return "", err
  303. }
  304. value := ""
  305. err = nil
  306. switch v := reply.(type) {
  307. case string:
  308. if len(v) == 0 {
  309. return "", ErrNil
  310. }
  311. value = v
  312. case []byte:
  313. if len(v) == 0 {
  314. return "", ErrNil
  315. }
  316. value = string(v)
  317. case int:
  318. value = strconv.FormatInt(int64(v), 10)
  319. case int8:
  320. value = strconv.FormatInt(int64(v), 10)
  321. case int16:
  322. value = strconv.FormatInt(int64(v), 10)
  323. case int32:
  324. value = strconv.FormatInt(int64(v), 10)
  325. case int64:
  326. value = strconv.FormatInt(v, 10)
  327. case uint:
  328. value = strconv.FormatUint(uint64(v), 10)
  329. case uint8:
  330. value = strconv.FormatUint(uint64(v), 10)
  331. case uint16:
  332. value = strconv.FormatUint(uint64(v), 10)
  333. case uint32:
  334. value = strconv.FormatUint(uint64(v), 10)
  335. case uint64:
  336. value = strconv.FormatUint(v, 10)
  337. case float32:
  338. value = strconv.FormatFloat(float64(v), 'f', -1, 32)
  339. case float64:
  340. value = strconv.FormatFloat(v, 'f', -1, 64)
  341. case bool:
  342. value = strconv.FormatBool(v)
  343. case nil:
  344. err = ErrNil
  345. case error:
  346. err = v
  347. default:
  348. err = fmt.Errorf("redis cluster: unexpected type for String, got type %T", v)
  349. }
  350. return value, err
  351. }
  352. func Strings(reply interface{}, err error) ([]string, error) {
  353. if err != nil {
  354. return nil, err
  355. }
  356. switch reply := reply.(type) {
  357. case []interface{}:
  358. result := make([]string, len(reply))
  359. for i := range reply {
  360. if reply[i] == nil {
  361. continue
  362. }
  363. switch subReply := reply[i].(type) {
  364. case string:
  365. result[i] = subReply
  366. case []byte:
  367. result[i] = string(subReply)
  368. default:
  369. return nil, fmt.Errorf("redis cluster: unexpected element type for String, got type %T", reply[i])
  370. }
  371. }
  372. return result, nil
  373. case []string:
  374. return reply, nil
  375. case nil:
  376. return nil, ErrNil
  377. case error:
  378. return nil, reply
  379. }
  380. return nil, fmt.Errorf("redis cluster: unexpected type for Strings, got type %T", reply)
  381. }
  382. func Values(reply interface{}, err error) ([]interface{}, error) {
  383. if err != nil {
  384. return nil, err
  385. }
  386. switch reply := reply.(type) {
  387. case []interface{}:
  388. return reply, nil
  389. case nil:
  390. return nil, ErrNil
  391. case error:
  392. return nil, reply
  393. }
  394. return nil, fmt.Errorf("redis cluster: unexpected type for Values, got type %T", reply)
  395. }