Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

393 wiersze
11 KiB

  1. package redis
  2. import (
  3. "context"
  4. "time"
  5. "github.com/redis/go-redis/v9/internal/hashtag"
  6. )
  7. type GenericCmdable interface {
  8. Del(ctx context.Context, keys ...string) *IntCmd
  9. Dump(ctx context.Context, key string) *StringCmd
  10. Exists(ctx context.Context, keys ...string) *IntCmd
  11. Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  12. ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
  13. ExpireTime(ctx context.Context, key string) *DurationCmd
  14. ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  15. ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  16. ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  17. ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  18. Keys(ctx context.Context, pattern string) *StringSliceCmd
  19. Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd
  20. Move(ctx context.Context, key string, db int) *BoolCmd
  21. ObjectFreq(ctx context.Context, key string) *IntCmd
  22. ObjectRefCount(ctx context.Context, key string) *IntCmd
  23. ObjectEncoding(ctx context.Context, key string) *StringCmd
  24. ObjectIdleTime(ctx context.Context, key string) *DurationCmd
  25. Persist(ctx context.Context, key string) *BoolCmd
  26. PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
  27. PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
  28. PExpireTime(ctx context.Context, key string) *DurationCmd
  29. PTTL(ctx context.Context, key string) *DurationCmd
  30. RandomKey(ctx context.Context) *StringCmd
  31. Rename(ctx context.Context, key, newkey string) *StatusCmd
  32. RenameNX(ctx context.Context, key, newkey string) *BoolCmd
  33. Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
  34. RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
  35. Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd
  36. SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd
  37. SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd
  38. SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd
  39. Touch(ctx context.Context, keys ...string) *IntCmd
  40. TTL(ctx context.Context, key string) *DurationCmd
  41. Type(ctx context.Context, key string) *StatusCmd
  42. Copy(ctx context.Context, sourceKey string, destKey string, db int, replace bool) *IntCmd
  43. Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd
  44. ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd
  45. }
  46. func (c cmdable) Del(ctx context.Context, keys ...string) *IntCmd {
  47. args := make([]interface{}, 1+len(keys))
  48. args[0] = "del"
  49. for i, key := range keys {
  50. args[1+i] = key
  51. }
  52. cmd := NewIntCmd(ctx, args...)
  53. _ = c(ctx, cmd)
  54. return cmd
  55. }
  56. func (c cmdable) Unlink(ctx context.Context, keys ...string) *IntCmd {
  57. args := make([]interface{}, 1+len(keys))
  58. args[0] = "unlink"
  59. for i, key := range keys {
  60. args[1+i] = key
  61. }
  62. cmd := NewIntCmd(ctx, args...)
  63. _ = c(ctx, cmd)
  64. return cmd
  65. }
  66. func (c cmdable) Dump(ctx context.Context, key string) *StringCmd {
  67. cmd := NewStringCmd(ctx, "dump", key)
  68. _ = c(ctx, cmd)
  69. return cmd
  70. }
  71. func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
  72. args := make([]interface{}, 1+len(keys))
  73. args[0] = "exists"
  74. for i, key := range keys {
  75. args[1+i] = key
  76. }
  77. cmd := NewIntCmd(ctx, args...)
  78. _ = c(ctx, cmd)
  79. return cmd
  80. }
  81. func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  82. return c.expire(ctx, key, expiration, "")
  83. }
  84. func (c cmdable) ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  85. return c.expire(ctx, key, expiration, "NX")
  86. }
  87. func (c cmdable) ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  88. return c.expire(ctx, key, expiration, "XX")
  89. }
  90. func (c cmdable) ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  91. return c.expire(ctx, key, expiration, "GT")
  92. }
  93. func (c cmdable) ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  94. return c.expire(ctx, key, expiration, "LT")
  95. }
  96. func (c cmdable) expire(
  97. ctx context.Context, key string, expiration time.Duration, mode string,
  98. ) *BoolCmd {
  99. args := make([]interface{}, 3, 4)
  100. args[0] = "expire"
  101. args[1] = key
  102. args[2] = formatSec(ctx, expiration)
  103. if mode != "" {
  104. args = append(args, mode)
  105. }
  106. cmd := NewBoolCmd(ctx, args...)
  107. _ = c(ctx, cmd)
  108. return cmd
  109. }
  110. func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
  111. cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
  112. _ = c(ctx, cmd)
  113. return cmd
  114. }
  115. func (c cmdable) ExpireTime(ctx context.Context, key string) *DurationCmd {
  116. cmd := NewDurationCmd(ctx, time.Second, "expiretime", key)
  117. _ = c(ctx, cmd)
  118. return cmd
  119. }
  120. func (c cmdable) Keys(ctx context.Context, pattern string) *StringSliceCmd {
  121. cmd := NewStringSliceCmd(ctx, "keys", pattern)
  122. _ = c(ctx, cmd)
  123. return cmd
  124. }
  125. func (c cmdable) Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd {
  126. cmd := NewStatusCmd(
  127. ctx,
  128. "migrate",
  129. host,
  130. port,
  131. key,
  132. db,
  133. formatMs(ctx, timeout),
  134. )
  135. cmd.setReadTimeout(timeout)
  136. _ = c(ctx, cmd)
  137. return cmd
  138. }
  139. func (c cmdable) Move(ctx context.Context, key string, db int) *BoolCmd {
  140. cmd := NewBoolCmd(ctx, "move", key, db)
  141. _ = c(ctx, cmd)
  142. return cmd
  143. }
  144. func (c cmdable) ObjectFreq(ctx context.Context, key string) *IntCmd {
  145. cmd := NewIntCmd(ctx, "object", "freq", key)
  146. _ = c(ctx, cmd)
  147. return cmd
  148. }
  149. func (c cmdable) ObjectRefCount(ctx context.Context, key string) *IntCmd {
  150. cmd := NewIntCmd(ctx, "object", "refcount", key)
  151. _ = c(ctx, cmd)
  152. return cmd
  153. }
  154. func (c cmdable) ObjectEncoding(ctx context.Context, key string) *StringCmd {
  155. cmd := NewStringCmd(ctx, "object", "encoding", key)
  156. _ = c(ctx, cmd)
  157. return cmd
  158. }
  159. func (c cmdable) ObjectIdleTime(ctx context.Context, key string) *DurationCmd {
  160. cmd := NewDurationCmd(ctx, time.Second, "object", "idletime", key)
  161. _ = c(ctx, cmd)
  162. return cmd
  163. }
  164. func (c cmdable) Persist(ctx context.Context, key string) *BoolCmd {
  165. cmd := NewBoolCmd(ctx, "persist", key)
  166. _ = c(ctx, cmd)
  167. return cmd
  168. }
  169. func (c cmdable) PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
  170. cmd := NewBoolCmd(ctx, "pexpire", key, formatMs(ctx, expiration))
  171. _ = c(ctx, cmd)
  172. return cmd
  173. }
  174. func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
  175. cmd := NewBoolCmd(
  176. ctx,
  177. "pexpireat",
  178. key,
  179. tm.UnixNano()/int64(time.Millisecond),
  180. )
  181. _ = c(ctx, cmd)
  182. return cmd
  183. }
  184. func (c cmdable) PExpireTime(ctx context.Context, key string) *DurationCmd {
  185. cmd := NewDurationCmd(ctx, time.Millisecond, "pexpiretime", key)
  186. _ = c(ctx, cmd)
  187. return cmd
  188. }
  189. func (c cmdable) PTTL(ctx context.Context, key string) *DurationCmd {
  190. cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key)
  191. _ = c(ctx, cmd)
  192. return cmd
  193. }
  194. func (c cmdable) RandomKey(ctx context.Context) *StringCmd {
  195. cmd := NewStringCmd(ctx, "randomkey")
  196. _ = c(ctx, cmd)
  197. return cmd
  198. }
  199. func (c cmdable) Rename(ctx context.Context, key, newkey string) *StatusCmd {
  200. cmd := NewStatusCmd(ctx, "rename", key, newkey)
  201. _ = c(ctx, cmd)
  202. return cmd
  203. }
  204. func (c cmdable) RenameNX(ctx context.Context, key, newkey string) *BoolCmd {
  205. cmd := NewBoolCmd(ctx, "renamenx", key, newkey)
  206. _ = c(ctx, cmd)
  207. return cmd
  208. }
  209. func (c cmdable) Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd {
  210. cmd := NewStatusCmd(
  211. ctx,
  212. "restore",
  213. key,
  214. formatMs(ctx, ttl),
  215. value,
  216. )
  217. _ = c(ctx, cmd)
  218. return cmd
  219. }
  220. func (c cmdable) RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd {
  221. cmd := NewStatusCmd(
  222. ctx,
  223. "restore",
  224. key,
  225. formatMs(ctx, ttl),
  226. value,
  227. "replace",
  228. )
  229. _ = c(ctx, cmd)
  230. return cmd
  231. }
  232. type Sort struct {
  233. By string
  234. Offset, Count int64
  235. Get []string
  236. Order string
  237. Alpha bool
  238. }
  239. func (sort *Sort) args(command, key string) []interface{} {
  240. args := []interface{}{command, key}
  241. if sort.By != "" {
  242. args = append(args, "by", sort.By)
  243. }
  244. if sort.Offset != 0 || sort.Count != 0 {
  245. args = append(args, "limit", sort.Offset, sort.Count)
  246. }
  247. for _, get := range sort.Get {
  248. args = append(args, "get", get)
  249. }
  250. if sort.Order != "" {
  251. args = append(args, sort.Order)
  252. }
  253. if sort.Alpha {
  254. args = append(args, "alpha")
  255. }
  256. return args
  257. }
  258. func (c cmdable) SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
  259. cmd := NewStringSliceCmd(ctx, sort.args("sort_ro", key)...)
  260. _ = c(ctx, cmd)
  261. return cmd
  262. }
  263. func (c cmdable) Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
  264. cmd := NewStringSliceCmd(ctx, sort.args("sort", key)...)
  265. _ = c(ctx, cmd)
  266. return cmd
  267. }
  268. func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd {
  269. args := sort.args("sort", key)
  270. if store != "" {
  271. args = append(args, "store", store)
  272. }
  273. cmd := NewIntCmd(ctx, args...)
  274. _ = c(ctx, cmd)
  275. return cmd
  276. }
  277. func (c cmdable) SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd {
  278. cmd := NewSliceCmd(ctx, sort.args("sort", key)...)
  279. _ = c(ctx, cmd)
  280. return cmd
  281. }
  282. func (c cmdable) Touch(ctx context.Context, keys ...string) *IntCmd {
  283. args := make([]interface{}, len(keys)+1)
  284. args[0] = "touch"
  285. for i, key := range keys {
  286. args[i+1] = key
  287. }
  288. cmd := NewIntCmd(ctx, args...)
  289. _ = c(ctx, cmd)
  290. return cmd
  291. }
  292. func (c cmdable) TTL(ctx context.Context, key string) *DurationCmd {
  293. cmd := NewDurationCmd(ctx, time.Second, "ttl", key)
  294. _ = c(ctx, cmd)
  295. return cmd
  296. }
  297. func (c cmdable) Type(ctx context.Context, key string) *StatusCmd {
  298. cmd := NewStatusCmd(ctx, "type", key)
  299. _ = c(ctx, cmd)
  300. return cmd
  301. }
  302. func (c cmdable) Copy(ctx context.Context, sourceKey, destKey string, db int, replace bool) *IntCmd {
  303. args := []interface{}{"copy", sourceKey, destKey, "DB", db}
  304. if replace {
  305. args = append(args, "REPLACE")
  306. }
  307. cmd := NewIntCmd(ctx, args...)
  308. _ = c(ctx, cmd)
  309. return cmd
  310. }
  311. //------------------------------------------------------------------------------
  312. func (c cmdable) Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd {
  313. args := []interface{}{"scan", cursor}
  314. if match != "" {
  315. args = append(args, "match", match)
  316. }
  317. if count > 0 {
  318. args = append(args, "count", count)
  319. }
  320. cmd := NewScanCmd(ctx, c, args...)
  321. if hashtag.Present(match) {
  322. cmd.SetFirstKeyPos(3)
  323. }
  324. _ = c(ctx, cmd)
  325. return cmd
  326. }
  327. func (c cmdable) ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd {
  328. args := []interface{}{"scan", cursor}
  329. if match != "" {
  330. args = append(args, "match", match)
  331. }
  332. if count > 0 {
  333. args = append(args, "count", count)
  334. }
  335. if keyType != "" {
  336. args = append(args, "type", keyType)
  337. }
  338. cmd := NewScanCmd(ctx, c, args...)
  339. if hashtag.Present(match) {
  340. cmd.SetFirstKeyPos(3)
  341. }
  342. _ = c(ctx, cmd)
  343. return cmd
  344. }