25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.5 KiB

  1. package redis
  2. import (
  3. "context"
  4. "github.com/redis/go-redis/v9/internal/hashtag"
  5. )
  6. type SetCmdable interface {
  7. SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd
  8. SCard(ctx context.Context, key string) *IntCmd
  9. SDiff(ctx context.Context, keys ...string) *StringSliceCmd
  10. SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd
  11. SInter(ctx context.Context, keys ...string) *StringSliceCmd
  12. SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd
  13. SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd
  14. SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd
  15. SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd
  16. SMembers(ctx context.Context, key string) *StringSliceCmd
  17. SMembersMap(ctx context.Context, key string) *StringStructMapCmd
  18. SMove(ctx context.Context, source, destination string, member interface{}) *BoolCmd
  19. SPop(ctx context.Context, key string) *StringCmd
  20. SPopN(ctx context.Context, key string, count int64) *StringSliceCmd
  21. SRandMember(ctx context.Context, key string) *StringCmd
  22. SRandMemberN(ctx context.Context, key string, count int64) *StringSliceCmd
  23. SRem(ctx context.Context, key string, members ...interface{}) *IntCmd
  24. SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
  25. SUnion(ctx context.Context, keys ...string) *StringSliceCmd
  26. SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd
  27. }
  28. //------------------------------------------------------------------------------
  29. func (c cmdable) SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd {
  30. args := make([]interface{}, 2, 2+len(members))
  31. args[0] = "sadd"
  32. args[1] = key
  33. args = appendArgs(args, members)
  34. cmd := NewIntCmd(ctx, args...)
  35. _ = c(ctx, cmd)
  36. return cmd
  37. }
  38. func (c cmdable) SCard(ctx context.Context, key string) *IntCmd {
  39. cmd := NewIntCmd(ctx, "scard", key)
  40. _ = c(ctx, cmd)
  41. return cmd
  42. }
  43. func (c cmdable) SDiff(ctx context.Context, keys ...string) *StringSliceCmd {
  44. args := make([]interface{}, 1+len(keys))
  45. args[0] = "sdiff"
  46. for i, key := range keys {
  47. args[1+i] = key
  48. }
  49. cmd := NewStringSliceCmd(ctx, args...)
  50. _ = c(ctx, cmd)
  51. return cmd
  52. }
  53. func (c cmdable) SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd {
  54. args := make([]interface{}, 2+len(keys))
  55. args[0] = "sdiffstore"
  56. args[1] = destination
  57. for i, key := range keys {
  58. args[2+i] = key
  59. }
  60. cmd := NewIntCmd(ctx, args...)
  61. _ = c(ctx, cmd)
  62. return cmd
  63. }
  64. func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
  65. args := make([]interface{}, 1+len(keys))
  66. args[0] = "sinter"
  67. for i, key := range keys {
  68. args[1+i] = key
  69. }
  70. cmd := NewStringSliceCmd(ctx, args...)
  71. _ = c(ctx, cmd)
  72. return cmd
  73. }
  74. func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
  75. numKeys := len(keys)
  76. args := make([]interface{}, 4+numKeys)
  77. args[0] = "sintercard"
  78. args[1] = numKeys
  79. for i, key := range keys {
  80. args[2+i] = key
  81. }
  82. args[2+numKeys] = "limit"
  83. args[3+numKeys] = limit
  84. cmd := NewIntCmd(ctx, args...)
  85. _ = c(ctx, cmd)
  86. return cmd
  87. }
  88. func (c cmdable) SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd {
  89. args := make([]interface{}, 2+len(keys))
  90. args[0] = "sinterstore"
  91. args[1] = destination
  92. for i, key := range keys {
  93. args[2+i] = key
  94. }
  95. cmd := NewIntCmd(ctx, args...)
  96. _ = c(ctx, cmd)
  97. return cmd
  98. }
  99. func (c cmdable) SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd {
  100. cmd := NewBoolCmd(ctx, "sismember", key, member)
  101. _ = c(ctx, cmd)
  102. return cmd
  103. }
  104. // SMIsMember Redis `SMISMEMBER key member [member ...]` command.
  105. func (c cmdable) SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd {
  106. args := make([]interface{}, 2, 2+len(members))
  107. args[0] = "smismember"
  108. args[1] = key
  109. args = appendArgs(args, members)
  110. cmd := NewBoolSliceCmd(ctx, args...)
  111. _ = c(ctx, cmd)
  112. return cmd
  113. }
  114. // SMembers Redis `SMEMBERS key` command output as a slice.
  115. func (c cmdable) SMembers(ctx context.Context, key string) *StringSliceCmd {
  116. cmd := NewStringSliceCmd(ctx, "smembers", key)
  117. _ = c(ctx, cmd)
  118. return cmd
  119. }
  120. // SMembersMap Redis `SMEMBERS key` command output as a map.
  121. func (c cmdable) SMembersMap(ctx context.Context, key string) *StringStructMapCmd {
  122. cmd := NewStringStructMapCmd(ctx, "smembers", key)
  123. _ = c(ctx, cmd)
  124. return cmd
  125. }
  126. func (c cmdable) SMove(ctx context.Context, source, destination string, member interface{}) *BoolCmd {
  127. cmd := NewBoolCmd(ctx, "smove", source, destination, member)
  128. _ = c(ctx, cmd)
  129. return cmd
  130. }
  131. // SPop Redis `SPOP key` command.
  132. func (c cmdable) SPop(ctx context.Context, key string) *StringCmd {
  133. cmd := NewStringCmd(ctx, "spop", key)
  134. _ = c(ctx, cmd)
  135. return cmd
  136. }
  137. // SPopN Redis `SPOP key count` command.
  138. func (c cmdable) SPopN(ctx context.Context, key string, count int64) *StringSliceCmd {
  139. cmd := NewStringSliceCmd(ctx, "spop", key, count)
  140. _ = c(ctx, cmd)
  141. return cmd
  142. }
  143. // SRandMember Redis `SRANDMEMBER key` command.
  144. func (c cmdable) SRandMember(ctx context.Context, key string) *StringCmd {
  145. cmd := NewStringCmd(ctx, "srandmember", key)
  146. _ = c(ctx, cmd)
  147. return cmd
  148. }
  149. // SRandMemberN Redis `SRANDMEMBER key count` command.
  150. func (c cmdable) SRandMemberN(ctx context.Context, key string, count int64) *StringSliceCmd {
  151. cmd := NewStringSliceCmd(ctx, "srandmember", key, count)
  152. _ = c(ctx, cmd)
  153. return cmd
  154. }
  155. func (c cmdable) SRem(ctx context.Context, key string, members ...interface{}) *IntCmd {
  156. args := make([]interface{}, 2, 2+len(members))
  157. args[0] = "srem"
  158. args[1] = key
  159. args = appendArgs(args, members)
  160. cmd := NewIntCmd(ctx, args...)
  161. _ = c(ctx, cmd)
  162. return cmd
  163. }
  164. func (c cmdable) SUnion(ctx context.Context, keys ...string) *StringSliceCmd {
  165. args := make([]interface{}, 1+len(keys))
  166. args[0] = "sunion"
  167. for i, key := range keys {
  168. args[1+i] = key
  169. }
  170. cmd := NewStringSliceCmd(ctx, args...)
  171. _ = c(ctx, cmd)
  172. return cmd
  173. }
  174. func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd {
  175. args := make([]interface{}, 2+len(keys))
  176. args[0] = "sunionstore"
  177. args[1] = destination
  178. for i, key := range keys {
  179. args[2+i] = key
  180. }
  181. cmd := NewIntCmd(ctx, args...)
  182. _ = c(ctx, cmd)
  183. return cmd
  184. }
  185. func (c cmdable) SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
  186. args := []interface{}{"sscan", key, cursor}
  187. if match != "" {
  188. args = append(args, "match", match)
  189. }
  190. if count > 0 {
  191. args = append(args, "count", count)
  192. }
  193. cmd := NewScanCmd(ctx, c, args...)
  194. if hashtag.Present(match) {
  195. cmd.SetFirstKeyPos(4)
  196. }
  197. _ = c(ctx, cmd)
  198. return cmd
  199. }