@@ -226,15 +226,22 @@ type Cmdable interface {
226
226
XGroupCreateMkStream (ctx context.Context , stream , group , start string ) * StatusCmd
227
227
XGroupSetID (ctx context.Context , stream , group , start string ) * StatusCmd
228
228
XGroupDestroy (ctx context.Context , stream , group string ) * IntCmd
229
+ XGroupCreateConsumer (ctx context.Context , stream , group , consumer string ) * IntCmd
229
230
XGroupDelConsumer (ctx context.Context , stream , group , consumer string ) * IntCmd
230
231
XReadGroup (ctx context.Context , a * XReadGroupArgs ) * XStreamSliceCmd
231
232
XAck (ctx context.Context , stream , group string , ids ... string ) * IntCmd
232
233
XPending (ctx context.Context , stream , group string ) * XPendingCmd
233
234
XPendingExt (ctx context.Context , a * XPendingExtArgs ) * XPendingExtCmd
234
235
XClaim (ctx context.Context , a * XClaimArgs ) * XMessageSliceCmd
235
236
XClaimJustID (ctx context.Context , a * XClaimArgs ) * StringSliceCmd
237
+
238
+ // TODO: XTrim and XTrimApprox remove in v9.
236
239
XTrim (ctx context.Context , key string , maxLen int64 ) * IntCmd
237
240
XTrimApprox (ctx context.Context , key string , maxLen int64 ) * IntCmd
241
+ XTrimMaxLen (ctx context.Context , key string , maxLen int64 ) * IntCmd
242
+ XTrimMaxLenApprox (ctx context.Context , key string , maxLen , limit int64 ) * IntCmd
243
+ XTrimMinID (ctx context.Context , key string , minID string ) * IntCmd
244
+ XTrimMinIDApprox (ctx context.Context , key string , minID string , limit int64 ) * IntCmd
238
245
XInfoGroups (ctx context.Context , key string ) * XInfoGroupsCmd
239
246
XInfoStream (ctx context.Context , key string ) * XInfoStreamCmd
240
247
XInfoConsumers (ctx context.Context , key string , group string ) * XInfoConsumersCmd
@@ -1621,22 +1628,50 @@ func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...st
1621
1628
// - XAddArgs.Values = map[string]interface{}{"key1": "value1", "key2": "value2"}
1622
1629
//
1623
1630
// Note that map will not preserve the order of key-value pairs.
1631
+ // MaxLen/MaxLenApprox and MinID are in conflict, only one of them can be used.
1624
1632
type XAddArgs struct {
1625
- Stream string
1626
- MaxLen int64 // MAXLEN N
1633
+ Stream string
1634
+ NoMkStream bool
1635
+ MaxLen int64 // MAXLEN N
1636
+
1637
+ // Deprecated: use MaxLen+Approx, remove in v9.
1627
1638
MaxLenApprox int64 // MAXLEN ~ N
1628
- ID string
1629
- Values interface {}
1639
+
1640
+ MinID string
1641
+ // Approx causes MaxLen and MinID to use "~" matcher (instead of "=").
1642
+ Approx bool
1643
+ Limit int64
1644
+ ID string
1645
+ Values interface {}
1630
1646
}
1631
1647
1648
+ // XAdd a.Limit has a bug, please confirm it and use it.
1649
+ // issue: https://github.com/redis/redis/issues/9046
1632
1650
func (c cmdable ) XAdd (ctx context.Context , a * XAddArgs ) * StringCmd {
1633
- args := make ([]interface {}, 0 , 8 )
1634
- args = append (args , "xadd" )
1635
- args = append (args , a .Stream )
1636
- if a .MaxLen > 0 {
1637
- args = append (args , "maxlen" , a .MaxLen )
1638
- } else if a .MaxLenApprox > 0 {
1651
+ args := make ([]interface {}, 0 , 11 )
1652
+ args = append (args , "xadd" , a .Stream )
1653
+ if a .NoMkStream {
1654
+ args = append (args , "nomkstream" )
1655
+ }
1656
+ switch {
1657
+ case a .MaxLen > 0 :
1658
+ if a .Approx {
1659
+ args = append (args , "maxlen" , "~" , a .MaxLen )
1660
+ } else {
1661
+ args = append (args , "maxlen" , a .MaxLen )
1662
+ }
1663
+ case a .MaxLenApprox > 0 :
1664
+ // TODO remove in v9.
1639
1665
args = append (args , "maxlen" , "~" , a .MaxLenApprox )
1666
+ case a .MinID != "" :
1667
+ if a .Approx {
1668
+ args = append (args , "minid" , "~" , a .MinID )
1669
+ } else {
1670
+ args = append (args , "minid" , a .MinID )
1671
+ }
1672
+ }
1673
+ if a .Limit > 0 {
1674
+ args = append (args , "limit" , a .Limit )
1640
1675
}
1641
1676
if a .ID != "" {
1642
1677
args = append (args , a .ID )
@@ -1757,6 +1792,12 @@ func (c cmdable) XGroupDestroy(ctx context.Context, stream, group string) *IntCm
1757
1792
return cmd
1758
1793
}
1759
1794
1795
+ func (c cmdable ) XGroupCreateConsumer (ctx context.Context , stream , group , consumer string ) * IntCmd {
1796
+ cmd := NewIntCmd (ctx , "xgroup" , "createconsumer" , stream , group , consumer )
1797
+ _ = c (ctx , cmd )
1798
+ return cmd
1799
+ }
1800
+
1760
1801
func (c cmdable ) XGroupDelConsumer (ctx context.Context , stream , group , consumer string ) * IntCmd {
1761
1802
cmd := NewIntCmd (ctx , "xgroup" , "delconsumer" , stream , group , consumer )
1762
1803
_ = c (ctx , cmd )
@@ -1914,16 +1955,63 @@ func xClaimArgs(a *XClaimArgs) []interface{} {
1914
1955
return args
1915
1956
}
1916
1957
1917
- func (c cmdable ) XTrim (ctx context.Context , key string , maxLen int64 ) * IntCmd {
1918
- cmd := NewIntCmd (ctx , "xtrim" , key , "maxlen" , maxLen )
1958
+ // xTrim If approx is true, add the "~" parameter, otherwise it is the default "=" (redis default).
1959
+ // example:
1960
+ // XTRIM key MAXLEN/MINID threshold LIMIT limit.
1961
+ // XTRIM key MAXLEN/MINID ~ threshold LIMIT limit.
1962
+ // The redis-server version is lower than 6.2, please set limit to 0.
1963
+ func (c cmdable ) xTrim (
1964
+ ctx context.Context , key , strategy string ,
1965
+ approx bool , threshold interface {}, limit int64 ,
1966
+ ) * IntCmd {
1967
+ args := make ([]interface {}, 0 , 7 )
1968
+ args = append (args , "xtrim" , key , strategy )
1969
+ if approx {
1970
+ args = append (args , "~" )
1971
+ }
1972
+ args = append (args , threshold )
1973
+ if limit > 0 {
1974
+ args = append (args , "limit" , limit )
1975
+ }
1976
+ cmd := NewIntCmd (ctx , args ... )
1919
1977
_ = c (ctx , cmd )
1920
1978
return cmd
1921
1979
}
1922
1980
1981
+ // Deprecated: use XTrimMaxLen, remove in v9.
1982
+ func (c cmdable ) XTrim (ctx context.Context , key string , maxLen int64 ) * IntCmd {
1983
+ return c .xTrim (ctx , key , "maxlen" , false , maxLen , 0 )
1984
+ }
1985
+
1986
+ // Deprecated: use XTrimMaxLenApprox, remove in v9.
1923
1987
func (c cmdable ) XTrimApprox (ctx context.Context , key string , maxLen int64 ) * IntCmd {
1924
- cmd := NewIntCmd (ctx , "xtrim" , key , "maxlen" , "~" , maxLen )
1925
- _ = c (ctx , cmd )
1926
- return cmd
1988
+ return c .xTrim (ctx , key , "maxlen" , true , maxLen , 0 )
1989
+ }
1990
+
1991
+ // XTrimMaxLen No `~` rules are used, `limit` cannot be used.
1992
+ // cmd: XTRIM key MAXLEN maxLen
1993
+ func (c cmdable ) XTrimMaxLen (ctx context.Context , key string , maxLen int64 ) * IntCmd {
1994
+ return c .xTrim (ctx , key , "maxlen" , false , maxLen , 0 )
1995
+ }
1996
+
1997
+ // XTrimMaxLenApprox LIMIT has a bug, please confirm it and use it.
1998
+ // issue: https://github.com/redis/redis/issues/9046
1999
+ // cmd: XTRIM key MAXLEN ~ maxLen LIMIT limit
2000
+ func (c cmdable ) XTrimMaxLenApprox (ctx context.Context , key string , maxLen , limit int64 ) * IntCmd {
2001
+ return c .xTrim (ctx , key , "maxlen" , true , maxLen , limit )
2002
+ }
2003
+
2004
+ // XTrimMinID No `~` rules are used, `limit` cannot be used.
2005
+ // cmd: XTRIM key MINID minID
2006
+ func (c cmdable ) XTrimMinID (ctx context.Context , key string , minID string ) * IntCmd {
2007
+ return c .xTrim (ctx , key , "minid" , false , minID , 0 )
2008
+ }
2009
+
2010
+ // XTrimMinIDApprox LIMIT has a bug, please confirm it and use it.
2011
+ // issue: https://github.com/redis/redis/issues/9046
2012
+ // cmd: XTRIM key MINID ~ minID LIMIT limit
2013
+ func (c cmdable ) XTrimMinIDApprox (ctx context.Context , key string , minID string , limit int64 ) * IntCmd {
2014
+ return c .xTrim (ctx , key , "minid" , true , minID , limit )
1927
2015
}
1928
2016
1929
2017
func (c cmdable ) XInfoConsumers (ctx context.Context , key string , group string ) * XInfoConsumersCmd {
0 commit comments