...

Source file src/github.com/redis/go-redis/v9/doctests/stream_tutorial_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     1  // EXAMPLE: stream_tutorial
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/redis/go-redis/v9"
    10  )
    11  
    12  // HIDE_END
    13  
    14  // REMOVE_START
    15  func UNUSED(v ...interface{}) {}
    16  
    17  // REMOVE_END
    18  
    19  func ExampleClient_xadd() {
    20  	ctx := context.Background()
    21  
    22  	rdb := redis.NewClient(&redis.Options{
    23  		Addr:     "localhost:6379",
    24  		Password: "", // no password docs
    25  		DB:       0,  // use default DB
    26  	})
    27  
    28  	// REMOVE_START
    29  	// start with fresh database
    30  	rdb.FlushDB(ctx)
    31  	rdb.Del(ctx, "race:france")
    32  	// REMOVE_END
    33  
    34  	// STEP_START xadd
    35  	res1, err := rdb.XAdd(ctx, &redis.XAddArgs{
    36  		Stream: "race:france",
    37  		Values: map[string]interface{}{
    38  			"rider":       "Castilla",
    39  			"speed":       30.2,
    40  			"position":    1,
    41  			"location_id": 1,
    42  		},
    43  	}).Result()
    44  
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  
    49  	// fmt.Println(res1) // >>> 1692632086370-0
    50  
    51  	res2, err := rdb.XAdd(ctx, &redis.XAddArgs{
    52  		Stream: "race:france",
    53  		Values: map[string]interface{}{
    54  			"rider":       "Norem",
    55  			"speed":       28.8,
    56  			"position":    3,
    57  			"location_id": 1,
    58  		},
    59  	}).Result()
    60  
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  
    65  	// fmt.PrintLn(res2) // >>> 1692632094485-0
    66  
    67  	res3, err := rdb.XAdd(ctx, &redis.XAddArgs{
    68  		Stream: "race:france",
    69  		Values: map[string]interface{}{
    70  			"rider":       "Prickett",
    71  			"speed":       29.7,
    72  			"position":    2,
    73  			"location_id": 1,
    74  		},
    75  	}).Result()
    76  
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  
    81  	// fmt.Println(res3) // >>> 1692632102976-0
    82  	// STEP_END
    83  
    84  	// REMOVE_START
    85  	UNUSED(res1, res2, res3)
    86  	// REMOVE_END
    87  
    88  	xlen, err := rdb.XLen(ctx, "race:france").Result()
    89  
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  
    94  	fmt.Println(xlen) // >>> 3
    95  
    96  	// Output:
    97  	// 3
    98  }
    99  
   100  func ExampleClient_racefrance1() {
   101  	ctx := context.Background()
   102  
   103  	rdb := redis.NewClient(&redis.Options{
   104  		Addr:     "localhost:6379",
   105  		Password: "", // no password docs
   106  		DB:       0,  // use default DB
   107  	})
   108  
   109  	// REMOVE_START
   110  	// start with fresh database
   111  	rdb.FlushDB(ctx)
   112  	rdb.Del(ctx, "race:france")
   113  	// REMOVE_END
   114  
   115  	_, err := rdb.XAdd(ctx, &redis.XAddArgs{
   116  		Stream: "race:france",
   117  		Values: map[string]interface{}{
   118  			"rider":       "Castilla",
   119  			"speed":       30.2,
   120  			"position":    1,
   121  			"location_id": 1,
   122  		},
   123  		ID: "1692632086370-0",
   124  	}).Result()
   125  
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  
   130  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   131  		Stream: "race:france",
   132  		Values: map[string]interface{}{
   133  			"rider":       "Norem",
   134  			"speed":       28.8,
   135  			"position":    3,
   136  			"location_id": 1,
   137  		},
   138  		ID: "1692632094485-0",
   139  	}).Result()
   140  
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  
   145  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   146  		Stream: "race:france",
   147  		Values: map[string]interface{}{
   148  			"rider":       "Prickett",
   149  			"speed":       29.7,
   150  			"position":    2,
   151  			"location_id": 1,
   152  		},
   153  		ID: "1692632102976-0",
   154  	}).Result()
   155  
   156  	if err != nil {
   157  		panic(err)
   158  	}
   159  
   160  	// STEP_START xrange
   161  	res4, err := rdb.XRangeN(ctx, "race:france", "1691765278160-0", "+", 2).Result()
   162  
   163  	if err != nil {
   164  		panic(err)
   165  	}
   166  
   167  	fmt.Println(res4)
   168  	// >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla...
   169  	// STEP_END
   170  
   171  	// STEP_START xread_block
   172  	res5, err := rdb.XRead(ctx, &redis.XReadArgs{
   173  		Streams: []string{"race:france", "0"},
   174  		Count:   100,
   175  		Block:   300,
   176  	}).Result()
   177  
   178  	if err != nil {
   179  		panic(err)
   180  	}
   181  
   182  	fmt.Println(res5)
   183  	// >>> // [{race:france [{1692632086370-0 map[location_id:1 position:1...
   184  	// STEP_END
   185  
   186  	// STEP_START xadd_2
   187  	res6, err := rdb.XAdd(ctx, &redis.XAddArgs{
   188  		Stream: "race:france",
   189  		Values: map[string]interface{}{
   190  			"rider":       "Castilla",
   191  			"speed":       29.9,
   192  			"position":    1,
   193  			"location_id": 2,
   194  		},
   195  	}).Result()
   196  
   197  	if err != nil {
   198  		panic(err)
   199  	}
   200  
   201  	//fmt.Println(res6) // >>> 1692632147973-0
   202  	// STEP_END
   203  
   204  	// STEP_START xlen
   205  	res7, err := rdb.XLen(ctx, "race:france").Result()
   206  
   207  	if err != nil {
   208  		panic(err)
   209  	}
   210  
   211  	fmt.Println(res7) // >>> 4
   212  	// STEP_END
   213  
   214  	// REMOVE_START
   215  	UNUSED(res6)
   216  	// REMOVE_END
   217  
   218  	// Output:
   219  	// [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]
   220  	// [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]}]}]
   221  	// 4
   222  }
   223  
   224  func ExampleClient_raceusa() {
   225  	ctx := context.Background()
   226  
   227  	rdb := redis.NewClient(&redis.Options{
   228  		Addr:     "localhost:6379",
   229  		Password: "", // no password docs
   230  		DB:       0,  // use default DB
   231  	})
   232  
   233  	// REMOVE_START
   234  	// start with fresh database
   235  	rdb.FlushDB(ctx)
   236  	rdb.Del(ctx, "race:usa")
   237  	// REMOVE_END
   238  
   239  	// STEP_START xadd_id
   240  	res8, err := rdb.XAdd(ctx, &redis.XAddArgs{
   241  		Stream: "race:usa",
   242  		Values: map[string]interface{}{
   243  			"racer": "Castilla",
   244  		},
   245  		ID: "0-1",
   246  	}).Result()
   247  
   248  	if err != nil {
   249  		panic(err)
   250  	}
   251  
   252  	fmt.Println(res8) // >>> 0-1
   253  
   254  	res9, err := rdb.XAdd(ctx, &redis.XAddArgs{
   255  		Stream: "race:usa",
   256  		Values: map[string]interface{}{
   257  			"racer": "Norem",
   258  		},
   259  		ID: "0-2",
   260  	}).Result()
   261  
   262  	if err != nil {
   263  		panic(err)
   264  	}
   265  
   266  	fmt.Println(res9) // >>> 0-2
   267  	// STEP_END
   268  
   269  	// STEP_START xadd_bad_id
   270  	res10, err := rdb.XAdd(ctx, &redis.XAddArgs{
   271  		Values: map[string]interface{}{
   272  			"racer": "Prickett",
   273  		},
   274  		ID: "0-1",
   275  	}).Result()
   276  
   277  	if err != nil {
   278  		// fmt.Println(err)
   279  		// >>> ERR The ID specified in XADD is equal or smaller than the target stream top item
   280  	}
   281  	// STEP_END
   282  
   283  	// STEP_START xadd_7
   284  	res11, err := rdb.XAdd(ctx, &redis.XAddArgs{
   285  		Stream: "race:usa",
   286  		Values: map[string]interface{}{
   287  			"racer": "Prickett",
   288  		},
   289  		ID: "0-*",
   290  	}).Result()
   291  
   292  	if err != nil {
   293  		panic(err)
   294  	}
   295  
   296  	fmt.Println(res11) // >>> 0-3
   297  	// STEP_END
   298  
   299  	// REMOVE_START
   300  	UNUSED(res10)
   301  	// REMOVE_END
   302  
   303  	// Output:
   304  	// 0-1
   305  	// 0-2
   306  	// 0-3
   307  }
   308  
   309  func ExampleClient_racefrance2() {
   310  	ctx := context.Background()
   311  
   312  	rdb := redis.NewClient(&redis.Options{
   313  		Addr:     "localhost:6379",
   314  		Password: "", // no password docs
   315  		DB:       0,  // use default DB
   316  	})
   317  
   318  	// REMOVE_START
   319  	// start with fresh database
   320  	rdb.FlushDB(ctx)
   321  	rdb.Del(ctx, "race:france")
   322  	// REMOVE_END
   323  
   324  	_, err := rdb.XAdd(ctx, &redis.XAddArgs{
   325  		Stream: "race:france",
   326  		Values: map[string]interface{}{
   327  			"rider":       "Castilla",
   328  			"speed":       30.2,
   329  			"position":    1,
   330  			"location_id": 1,
   331  		},
   332  		ID: "1692632086370-0",
   333  	}).Result()
   334  
   335  	if err != nil {
   336  		panic(err)
   337  	}
   338  
   339  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   340  		Stream: "race:france",
   341  		Values: map[string]interface{}{
   342  			"rider":       "Norem",
   343  			"speed":       28.8,
   344  			"position":    3,
   345  			"location_id": 1,
   346  		},
   347  		ID: "1692632094485-0",
   348  	}).Result()
   349  
   350  	if err != nil {
   351  		panic(err)
   352  	}
   353  
   354  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   355  		Stream: "race:france",
   356  		Values: map[string]interface{}{
   357  			"rider":       "Prickett",
   358  			"speed":       29.7,
   359  			"position":    2,
   360  			"location_id": 1,
   361  		},
   362  		ID: "1692632102976-0",
   363  	}).Result()
   364  
   365  	if err != nil {
   366  		panic(err)
   367  	}
   368  
   369  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   370  		Stream: "race:france",
   371  		Values: map[string]interface{}{
   372  			"rider":       "Castilla",
   373  			"speed":       29.9,
   374  			"position":    1,
   375  			"location_id": 2,
   376  		},
   377  		ID: "1692632147973-0",
   378  	}).Result()
   379  
   380  	if err != nil {
   381  		panic(err)
   382  	}
   383  	// STEP_START xrange_all
   384  	res12, err := rdb.XRange(ctx, "race:france", "-", "+").Result()
   385  
   386  	if err != nil {
   387  		panic(err)
   388  	}
   389  
   390  	fmt.Println(res12)
   391  	// >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla...
   392  	// STEP_END
   393  
   394  	// STEP_START xrange_time
   395  	res13, err := rdb.XRange(ctx, "race:france",
   396  		"1692632086369", "1692632086371",
   397  	).Result()
   398  
   399  	if err != nil {
   400  		panic(err)
   401  	}
   402  
   403  	fmt.Println(res13)
   404  	// >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}]
   405  	// STEP_END
   406  
   407  	// STEP_START xrange_step_1
   408  	res14, err := rdb.XRangeN(ctx, "race:france", "-", "+", 2).Result()
   409  
   410  	if err != nil {
   411  		panic(err)
   412  	}
   413  
   414  	fmt.Println(res14)
   415  	// >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]
   416  	// STEP_END
   417  
   418  	// STEP_START xrange_step_2
   419  	res15, err := rdb.XRangeN(ctx, "race:france",
   420  		"(1692632094485-0", "+", 2,
   421  	).Result()
   422  
   423  	if err != nil {
   424  		panic(err)
   425  	}
   426  
   427  	fmt.Println(res15)
   428  	// >>> [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}]
   429  	// STEP_END
   430  
   431  	// STEP_START xrange_empty
   432  	res16, err := rdb.XRangeN(ctx, "race:france",
   433  		"(1692632147973-0", "+", 2,
   434  	).Result()
   435  
   436  	if err != nil {
   437  		panic(err)
   438  	}
   439  
   440  	fmt.Println(res16)
   441  	// >>> []
   442  	// STEP_END
   443  
   444  	// STEP_START xrevrange
   445  	res17, err := rdb.XRevRangeN(ctx, "race:france", "+", "-", 1).Result()
   446  
   447  	if err != nil {
   448  		panic(err)
   449  	}
   450  
   451  	fmt.Println(res17)
   452  	// >>> [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}]
   453  	// STEP_END
   454  
   455  	// STEP_START xread
   456  	res18, err := rdb.XRead(ctx, &redis.XReadArgs{
   457  		Streams: []string{"race:france", "0"},
   458  		Count:   2,
   459  	}).Result()
   460  
   461  	if err != nil {
   462  		panic(err)
   463  	}
   464  
   465  	fmt.Println(res18)
   466  	// >>> [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}]
   467  	// STEP_END
   468  
   469  	// Output:
   470  	// [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}]
   471  	// [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}]
   472  	// [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]
   473  	// [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}]
   474  	// []
   475  	// [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}]
   476  	// [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}]
   477  }
   478  
   479  func ExampleClient_xgroupcreate() {
   480  	ctx := context.Background()
   481  
   482  	rdb := redis.NewClient(&redis.Options{
   483  		Addr:     "localhost:6379",
   484  		Password: "", // no password docs
   485  		DB:       0,  // use default DB
   486  	})
   487  
   488  	// REMOVE_START
   489  	// start with fresh database
   490  	rdb.FlushDB(ctx)
   491  	rdb.Del(ctx, "race:france")
   492  	// REMOVE_END
   493  
   494  	_, err := rdb.XAdd(ctx, &redis.XAddArgs{
   495  		Stream: "race:france",
   496  		Values: map[string]interface{}{
   497  			"rider":       "Castilla",
   498  			"speed":       30.2,
   499  			"position":    1,
   500  			"location_id": 1,
   501  		},
   502  		ID: "1692632086370-0",
   503  	}).Result()
   504  
   505  	if err != nil {
   506  		panic(err)
   507  	}
   508  
   509  	// STEP_START xgroup_create
   510  	res19, err := rdb.XGroupCreate(ctx, "race:france", "france_riders", "$").Result()
   511  
   512  	if err != nil {
   513  		panic(err)
   514  	}
   515  
   516  	fmt.Println(res19) // >>> OK
   517  	// STEP_END
   518  
   519  	// Output:
   520  	// OK
   521  }
   522  
   523  func ExampleClient_xgroupcreatemkstream() {
   524  	ctx := context.Background()
   525  
   526  	rdb := redis.NewClient(&redis.Options{
   527  		Addr:     "localhost:6379",
   528  		Password: "", // no password docs
   529  		DB:       0,  // use default DB
   530  	})
   531  
   532  	// REMOVE_START
   533  	// start with fresh database
   534  	rdb.FlushDB(ctx)
   535  	rdb.Del(ctx, "race:italy")
   536  	// REMOVE_END
   537  
   538  	// STEP_START xgroup_create_mkstream
   539  	res20, err := rdb.XGroupCreateMkStream(ctx,
   540  		"race:italy", "italy_riders", "$",
   541  	).Result()
   542  
   543  	if err != nil {
   544  		panic(err)
   545  	}
   546  
   547  	fmt.Println(res20) // >>> OK
   548  	// STEP_END
   549  
   550  	// Output:
   551  	// OK
   552  }
   553  
   554  func ExampleClient_xgroupread() {
   555  	ctx := context.Background()
   556  
   557  	rdb := redis.NewClient(&redis.Options{
   558  		Addr:     "localhost:6379",
   559  		Password: "", // no password docs
   560  		DB:       0,  // use default DB
   561  	})
   562  
   563  	// REMOVE_START
   564  	// start with fresh database
   565  	rdb.FlushDB(ctx)
   566  	rdb.Del(ctx, "race:italy")
   567  	// REMOVE_END
   568  
   569  	_, err := rdb.XGroupCreateMkStream(ctx,
   570  		"race:italy", "italy_riders", "$",
   571  	).Result()
   572  
   573  	if err != nil {
   574  		panic(err)
   575  	}
   576  
   577  	// STEP_START xgroup_read
   578  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   579  		Stream: "race:italy",
   580  		Values: map[string]interface{}{"rider": "Castilla"},
   581  	}).Result()
   582  	// >>> 1692632639151-0
   583  
   584  	if err != nil {
   585  		panic(err)
   586  	}
   587  
   588  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   589  		Stream: "race:italy",
   590  		Values: map[string]interface{}{"rider": "Royce"},
   591  	}).Result()
   592  	// >>> 1692632647899-0
   593  
   594  	if err != nil {
   595  		panic(err)
   596  	}
   597  
   598  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   599  		Stream: "race:italy",
   600  		Values: map[string]interface{}{"rider": "Sam-Bodden"},
   601  	}).Result()
   602  	// >>> 1692632662819-0
   603  
   604  	if err != nil {
   605  		panic(err)
   606  	}
   607  
   608  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   609  		Stream: "race:italy",
   610  		Values: map[string]interface{}{"rider": "Prickett"},
   611  	}).Result()
   612  	// >>> 1692632670501-0
   613  
   614  	if err != nil {
   615  		panic(err)
   616  	}
   617  
   618  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   619  		Stream: "race:italy",
   620  		Values: map[string]interface{}{"rider": "Norem"},
   621  	}).Result()
   622  	// >>> 1692632678249-0
   623  
   624  	if err != nil {
   625  		panic(err)
   626  	}
   627  
   628  	// fmt.Println(res25)
   629  
   630  	res21, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
   631  		Streams:  []string{"race:italy", ">"},
   632  		Group:    "italy_riders",
   633  		Consumer: "Alice",
   634  		Count:    1,
   635  	}).Result()
   636  
   637  	if err != nil {
   638  		panic(err)
   639  	}
   640  
   641  	// fmt.Println(res21)
   642  	// >>> [{race:italy [{1692632639151-0 map[rider:Castilla]}]}]
   643  	// STEP_END
   644  
   645  	// REMOVE_START
   646  	UNUSED(res21)
   647  	// REMOVE_END
   648  
   649  	xlen, err := rdb.XLen(ctx, "race:italy").Result()
   650  
   651  	if err != nil {
   652  		panic(err)
   653  	}
   654  
   655  	fmt.Println(xlen)
   656  
   657  	// Output:
   658  	// 5
   659  }
   660  
   661  func ExampleClient_raceitaly() {
   662  	ctx := context.Background()
   663  
   664  	rdb := redis.NewClient(&redis.Options{
   665  		Addr:     "localhost:6379",
   666  		Password: "", // no password docs
   667  		DB:       0,  // use default DB
   668  	})
   669  
   670  	// REMOVE_START
   671  	// start with fresh database
   672  	rdb.FlushDB(ctx)
   673  	rdb.Del(ctx, "race:italy")
   674  	rdb.XGroupDestroy(ctx, "race:italy", "italy_riders")
   675  	// REMOVE_END
   676  
   677  	_, err := rdb.XGroupCreateMkStream(ctx,
   678  		"race:italy", "italy_riders", "$",
   679  	).Result()
   680  
   681  	if err != nil {
   682  		panic(err)
   683  	}
   684  
   685  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   686  		Stream: "race:italy",
   687  		Values: map[string]interface{}{"rider": "Castilla"},
   688  		ID:     "1692632639151-0",
   689  	}).Result()
   690  
   691  	if err != nil {
   692  		panic(err)
   693  	}
   694  
   695  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   696  		Stream: "race:italy",
   697  		Values: map[string]interface{}{"rider": "Royce"},
   698  		ID:     "1692632647899-0",
   699  	}).Result()
   700  
   701  	if err != nil {
   702  		panic(err)
   703  	}
   704  
   705  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   706  		Stream: "race:italy",
   707  		Values: map[string]interface{}{"rider": "Sam-Bodden"},
   708  		ID:     "1692632662819-0",
   709  	}).Result()
   710  
   711  	if err != nil {
   712  		panic(err)
   713  	}
   714  
   715  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   716  		Stream: "race:italy",
   717  		Values: map[string]interface{}{"rider": "Prickett"},
   718  		ID:     "1692632670501-0",
   719  	}).Result()
   720  
   721  	if err != nil {
   722  		panic(err)
   723  	}
   724  
   725  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   726  		Stream: "race:italy",
   727  		Values: map[string]interface{}{"rider": "Norem"},
   728  		ID:     "1692632678249-0",
   729  	}).Result()
   730  
   731  	if err != nil {
   732  		panic(err)
   733  	}
   734  
   735  	_, err = rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
   736  		Streams:  []string{"race:italy", ">"},
   737  		Group:    "italy_riders",
   738  		Consumer: "Alice",
   739  		Count:    1,
   740  	}).Result()
   741  
   742  	if err != nil {
   743  		panic(err)
   744  	}
   745  	// STEP_START xgroup_read_id
   746  	res22, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
   747  		Streams:  []string{"race:italy", "0"},
   748  		Group:    "italy_riders",
   749  		Consumer: "Alice",
   750  	}).Result()
   751  
   752  	if err != nil {
   753  		panic(err)
   754  	}
   755  
   756  	fmt.Println(res22)
   757  	// >>> [{race:italy [{1692632639151-0 map[rider:Castilla]}]}]
   758  	// STEP_END
   759  
   760  	// STEP_START xack
   761  	res23, err := rdb.XAck(ctx,
   762  		"race:italy", "italy_riders", "1692632639151-0",
   763  	).Result()
   764  
   765  	if err != nil {
   766  		panic(err)
   767  	}
   768  
   769  	fmt.Println(res23) // >>> 1
   770  
   771  	res24, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
   772  		Streams:  []string{"race:italy", "0"},
   773  		Group:    "italy_riders",
   774  		Consumer: "Alice",
   775  	}).Result()
   776  
   777  	if err != nil {
   778  		panic(err)
   779  	}
   780  
   781  	fmt.Println(res24)
   782  	// >>> [{race:italy []}]
   783  	// STEP_END
   784  
   785  	// STEP_START xgroup_read_bob
   786  	res25, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
   787  		Streams:  []string{"race:italy", ">"},
   788  		Group:    "italy_riders",
   789  		Consumer: "Bob",
   790  		Count:    2,
   791  	}).Result()
   792  
   793  	if err != nil {
   794  		panic(err)
   795  	}
   796  
   797  	fmt.Println(res25)
   798  	// >>> [{race:italy [{1692632647899-0 map[rider:Royce]} {1692632662819-0 map[rider:Sam-Bodden]}]}]
   799  
   800  	// STEP_END
   801  
   802  	// STEP_START xpending
   803  	res26, err := rdb.XPending(ctx, "race:italy", "italy_riders").Result()
   804  
   805  	if err != nil {
   806  		panic(err)
   807  	}
   808  
   809  	fmt.Println(res26)
   810  	// >>> &{2 1692632647899-0 1692632662819-0 map[Bob:2]}
   811  	// STEP_END
   812  
   813  	// STEP_START xpending_plus_minus
   814  	res27, err := rdb.XPendingExt(ctx, &redis.XPendingExtArgs{
   815  		Stream: "race:italy",
   816  		Group:  "italy_riders",
   817  		Start:  "-",
   818  		End:    "+",
   819  		Count:  10,
   820  	}).Result()
   821  
   822  	if err != nil {
   823  		panic(err)
   824  	}
   825  
   826  	// fmt.Println(res27)
   827  	// >>> [{1692632647899-0 Bob 0s 1} {1692632662819-0 Bob 0s 1}]
   828  	// STEP_END
   829  
   830  	// STEP_START xrange_pending
   831  	res28, err := rdb.XRange(ctx, "race:italy",
   832  		"1692632647899-0", "1692632647899-0",
   833  	).Result()
   834  
   835  	if err != nil {
   836  		panic(err)
   837  	}
   838  
   839  	fmt.Println(res28) // >>> [{1692632647899-0 map[rider:Royce]}]
   840  	// STEP_END
   841  
   842  	// STEP_START xclaim
   843  	res29, err := rdb.XClaim(ctx, &redis.XClaimArgs{
   844  		Stream:   "race:italy",
   845  		Group:    "italy_riders",
   846  		Consumer: "Alice",
   847  		MinIdle:  0,
   848  		Messages: []string{"1692632647899-0"},
   849  	}).Result()
   850  
   851  	if err != nil {
   852  		panic(err)
   853  	}
   854  
   855  	fmt.Println(res29)
   856  	// STEP_END
   857  
   858  	// STEP_START xautoclaim
   859  	res30, res30a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{
   860  		Stream:   "race:italy",
   861  		Group:    "italy_riders",
   862  		Consumer: "Alice",
   863  		Start:    "0-0",
   864  		Count:    1,
   865  	}).Result()
   866  
   867  	if err != nil {
   868  		panic(err)
   869  	}
   870  
   871  	fmt.Println(res30)  // >>> [{1692632647899-0 map[rider:Royce]}]
   872  	fmt.Println(res30a) // >>> 1692632662819-0
   873  	// STEP_END
   874  
   875  	// STEP_START xautoclaim_cursor
   876  	res31, res31a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{
   877  		Stream:   "race:italy",
   878  		Group:    "italy_riders",
   879  		Consumer: "Lora",
   880  		Start:    "(1692632662819-0",
   881  		Count:    1,
   882  	}).Result()
   883  
   884  	if err != nil {
   885  		panic(err)
   886  	}
   887  
   888  	fmt.Println(res31)  // >>> []
   889  	fmt.Println(res31a) // >>> 0-0
   890  	// STEP_END
   891  
   892  	// STEP_START xinfo
   893  	res32, err := rdb.XInfoStream(ctx, "race:italy").Result()
   894  
   895  	if err != nil {
   896  		panic(err)
   897  	}
   898  
   899  	fmt.Println(res32)
   900  	// >>> &{5 1 2 1 1692632678249-0 0-0 5 {1692632639151-0 map[rider:Castilla]} {1692632678249-0 map[rider:Norem]} 1692632639151-0}
   901  	// STEP_END
   902  
   903  	// STEP_START xinfo_groups
   904  	res33, err := rdb.XInfoGroups(ctx, "race:italy").Result()
   905  
   906  	if err != nil {
   907  		panic(err)
   908  	}
   909  
   910  	fmt.Println(res33)
   911  	// >>> [{italy_riders 3 2 1692632662819-0 3 2}]
   912  	// STEP_END
   913  
   914  	// STEP_START xinfo_consumers
   915  	res34, err := rdb.XInfoConsumers(ctx, "race:italy", "italy_riders").Result()
   916  
   917  	if err != nil {
   918  		panic(err)
   919  	}
   920  
   921  	// fmt.Println(res34)
   922  	// >>> [{Alice 1 1ms 1ms} {Bob 1 2ms 2ms} {Lora 0 1ms -1ms}]
   923  	// STEP_END
   924  
   925  	// STEP_START maxlen
   926  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   927  		Stream: "race:italy",
   928  		MaxLen: 2,
   929  		Values: map[string]interface{}{"rider": "Jones"},
   930  	},
   931  	).Result()
   932  
   933  	if err != nil {
   934  		panic(err)
   935  	}
   936  
   937  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   938  		Stream: "race:italy",
   939  		MaxLen: 2,
   940  		Values: map[string]interface{}{"rider": "Wood"},
   941  	},
   942  	).Result()
   943  
   944  	if err != nil {
   945  		panic(err)
   946  	}
   947  
   948  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
   949  		Stream: "race:italy",
   950  		MaxLen: 2,
   951  		Values: map[string]interface{}{"rider": "Henshaw"},
   952  	},
   953  	).Result()
   954  
   955  	if err != nil {
   956  		panic(err)
   957  	}
   958  
   959  	res35, err := rdb.XLen(ctx, "race:italy").Result()
   960  
   961  	if err != nil {
   962  		panic(err)
   963  	}
   964  
   965  	fmt.Println(res35) // >>> 2
   966  
   967  	res36, err := rdb.XRange(ctx, "race:italy", "-", "+").Result()
   968  
   969  	if err != nil {
   970  		panic(err)
   971  	}
   972  
   973  	// fmt.Println(res36)
   974  	// >>> [{1726649529170-1 map[rider:Wood]} {1726649529171-0 map[rider:Henshaw]}]
   975  	// STEP_END
   976  
   977  	// STEP_START xtrim
   978  	res37, err := rdb.XTrimMaxLen(ctx, "race:italy", 10).Result()
   979  
   980  	if err != nil {
   981  		panic(err)
   982  	}
   983  
   984  	fmt.Println(res37) // >>> 0
   985  	// STEP_END
   986  
   987  	// STEP_START xtrim2
   988  	res38, err := rdb.XTrimMaxLenApprox(ctx, "race:italy", 10, 20).Result()
   989  
   990  	if err != nil {
   991  		panic(err)
   992  	}
   993  
   994  	fmt.Println(res38) // >>> 0
   995  	// STEP_END
   996  
   997  	// REMOVE_START
   998  	UNUSED(res27, res34, res36)
   999  	// REMOVE_END
  1000  
  1001  	// Output:
  1002  	// [{race:italy [{1692632639151-0 map[rider:Castilla]}]}]
  1003  	// 1
  1004  	// [{race:italy []}]
  1005  	// [{race:italy [{1692632647899-0 map[rider:Royce]} {1692632662819-0 map[rider:Sam-Bodden]}]}]
  1006  	// &{2 1692632647899-0 1692632662819-0 map[Bob:2]}
  1007  	// [{1692632647899-0 map[rider:Royce]}]
  1008  	// [{1692632647899-0 map[rider:Royce]}]
  1009  	// [{1692632647899-0 map[rider:Royce]}]
  1010  	// 1692632662819-0
  1011  	// []
  1012  	// 0-0
  1013  	// &{5 1 2 1 1692632678249-0 0-0 5 {1692632639151-0 map[rider:Castilla]} {1692632678249-0 map[rider:Norem]} 1692632639151-0}
  1014  	// [{italy_riders 3 2 1692632662819-0 3 2}]
  1015  	// 2
  1016  	// 0
  1017  	// 0
  1018  }
  1019  
  1020  func ExampleClient_xdel() {
  1021  	ctx := context.Background()
  1022  
  1023  	rdb := redis.NewClient(&redis.Options{
  1024  		Addr:     "localhost:6379",
  1025  		Password: "", // no password docs
  1026  		DB:       0,  // use default DB
  1027  	})
  1028  
  1029  	// REMOVE_START
  1030  	// start with fresh database
  1031  	rdb.FlushDB(ctx)
  1032  	rdb.Del(ctx, "race:italy")
  1033  	// REMOVE_END
  1034  
  1035  	_, err := rdb.XAdd(ctx, &redis.XAddArgs{
  1036  		Stream: "race:italy",
  1037  		MaxLen: 2,
  1038  		Values: map[string]interface{}{"rider": "Wood"},
  1039  		ID:     "1692633198206-0",
  1040  	},
  1041  	).Result()
  1042  
  1043  	if err != nil {
  1044  		panic(err)
  1045  	}
  1046  
  1047  	_, err = rdb.XAdd(ctx, &redis.XAddArgs{
  1048  		Stream: "race:italy",
  1049  		MaxLen: 2,
  1050  		Values: map[string]interface{}{"rider": "Henshaw"},
  1051  		ID:     "1692633208557-0",
  1052  	},
  1053  	).Result()
  1054  
  1055  	if err != nil {
  1056  		panic(err)
  1057  	}
  1058  
  1059  	// STEP_START xdel
  1060  	res39, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result()
  1061  
  1062  	if err != nil {
  1063  		panic(err)
  1064  	}
  1065  
  1066  	fmt.Println(res39)
  1067  	// >>> [{1692633198206-0 map[rider:Wood]} {1692633208557-0 map[rider:Henshaw]}]
  1068  
  1069  	res40, err := rdb.XDel(ctx, "race:italy", "1692633208557-0").Result()
  1070  
  1071  	if err != nil {
  1072  		panic(err)
  1073  	}
  1074  
  1075  	fmt.Println(res40) // 1
  1076  
  1077  	res41, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result()
  1078  
  1079  	if err != nil {
  1080  		panic(err)
  1081  	}
  1082  
  1083  	fmt.Println(res41)
  1084  	// >>> [{1692633198206-0 map[rider:Wood]}]
  1085  	// STEP_END
  1086  
  1087  	// Output:
  1088  	// [{1692633198206-0 map[rider:Wood]} {1692633208557-0 map[rider:Henshaw]}]
  1089  	// 1
  1090  	// [{1692633198206-0 map[rider:Wood]}]
  1091  }
  1092  

View as plain text