📜  redis quicstart - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:19:47.355000             🧑  作者: Mango

Redis Quickstart - Shell-Bash

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.

Installation

To install Redis, follow these steps:

  1. Open your terminal and type the following command:
$ sudo apt-get update 
  1. Install Redis by running the command below:
$ sudo apt-get install redis
  1. Once Redis is installed, start the Redis server with:
$ redis-server
  1. Verify that Redis is running by typing the command:
$ redis-cli ping

You should receive the response PONG.

Basic Redis Commands

Redis provides several commands for working with data:

SET and GET

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 
INCR and DECR

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 
KEYS and DEL

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 Data Structures

Redis supports several data structures including strings, hashes, lists, sets, and sorted sets.

Strings

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

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

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

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

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
Conclusion

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.