|
| 1 | +# Redis |
| 2 | + |
| 3 | +## Install Redis |
| 4 | + |
| 5 | +There are a few ways of |
| 6 | +[installing Redis itself](https://redis.io/docs/latest/operate/oss_and_stack/install/). |
| 7 | + |
| 8 | +::: [TIP] |
| 9 | + |
| 10 | +Note that Redis is a lightweight version, whereas Redis Stack contains more |
| 11 | +features. |
| 12 | + |
| 13 | +::: |
| 14 | + |
| 15 | +If you are using Docker, you should be able to access Redis without explicitly |
| 16 | +running it. If you have installed natively, you may need to run it: |
| 17 | + |
| 18 | +::: code-group |
| 19 | + |
| 20 | +```bash |
| 21 | +redis-server |
| 22 | +``` |
| 23 | + |
| 24 | +```bash |
| 25 | +# if using redis stack |
| 26 | +redis-stack-server |
| 27 | +``` |
| 28 | + |
| 29 | +::: |
| 30 | + |
| 31 | +## Install Redis Client |
| 32 | + |
| 33 | +There is a client library for Node.js which makes developing against a Redis |
| 34 | +database much easier. |
| 35 | + |
| 36 | +To install it, run |
| 37 | + |
| 38 | +```bash |
| 39 | +npm install redis |
| 40 | +``` |
| 41 | + |
| 42 | +in your Node project. |
| 43 | + |
| 44 | +## Basic operations |
| 45 | + |
| 46 | +At its core, Redis is concerned with setting and getting key/value pairs. |
| 47 | + |
| 48 | +```js |
| 49 | +// Import the redis package |
| 50 | +import redis from 'redis' |
| 51 | + |
| 52 | +// Create a Redis client instance |
| 53 | +const r = redis.createClient({ |
| 54 | + url: 'redis://localhost:6379' |
| 55 | +}) |
| 56 | + |
| 57 | +// Handle connection errors |
| 58 | +r.on('error', err => console.log('Redis Client Error', err)) |
| 59 | + |
| 60 | +// Connect the client |
| 61 | +await r.connect() |
| 62 | + |
| 63 | +// Basic Redis commands (set and get) |
| 64 | +await r.set('mykey', 'Hello Redis') // Set a key-value pair |
| 65 | +const value = await r.get('mykey') // Get the value of the key |
| 66 | +console.log('mykey:', value) |
| 67 | + |
| 68 | +// Close the connection when done |
| 69 | +await r.quit() |
| 70 | +``` |
| 71 | + |
| 72 | +## Working with JSON |
| 73 | + |
| 74 | +Redis can store JSON data without stringifying it. |
| 75 | + |
| 76 | +```js |
| 77 | +// Create a JSON object to store in Redis |
| 78 | +const user = { |
| 79 | + id: 'user:1001', |
| 80 | + name: 'Jane Doe', |
| 81 | + age: 25, |
| 82 | + |
| 83 | + address: { |
| 84 | + city: 'London', |
| 85 | + country: 'UK' |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Set the JSON object using RedisJSON (json.set) |
| 90 | +await r.json.set(user.id, '$', user) |
| 91 | + |
| 92 | +const storedUser = await r.json.get(user.id) |
| 93 | +``` |
| 94 | + |
| 95 | +The keys can be manipulated individually: |
| 96 | + |
| 97 | +```js |
| 98 | +// Update the age field |
| 99 | +await r.json.set('user:1001', '$.age', 26) |
| 100 | + |
| 101 | +// Read the email field |
| 102 | +const userEmail = await r.json.get('user:1001', { path: '$.email' }) |
| 103 | +``` |
| 104 | + |
| 105 | +## Search index |
| 106 | + |
| 107 | +We can create a search index which allows us to implement full text search. |
| 108 | + |
| 109 | +```js |
| 110 | +// Create a new search index using RedisSearch |
| 111 | +await r.ft.create('userIndex' |
| 112 | + { |
| 113 | + '$.name': { |
| 114 | + type: 'TEXT', // Full-text search over the 'name' field |
| 115 | + sortable: true // Allows sorting by this field |
| 116 | + }, |
| 117 | + '$.age': { |
| 118 | + type: 'NUMERIC', // Numeric field to support range queries |
| 119 | + } |
| 120 | + }, |
| 121 | + { |
| 122 | + ON: 'JSON', // We're indexing JSON documents |
| 123 | + PREFIX: 'user:' // Only index keys that start with 'user:' |
| 124 | + } |
| 125 | +) |
| 126 | +``` |
| 127 | + |
| 128 | +Now we can search for users by name or age. |
| 129 | + |
| 130 | +```js |
| 131 | +const result1 = await r.ft.search('userIdx', '@name:Jane') |
| 132 | +const result2 = await r.ft.search('userIdx', '@age:[25 35]') |
| 133 | +``` |
0 commit comments