智慧食堂
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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