|
| 1 | +// EXAMPLE: cmds_generic |
| 2 | +// REMOVE_START |
| 3 | +import assert from "node:assert"; |
| 4 | +// REMOVE_END |
| 5 | + |
| 6 | +// HIDE_START |
| 7 | +import { createClient } from 'redis'; |
| 8 | + |
| 9 | +const client = createClient(); |
| 10 | +await client.connect().catch(console.error); |
| 11 | +// HIDE_END |
| 12 | + |
| 13 | +// STEP_START del |
| 14 | +const delRes1 = await client.set('key1', 'Hello'); |
| 15 | +console.log(delRes1); // OK |
| 16 | + |
| 17 | +const delRes2 = await client.set('key2', 'World'); |
| 18 | +console.log(delRes2); // OK |
| 19 | + |
| 20 | +const delRes3 = await client.del(['key1', 'key2', 'key3']); |
| 21 | +console.log(delRes3); // 2 |
| 22 | +// REMOVE_START |
| 23 | +assert.equal(delRes3, 2); |
| 24 | +// REMOVE_END |
| 25 | +// STEP_END |
| 26 | + |
| 27 | +// STEP_START expire |
| 28 | +const expireRes1 = await client.set('mykey', 'Hello'); |
| 29 | +console.log(expireRes1); // OK |
| 30 | + |
| 31 | +const expireRes2 = await client.expire('mykey', 10); |
| 32 | +console.log(expireRes2); // true |
| 33 | + |
| 34 | +const expireRes3 = await client.ttl('mykey'); |
| 35 | +console.log(expireRes3); // 10 |
| 36 | +// REMOVE_START |
| 37 | +assert.equal(expireRes3, 10); |
| 38 | +// REMOVE_END |
| 39 | + |
| 40 | +const expireRes4 = await client.set('mykey', 'Hello World'); |
| 41 | +console.log(expireRes4); // OK |
| 42 | + |
| 43 | +const expireRes5 = await client.ttl('mykey'); |
| 44 | +console.log(expireRes5); // -1 |
| 45 | +// REMOVE_START |
| 46 | +assert.equal(expireRes5, -1); |
| 47 | +// REMOVE_END |
| 48 | + |
| 49 | +const expireRes6 = await client.expire('mykey', 10, "XX"); |
| 50 | +console.log(expireRes6); // false |
| 51 | +// REMOVE_START |
| 52 | +assert.equal(expireRes6, false) |
| 53 | +// REMOVE_END |
| 54 | + |
| 55 | +const expireRes7 = await client.ttl('mykey'); |
| 56 | +console.log(expireRes7); // -1 |
| 57 | +// REMOVE_START |
| 58 | +assert.equal(expireRes7, -1); |
| 59 | +// REMOVE_END |
| 60 | + |
| 61 | +const expireRes8 = await client.expire('mykey', 10, "NX"); |
| 62 | +console.log(expireRes8); // true |
| 63 | +// REMOVE_START |
| 64 | +assert.equal(expireRes8, true); |
| 65 | +// REMOVE_END |
| 66 | + |
| 67 | +const expireRes9 = await client.ttl('mykey'); |
| 68 | +console.log(expireRes9); // 10 |
| 69 | +// REMOVE_START |
| 70 | +assert.equal(expireRes9, 10); |
| 71 | +await client.del('mykey'); |
| 72 | +// REMOVE_END |
| 73 | +// STEP_END |
| 74 | + |
| 75 | +// STEP_START ttl |
| 76 | +const ttlRes1 = await client.set('mykey', 'Hello'); |
| 77 | +console.log(ttlRes1); // OK |
| 78 | + |
| 79 | +const ttlRes2 = await client.expire('mykey', 10); |
| 80 | +console.log(ttlRes2); // true |
| 81 | + |
| 82 | +const ttlRes3 = await client.ttl('mykey'); |
| 83 | +console.log(ttlRes3); // 10 |
| 84 | +// REMOVE_START |
| 85 | +assert.equal(ttlRes3, 10); |
| 86 | +await client.del('mykey'); |
| 87 | +// REMOVE_END |
| 88 | +// STEP_END |
| 89 | + |
| 90 | +// STEP_START scan1 |
| 91 | +const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']); |
| 92 | +console.log(scan1Res1); // 6 |
| 93 | + |
| 94 | +const scan1Res2 = []; |
| 95 | +for await (const value of client.sScanIterator('myset', { MATCH: 'f*' })) { |
| 96 | + scan1Res2.push(value); |
| 97 | +} |
| 98 | +console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood'] |
| 99 | +// REMOVE_START |
| 100 | +console.assert(scan1Res2.sort().toString() === ['foo', 'foobar', 'feelsgood'].sort().toString()); |
| 101 | +await client.del('myset'); |
| 102 | +// REMOVE_END |
| 103 | +// STEP_END |
| 104 | + |
| 105 | +// STEP_START scan2 |
| 106 | +// REMOVE_START |
| 107 | +for (let i = 1; i <= 1000; i++) { |
| 108 | + await client.set(`key:${i}`, i); |
| 109 | +} |
| 110 | +// REMOVE_END |
| 111 | +let cursor = '0'; |
| 112 | +let scanResult; |
| 113 | + |
| 114 | +scanResult = await client.scan(cursor, { MATCH: '*11*' }); |
| 115 | +console.log(scanResult.cursor, scanResult.keys); |
| 116 | + |
| 117 | +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); |
| 118 | +console.log(scanResult.cursor, scanResult.keys); |
| 119 | + |
| 120 | +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); |
| 121 | +console.log(scanResult.cursor, scanResult.keys); |
| 122 | + |
| 123 | +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); |
| 124 | +console.log(scanResult.cursor, scanResult.keys); |
| 125 | + |
| 126 | +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 }); |
| 127 | +console.log(scanResult.cursor, scanResult.keys); |
| 128 | +// REMOVE_START |
| 129 | +console.assert(scanResult.keys.length === 18); |
| 130 | +cursor = '0'; |
| 131 | +const prefix = 'key:*'; |
| 132 | +while (cursor !== 0) { |
| 133 | + scanResult = await client.scan(cursor, { MATCH: prefix, COUNT: 1000 }); |
| 134 | + console.log(scanResult.cursor, scanResult.keys); |
| 135 | + cursor = scanResult.cursor; |
| 136 | + const keys = scanResult.keys; |
| 137 | + if (keys.length) { |
| 138 | + await client.del(keys); |
| 139 | + } |
| 140 | +} |
| 141 | +// REMOVE_END |
| 142 | +// STEP_END |
| 143 | + |
| 144 | +// STEP_START scan3 |
| 145 | +const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' }); |
| 146 | +console.log(scan3Res1); // 1 |
| 147 | + |
| 148 | +const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]); |
| 149 | +console.log(scan3Res2); // 1 |
| 150 | + |
| 151 | +const scan3Res3 = await client.type('geokey'); |
| 152 | +console.log(scan3Res3); // zset |
| 153 | +// REMOVE_START |
| 154 | +console.assert(scan3Res3 === 'zset'); |
| 155 | +// REMOVE_END |
| 156 | + |
| 157 | +const scan3Res4 = await client.type('zkey'); |
| 158 | +console.log(scan3Res4); // zset |
| 159 | +// REMOVE_START |
| 160 | +console.assert(scan3Res4 === 'zset'); |
| 161 | +// REMOVE_END |
| 162 | + |
| 163 | +const scan3Res5 = await client.scan('0', { TYPE: 'zset' }); |
| 164 | +console.log(scan3Res5.keys); // ['zkey', 'geokey'] |
| 165 | +// REMOVE_START |
| 166 | +console.assert(scan3Res5.keys.sort().toString() === ['zkey', 'geokey'].sort().toString()); |
| 167 | +await client.del(['geokey', 'zkey']); |
| 168 | +// REMOVE_END |
| 169 | +// STEP_END |
| 170 | + |
| 171 | +// STEP_START scan4 |
| 172 | +const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 }); |
| 173 | +console.log(scan4Res1); // 2 |
| 174 | + |
| 175 | +const scan4Res2 = await client.hScan('myhash', 0); |
| 176 | +console.log(scan4Res2.tuples); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}] |
| 177 | +// REMOVE_START |
| 178 | +assert.deepEqual(scan4Res2.tuples, [ |
| 179 | + { field: 'a', value: '1' }, |
| 180 | + { field: 'b', value: '2' } |
| 181 | +]); |
| 182 | +// REMOVE_END |
| 183 | + |
| 184 | +const scan4Res3 = await client.hScan('myhash', 0, { COUNT: 10 }); |
| 185 | +const items = scan4Res3.tuples.map((item) => item.field) |
| 186 | +console.log(items); // ['a', 'b'] |
| 187 | +// REMOVE_START |
| 188 | +assert.deepEqual(items, ['a', 'b']) |
| 189 | +await client.del('myhash'); |
| 190 | +// REMOVE_END |
| 191 | +// STEP_END |
| 192 | + |
| 193 | +// HIDE_START |
| 194 | +await client.quit(); |
| 195 | +// HIDE_END |
0 commit comments