Installation and Simple Use of Redis

Install Redis on Windows

Download and install Redis: Releases · tporadowski/redis (github.com)


Use Redis in cmd

Open the cmd window in the Redis installation directory and execute the following command to start the service:

1
redis-cli.exe

Enter ping to test whether the Redis server is running normally:

The Redis ping command is used by the client to send a PING to the Redis server. If the server is operating normally, a PONG will be returned.

Now we can add key-value pairs to Redis:

Query the key-value pairs just added:


Use Redis in Python

Install Redis in Python:

1
pip install redis

Now we can use Redis in Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import redis

# Connect to the local Redis server
redis_data = redis.Redis(host='localhost', port=6379, db=0)

old_keys = redis_data.keys()
print("All keys in Redis before adding data:")
print(old_keys)

# Add key-value pairs to Redis
redis_data.set("id_1", 123456)
redis_data.set("id_2", 456789)

# Query keys in Redis
print("\nQuery the new keys:")
value_1 = redis_data.get("id_1")
print("id_1: " + str(value_1))
value_2 = redis_data.get("id_2")
print("id_2: " + str(value_2))

new_keys = redis_data.keys()
print("\nAll keys in Redis after adding data:")
print(new_keys)

The output of the program is as follows:

1
2
3
4
5
6
7
8
9
All keys in Redis before adding data:
[b'key_2', b'key_1']

Query the new keys:
id_1: b'123456'
id_2: b'456789'

All keys in Redis after adding data:
[b'key_2', b'id_1', b'key_1', b'id_2']

Use Redis in GUI client

Redis can also be used through Redis desktop manager, here we use Redis Insight.

Download Redis Insight from here: https://redis.io/insight/

Now we can use Redis in the GUI: