📅  最后修改于: 2023-12-03 15:19:47.355000             🧑  作者: Mango
Redis is an in-memory data structure store, used as a database, cache, and message broker. In this quickstart guide, we will walk you through the basics of Redis installation and usage via the Shell/Bash command line.
To install Redis, follow these steps:
$ sudo apt-get update
$ sudo apt-get install redis
$ redis-server
$ redis-cli ping
You should receive the response PONG
.
Redis provides several commands for working with data:
To store a key-value pair in Redis, use the SET
command:
$ redis-cli SET key value
To retrieve the value associated with a key, use the GET
command:
$ redis-cli GET key
To increment the value of a key by one, use the INCR
command:
$ redis-cli INCR key
To decrement the value of a key by one, use the DECR
command:
$ redis-cli DECR key
To list all keys in Redis, use the KEYS
command:
$ redis-cli KEYS *
To delete a key-value pair, use the DEL
command:
$ redis-cli DEL key
Redis supports several data structures including strings, hashes, lists, sets, and sorted sets.
Strings are the simplest data structure in Redis. To store a string, use the SET
command:
$ redis-cli SET key "hello world"
To retrieve a string, use the GET
command:
$ redis-cli GET key
Hashes are used to store key-value pairs within a single Redis key. To set a hash, use the HSET
command:
$ redis-cli HSET key field value
To retrieve a hash value, use the HGET
command:
$ redis-cli HGET key field
Lists are used to store ordered sequences of strings, similar to a list in Python. To append a value to a list, use the RPUSH
command:
$ redis-cli RPUSH key value
To retrieve a list, use the LRANGE
command:
$ redis-cli LRANGE key 0 -1
Sets are used to store unique values. To add a value to a set, use the SADD
command:
$ redis-cli SADD key value
To retrieve all values in a set, use the SMEMBERS
command:
$ redis-cli SMEMBERS key
Sorted sets are used to store unique values with an associated score. To add a value to a sorted set, use the ZADD
command:
$ redis-cli ZADD key score value
To retrieve all values in a sorted set, use the ZRANGE
command:
$ redis-cli ZRANGE key 0 -1 WITHSCORES
In this quickstart guide, we covered the basics of Redis installation and usage via the Shell/Bash command line. There are many more Redis commands and data structures available. Refer to the Redis documentation for more information.