|
| 1 | +# Redis Arrays |
| 2 | + |
| 3 | +Redis Arrays provide sparse arrays of arbitrary Redis values with unsigned array indexes and a notional write head. SE.Redis exposes the array API as experimental Redis 8.8 APIs; callers should expect details to change while the server feature is still in preview. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Arrays require Redis 8.8 or later. The APIs are marked with the `SER006` experimental warning. |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +Use `ArraySetAsync` and `ArrayGetAsync` to write and read individual cells: |
| 12 | + |
| 13 | +```csharp |
| 14 | +var db = conn.GetDatabase(); |
| 15 | +RedisKey key = "events"; |
| 16 | + |
| 17 | +bool inserted = await db.ArraySetAsync(key, 0, "created"); |
| 18 | +RedisValue value = await db.ArrayGetAsync(key, 0); |
| 19 | +RedisValue missing = await db.ArrayGetAsync(key, 1); |
| 20 | + |
| 21 | +Console.WriteLine(inserted); // True when the cell did not previously have a value |
| 22 | +Console.WriteLine(value); // created |
| 23 | +Console.WriteLine(missing.IsNull); // True |
| 24 | +``` |
| 25 | + |
| 26 | +Array indexes use `RedisArrayIndex`, with implicit conversions from `int`, `long`, and `ulong`. This allows normal small indexes to be used directly, while still allowing the full unsigned index range when needed. |
| 27 | + |
| 28 | +```csharp |
| 29 | +await db.ArraySetAsync(key, 42, "answer"); |
| 30 | +await db.ArraySetAsync(key, new RedisArrayIndex(10_000_000UL), "large index"); |
| 31 | +``` |
| 32 | + |
| 33 | +## Sparse Arrays |
| 34 | + |
| 35 | +Arrays are sparse: unset cells do not have values. `ArrayLengthAsync` reports the notional length, which is the highest used index plus one. `ArrayCountAsync` reports only cells that currently have values. |
| 36 | + |
| 37 | +```csharp |
| 38 | +await db.KeyDeleteAsync(key); |
| 39 | + |
| 40 | +await db.ArraySetAsync(key, 0, "a"); |
| 41 | +await db.ArraySetAsync(key, 10, "b"); |
| 42 | + |
| 43 | +RedisArrayIndex length = await db.ArrayLengthAsync(key); // 11 |
| 44 | +RedisArrayIndex count = await db.ArrayCountAsync(key); // 2 |
| 45 | +``` |
| 46 | + |
| 47 | +## Setting Multiple Values |
| 48 | + |
| 49 | +To write a contiguous range, pass the first index and the values: |
| 50 | + |
| 51 | +```csharp |
| 52 | +int inserted = await db.ArraySetAsync(key, 0, ["a", "b", "c"]); |
| 53 | +``` |
| 54 | + |
| 55 | +To write multiple specific indexes, use `RedisArrayEntry` values: |
| 56 | + |
| 57 | +```csharp |
| 58 | +await db.ArraySetAsync(key, |
| 59 | +[ |
| 60 | + new RedisArrayEntry(0, "alpha"), |
| 61 | + new RedisArrayEntry(5, "bravo"), |
| 62 | + new RedisArrayEntry(100, "charlie"), |
| 63 | +]); |
| 64 | +``` |
| 65 | + |
| 66 | +The returned `int` is the number of cells that were newly filled. |
| 67 | + |
| 68 | +## Reading Multiple Values |
| 69 | + |
| 70 | +Read selected indexes with `ArrayGetAsync`: |
| 71 | + |
| 72 | +```csharp |
| 73 | +RedisValue[] values = await db.ArrayGetAsync(key, [0, 5, 6, 100]); |
| 74 | +``` |
| 75 | + |
| 76 | +Read a range with `ArrayGetRangeAsync`. Ranges can be read forward or backward: |
| 77 | + |
| 78 | +```csharp |
| 79 | +RedisValue[] forward = await db.ArrayGetRangeAsync(key, 0, 5); |
| 80 | +RedisValue[] reverse = await db.ArrayGetRangeAsync(key, 5, 0); |
| 81 | +``` |
| 82 | + |
| 83 | +For sparse arrays, use `ArrayScanAsync` to return only populated cells in a range: |
| 84 | + |
| 85 | +```csharp |
| 86 | +RedisArrayEntry[] entries = await db.ArrayScanAsync(key, 0, 100, limit: 50); |
| 87 | + |
| 88 | +foreach (var entry in entries) |
| 89 | +{ |
| 90 | + Console.WriteLine($"{entry.Index}: {entry.Value}"); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Deleting Values |
| 95 | + |
| 96 | +Delete a single cell with `ArrayDeleteAsync`: |
| 97 | + |
| 98 | +```csharp |
| 99 | +bool removed = await db.ArrayDeleteAsync(key, 5); |
| 100 | +``` |
| 101 | + |
| 102 | +Delete multiple specific cells by index: |
| 103 | + |
| 104 | +```csharp |
| 105 | +int removedCount = await db.ArrayDeleteAsync(key, [0, 5, 100]); |
| 106 | +``` |
| 107 | + |
| 108 | +Delete one or more ranges: |
| 109 | + |
| 110 | +```csharp |
| 111 | +await db.ArrayDeleteRangeAsync(key, 10, 20); |
| 112 | + |
| 113 | +await db.ArrayDeleteRangeAsync(key, |
| 114 | +[ |
| 115 | + new RedisArrayRange(100, 199), |
| 116 | + new RedisArrayRange(500, 599), |
| 117 | +]); |
| 118 | +``` |
| 119 | + |
| 120 | +## Searching |
| 121 | + |
| 122 | +Use `ArrayGrepRequest` with `ArrayGrepAsync` to search values. When `Start` or `End` is not specified, the server's open-ended lower or upper bound is used. |
| 123 | + |
| 124 | +```csharp |
| 125 | +var request = new ArrayGrepRequest |
| 126 | +{ |
| 127 | + Limit = 10, |
| 128 | +}; |
| 129 | +request.AddPredicate(ArrayGrepRequest.Predicate.Match("error")); |
| 130 | + |
| 131 | +RedisArrayEntry[] matches = await db.ArrayGrepAsync(key, request); |
| 132 | + |
| 133 | +foreach (var match in matches) |
| 134 | +{ |
| 135 | + Console.WriteLine(match.Index); |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Set `IncludeValues` to return values along with the matching indexes: |
| 140 | + |
| 141 | +```csharp |
| 142 | +var request = new ArrayGrepRequest |
| 143 | +{ |
| 144 | + IncludeValues = true, |
| 145 | +}; |
| 146 | +request.AddPredicate(ArrayGrepRequest.Predicate.Regex("^ERR[0-9]+")); |
| 147 | + |
| 148 | +RedisArrayEntry[] matches = await db.ArrayGrepAsync(key, request); |
| 149 | + |
| 150 | +foreach (var match in matches) |
| 151 | +{ |
| 152 | + Console.WriteLine($"{match.Index}: {match.Value}"); |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +Multiple predicates can be combined. By default, predicates are combined as `OR`; set `IsIntersection` to combine them as `AND`. |
| 157 | + |
| 158 | +```csharp |
| 159 | +var request = new ArrayGrepRequest |
| 160 | +{ |
| 161 | + IsIntersection = true, |
| 162 | +}; |
| 163 | +request.AddPredicate(ArrayGrepRequest.Predicate.Match("redis")); |
| 164 | +request.AddPredicate(ArrayGrepRequest.Predicate.Glob("*array*")); |
| 165 | + |
| 166 | +RedisArrayEntry[] matches = await db.ArrayGrepAsync(key, request); |
| 167 | +``` |
| 168 | + |
| 169 | +## Write Head |
| 170 | + |
| 171 | +Arrays have a write head used by insert operations. `ArrayInsertAsync` writes at the current write head and advances it. |
| 172 | + |
| 173 | +```csharp |
| 174 | +RedisArrayIndex first = await db.ArrayInsertAsync(key, "first"); |
| 175 | +RedisArrayIndex second = await db.ArrayInsertAsync(key, "second"); |
| 176 | + |
| 177 | +RedisArrayIndex? next = await db.ArrayNextAsync(key); |
| 178 | +``` |
| 179 | + |
| 180 | +Move the write head with `ArraySeekAsync`: |
| 181 | + |
| 182 | +```csharp |
| 183 | +bool moved = await db.ArraySeekAsync(key, 1_000); |
| 184 | +RedisArrayIndex written = await db.ArrayInsertAsync(key, "later"); |
| 185 | +``` |
| 186 | + |
| 187 | +## Ring Buffers |
| 188 | + |
| 189 | +Use `ArrayRingAsync` to keep at most a fixed number of cells and wrap writes around that capacity: |
| 190 | + |
| 191 | +```csharp |
| 192 | +for (int i = 0; i < 10; i++) |
| 193 | +{ |
| 194 | + await db.ArrayRingAsync(key, maxLength: 5, value: i); |
| 195 | +} |
| 196 | + |
| 197 | +RedisArrayIndex count = await db.ArrayCountAsync(key); // 5 |
| 198 | +``` |
| 199 | + |
| 200 | +`ArrayLastItemsAsync` is intended for this capped ring-buffer model. It reads the last values in the ring-buffer sense, where "last" relates to the retained values after wrap-around and trimming: |
| 201 | + |
| 202 | +```csharp |
| 203 | +RedisValue[] last = await db.ArrayLastItemsAsync(key, count: 10); |
| 204 | +RedisValue[] lastReversed = await db.ArrayLastItemsAsync(key, count: 10, reverse: true); |
| 205 | +``` |
| 206 | + |
| 207 | +## Operations and Info |
| 208 | + |
| 209 | +Use `ArrayOperationAsync` for simple server-side operations over a range: |
| 210 | + |
| 211 | +```csharp |
| 212 | +RedisValue sum = await db.ArrayOperationAsync(key, 0, 10, ArrayOperation.Sum); |
| 213 | +RedisValue used = await db.ArrayOperationAsync(key, 0, 10, ArrayOperation.Used); |
| 214 | +RedisValue matches = await db.ArrayOperationAsync(key, 0, 10, ArrayOperation.Match, "error"); |
| 215 | +``` |
| 216 | + |
| 217 | +Use `ArrayInfoAsync` for metadata: |
| 218 | + |
| 219 | +```csharp |
| 220 | +ArrayInfo info = await db.ArrayInfoAsync(key); |
| 221 | + |
| 222 | +Console.WriteLine($"Count: {info.Count}"); |
| 223 | +Console.WriteLine($"Length: {info.Length}"); |
| 224 | +Console.WriteLine($"Next insert index: {info.NextInsertIndex}"); |
| 225 | +``` |
0 commit comments