Installing Redis on Windows is surprisingly easy!
Install Redis on Windows
- Go to the releases page of the Redis for Windows repo: https://github.com/MicrosoftArchive/redis/releases
- Download the 'Redis-x64-xxx.zip' file. You can use any version. Make sure you do not download the 'source code' zip.
- Unzip the file
- In the newly created folder, run redis-server.exe
- You should see a window appear that says redis is running on port 6379.
Run Redis
- Open a folder in Terminal
- Install redis
npm install --save redis
- Run node
node
- Run the following one after the other to get started with redis set and get
const redis = require('redis');
const redisUrl = 'redis://127.0.0.1:6379'
const client = redis.createClient(redisUrl)
client
client.set('hi', 'there');
client.get('hi', (err, value) => console.log(value));
client.get('hi', console.log);
Example of using hash inside values with hset and hget
client.hset('german', 'red', 'rot');
client.hget('german', 'red', console.log);
client.hset('german', 'blue', 'blau');
client.hget('german', 'blue', console.log);
Cannot set value to be Javascript Object
client.set('javascriptObject', { red: 'rojo' }); ❌
client.get('javascriptObject', console.log);
Output:
null '[object Object]'
Workaround: Use JSON.stringify()
client.set('jsonString', JSON.stringify({ red: 'rojo' })); ✔️
client.get('jsonString', console.log);
Output:
null '{"red":"rojo"}'
Get and convert back to Javascript Object
client.get('jsonString', (err, value) => console.log(JSON.parse(value)));
Output:
{ red: 'rojo' }
Comments
Post a Comment