Redis and other key-value caching products have the following three characteristics:

  • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
  • Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, namely, data backup in master-slave mode.
  • The official documentation
  • 英 文文档 #2.Redis installation ##1. Windows installation after downloading the official file, you can install. Enter the command in the command window
redis-cli
Copy the code

  • Setting up Remote Access

# # 2. Linux installation

  • Download the installation package and decompress it. Open the command window in the folder
# input command
make

Enter a new command in the current window
sudo make install
Copy the code
  • Set up Redis to run in the background

  • Start and stop the Redis service
Start the Redis service
./redis-server redis.conf 

Check whether the Redis service process is started successfully
ps -ef | grep redis
ps -A | grep redis

# Redis client starts
redis-cli

# Close the Redis service
ps -A | grep redis
Copy the code

#3. Redis common configuration

  • Configuration file redis.conf
  • Common Configuration Items
    • Bind 127.0.0.1 [bind IP address]
    • Port 6379 [default access address 6379]
    • Daemonize yes [whether the backend process < daemon > is running]
    • Dbfilename dump. RDB [file to store data]
    • Dir./. [Path where data is stored]
  • Data types in Redis
  • ** Redis data store: **key=value key value pairs
  • **key< key > data type: ** string
  • Value < value type > :
    • String string
    • Hash hash
    • List the list
    • The set collection
    • Zset ordered set

#4. Redis data operation

  • String: string operation

Setex key seconds value: setex key seconds value: setex key seconds value, expiration time in seconds **mset key value [key value] : ** Sets multiple key-value pairs

**mget key [key] : ** Get multiple values based on multiple keys

** Incrby key increment: ** Incrby key increment: ** Incrby key increment: ** Incrby key increment: ** Incrby key increment: ** Incrby key increment: ** Incrby key increment: ** Increment the corresponding key value

** Append key value: ** Concatenate value after x **strlen key: ** Get the length of the value corresponding to the key

  • The key operation

Key: **exists, returns 1, otherwise returns 0 **type key: ** Del key: ** Expire key seconds: ** Expire key seconds: ** TTL key: ** Check the validity of the key (if the result is -1, it will expire, and -1 will never expire)

  • Hash: Stores objects in the form of key-value pairs

**hset key field value: ** Sets a single attribute **hmset key field value [field value]

**hget key field: **hmget key field [field] : **hgetall key: **hkeys key: ** HVALS Key: ** Get all values ** HEXISTS key Field: ** Check whether the attribute exists **hdel key field [field] : ** Delete attributes and values by attribute name **hstrlen key field: ** The length of the string returned by the value

  • List: Stores multiple data in an ordered manner

* * lpush key value (value) : list the head increase more data * * * * rpush key value (value) : * * * * list tail adding multiple data linsert key before | after privot value: ** Insert data after an element / **lset key index value: ** Sets the value of the element with the specified index

**lpop key: **lrange key start stop: ** Returns the range of data that exists in the list of keys

**llen key: ** Gets the length of the list **lindex key index: ** Gets the elements corresponding to the index in the list ** Ltrim key start stop: ** Gets the new list from start to stop

  • Set collection: Unordered storage of multiple data

**sadd key value [value] : ** Smembers key: ** Sismember key value: ** Scard key: ** Gets the number of elements in the key set

**sinter key [key] : ** Gets the intersection of multiple sets **sdiff key [key] : ** Gets the difference of multiple sets **sunion key [key] : ** Gets the union of multiple sets

  • Zset collection: Stores multiple data in an ordered manner
  • Sorted set
  • The element is of type string
  • Elements are unique and not repeated
  • Each element is associated with a score of type double, representing the weight by which the elements are sorted from smallest to largest
  • Elements can have the same score

**zadd key score value [score value] : ** Zrange key start stop: ** Get all elements in the specified range **zcard key: **zcount key min Max: **zscore key member: ** Returns score of member elements in the set

#5. Redis publishes subscriptions

  • Publishers do not plan to send messages to specific receivers (subscribers), but publish messages to different channels without knowing what subscribers subscribe to
  • Subscribers interested in one or more channels only need to receive messages of interest, and do not need to know what publisher published them
  • Decoupling of publishers and subscribers leads to greater scalability and a more dynamic network topology
  • A message sent by a client to a channel will be pushed to all clients that subscribe to the channel
  • Clients don’t need to actively get messages, just subscribe to the channel and the content of the channel will be pushed to them
  • Message format

The push message format consists of three parts

  • Part1: Message type, including three types

    • Subscribe: indicates that the subscription succeeds
    • Unsubscribe: indicates that the subscription is successfully cancelled
    • Message: indicates that other terminals publish messages
  • If the value of the first part is subscribe, the second part is the channel, and the third part is the number of channels currently subscribed

  • If the value of the first part is unsubscribe, then the second part is the channel, and the third part is the number of channels currently subscribed. If the value is 0, then there are no channels currently subscribed. When in a state other than Pub/Sub, the client can issue any redis command

  • If the value of the first part is message, the second part is the name of the source channel and the third part is the content of the message

Subscribe Channel name unsubscribe channel name publish channel message: Pushes a message to a specified channelCopy the code

## Open multiple command Windows:

  • The first window is the subscriber

Enter the command:

# start redis
redis-cli

# select database
select 0

# Subscribe channels
subscribe  zhiji
Copy the code
  • The second window acts as the client

Type the command

# start redis
redis-cli

# select database
select 1

# Post a message
publish  zhiji  'hellow'
Copy the code

#6. Active/standby

  • Master/slave configuration A master can have multiple slaves, and a slave can have multiple slaves. In this way, a powerful multi-level server cluster architecture is formed. For example, a machine at IP 192.168.1.10 is the master server, and a machine at IP 192.168.1.11 is the slave server

  • Bind 192.168.1.10 Bind the slave server. Note: Write the host IP address after Slaveof, and then write the port. The port must be written

Use redis.**. Conf to configure active/standby modebindConfigure the primary database server. Slaveof Configures the secondary database serverbind192.168.1.11 slaveof 192.168.1.10 6379 Run the info command on the master and slave to view the output. Write data to the mastersetHello World Reads data on the slave. Get HelloCopy the code