Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Build a micro-service framework and test environment-3-Redis

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In recent years, Redis has been widely used as an in-memory database to cache important information; it can also be used as a NoSQL database to support the storage of string (string), list (linked list), set (collection), zset (sorted set ordered collection) and hash (hash type). The content of this article is a chapter of the internal training notes written in November 2018. The demonstration platform is the windows operating system. The configuration under Linux is about different.

The configuration and use of reidis clusters are not included in this article.

The following excerpts:

3.4.1 download

Address: https://redis.io/download

Download the file: redis-5.0.0.tar.gz, extract it to: d:\ redis-5.0.0, and then compile using gcc.

If you compile with vs2017, you need to download: https://github.com/MicrosoftArchive/redis.

Clone to D:\ redis-3.0.504.

Open the sln file using VS2017:

Build the solution:

Get the debug version of the redis system file:

D:\ redis-3.0.504\ redis\ msvs\ x64\ Debug

There are also compiled ones, download the URL: https://github.com/ServiceStack/redis-windows

3.4.2 Environment variables and settings

Add D:\ redis-3.0.504\ redis\ msvs\ x64\ Debug to path.

Enter D:\ redis-3.0.504\ redis\ msvs\ x64\ Debug to add a new file: redis.windows.conf.

Add text content:

3.4.3 start

Start redis:

Start the redis-cli command line:

Test:

Start the first cmd window:

Launch the second cmd window:

Looking back at the first cmd window:

3.4.4 Common instructions

1. Redis connection command

1. Start redis:

Local launch: redis-cli

Remote launch: redis-cli-h host-p port-a password

2 、 AUTH password

Verify that the password is correct

3 、 ECHO message

Print string

4 、 PING

Check to see if the service is running

5 、 QUIT

Close the current connection

6 、 SELECT index

Switch to the specified database

II. Redis keys command

1 、 DEL key

DUMP key

Serializes the given key and returns the serialized value

2 、 EXISTS key

Check whether the given key exists

3 、 EXPIRE key seconds

Set the expiration time for key

4 、 EXPIRE key timestamp

Set the expiration time for key by timestamp

5 、 PEXPIRE key milliseconds

Set the expiration time of key in milliseconds

6 、 KEYS pattern

Find all key that match a given pattern

7 、 MOVE key db

Move the key of the current database to the database db

8 、 PERSIST key

Remove the expiration time of key, and key will persist.

9 、 PTTL key

Returns the remaining expiration time of key in milliseconds

10 、 TTL key

Returns the remaining lifetime of a given key in seconds

11 、 RANDOMKEY

Randomly returns a key from the current database

12 、 RENAME key newkey

Modify the name of key

13 、 RENAMENX key newkey

Rename key to newkey only if newkey does not exist

14 、 TYPE key

Returns the type of value stored by key

3. Reids string command

1 、 SET key value

Set key value

2 、 GET key

Get the key value

3 、 GETRANGE key start end

Returns the subcharacters of the string value in key

4 、 GETSET key value

Sets the value of the given key to value and returns the old value of key

5 、 GETBIT KEY OFFSET

Gets the bit at the specified offset for the string value stored in key

6 、 MGET KEY1 KEY2

Gets the value of one or more given key

7 、 SETBIT KEY OFFSET VALUE

Sets or clears the bit on the specified offset for the string value stored by key

8 、 SETEX key seconds value

Associate the value value to key and set the expiration time of key to seconds (in seconds).

9 、 SETNX key value

The value of key is set only if key does not exist.

10 、 SETRANGE key offset value

Overwrites the string value stored in a given key with the value parameter, starting with the offset offset.

11 、 STRLEN key

Returns the length of the string value stored by key.

12 、 MSET key value [key value...]

Set one or more key-value pairs simultaneously.

13 、 MSETNX key value [key value...]

Set one or more key-value pairs at the same time if and only if none of the given key exists.

14 、 PSETEX key milliseconds value

This command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds, rather than seconds, as the SETEX command does.

15 、 INCR key

Increments the numerical value stored in key by one.

16 、 INCRBY key increment

Adds the value stored by key to the given incremental value (increment).

17 、 INCRBYFLOAT key increment

Adds the value stored by key to the given floating-point increment value (increment).

18 、 DECR key

Subtracts the numeric value stored in key by one.

19 、 DECRBY key decrement

The value stored by the key minus the given decrement.

20 、 APPEND key value

If key already exists and is a string, the APPEND command appends the specified value to the end of the original value (value) of the change key.

4. Redis hash command

1 、 HDEL key field1 [field2]

Delete one or more hash table fields

2 、 HEXISTS key field

Check whether the specified field exists in the hash table key.

/ 3 、 HGET key field

Gets the value of the specified field stored in the hash table.

/ 4 、 HGETALL key

Gets all the fields and values that specify key in the hash table

/ 5 、 HINCRBY key field increment

Adds an incremental increment to the integer value of the specified field in the hash table key.

/ 6 、 HINCRBYFLOAT key field increment

Adds an incremental increment to the floating-point value of the specified field in the hash table key.

/ 7 、 HKEYS key

Get all the fields in the hash table

/ 8 、 HLEN key

Gets the number of fields in the hash table

/ 9 、 HMGET key field1 [field2]

Get the values of all given fields

/ 10 、 HMSET key field1 value1 [field2 value2]

Multiple field-value (field-value) pairs are set to the hash table key at the same time.

/ 11 、 HSET key field value

Set the value of field field in the hash table key to value.

/ 12 、 HSETNX key field value

Set the value of the hash table field only if the field field does not exist.

/ 13 、 HVALS key

Get all values in the hash table

/ 14 、 HSCAN key cursor [MATCH pattern] [COUNT count]

Key-value pairs in an iterative hash table.

5. Redis list command

/ 1 、 BLPOP key1 [key2] timeout

Move out and get the first element of the list, and if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

/ 2 、 BRPOP key1 [key2] timeout

Move out and get the last element of the list, and if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

/ 3 、 BRPOPLPUSH source destination timeout

Pop a value from the list, insert the pop-up element into another list and return it; if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

/ 4 、 LINDEX key index

Get the elements in the list through the index

/ 5. LINSERT key BEFORE | AFTER pivot value

Insert an element before or after an element in the list

/ 6 、 LLEN key

Get the list length

/ 7 、 LPOP key

Move out and get the first element of the list

/ 8 、 LPUSH key value1 [value2]

Insert one or more values into the list header

/ 9 、 LPUSHX key value

Insert a value into the existing list header

/ 10 、 LRANGE key start stop

Gets the elements within the specified range of the list

/ 11 、 LREM key count value

Remove list element

/ 12 、 LSET key index value

Set the value of a list element through an index

/ 13 、 LTRIM key start stop

Trim a list, that is, let the list retain only the elements within the specified range, and all elements that are not within the specified range will be deleted.

/ 14 、 RPOP key

Remove and get the last element of the list

/ 15 、 RPOPLPUSH source destination

Remove the last element of the list, add that element to another list and return

/ 16 、 RPUSH key value1 [value2]

Add one or more values to the list

/ 17 、 RPUSHX key value

Add values to an existing list

VI. Redis assembly command

/ 1 、 SADD key member1 [member2]

Add one or more members to the collection

/ 2 、 SCARD key

Gets the number of members of the collection

/ 3 、 SDIFF key1 [key2]

Returns the difference of all given sets

/ 4 、 SDIFFSTORE destination key1 [key2]

Returns the difference of all given sets and stores them in destination

/ 5 、 SINTER key1 [key2]

Returns the intersection of all given sets

/ 6 、 SINTERSTORE destination key1 [key2]

Returns the intersection of all given collections and stores them in destination

/ 7 、 SISMEMBER key member

Determine whether the member element is a member of the collection key

/ 8 、 SMEMBERS key

Returns all members of the collection

/ 9 、 SMOVE source destination member

Move member elements from the source collection to the destination collection

/ 10 、 SPOP key

Removes and returns a random element in the collection

/ 11 、 SRANDMEMBER key [count]

Returns one or more random numbers in the collection

/ 12 、 SREM key member1 [member2]

Remove one or more members of the collection

/ 13 、 SUNION key1 [key2]

Returns the union of all given sets

/ 14 、 SUNIONSTORE destination key1 [key2]

The union of all given collections is stored in the destination collection

/ 15 、 SSCAN key cursor [MATCH pattern] [COUNT count]

Iterate over the elements in a collection

7. Redis ordered assembly command

/ 1 、 ZADD key score1 member1 [score2 member2]

Add one or more members to an ordered collection, or update the scores of existing members

/ 2 、 ZCARD key

Get the number of members of an ordered set

/ 3 、 ZCOUNT key min max

Calculate the number of members with a specified interval score in an ordered set

/ 4 、 ZINCRBY key increment member

Add an incremental increment to the score of a specified member in an ordered set

/ 5 、 ZINTERSTORE destination numkeys key [key...]

Calculates the intersection of one or more given ordered sets and stores the result set in a new ordered set key

/ 6 、 ZLEXCOUNT key min max

Calculate the number of members in a specified dictionary interval in an ordered set

/ 7 、 ZRANGE key start stop [WITHSCORES]

Return the ordered set through the index interval to synthesize the members in the specified interval

/ 8 、 ZRANGEBYLEX key min max [LIMIT offset count]

Returns the members of an ordered set through a dictionary interval

/ 9 、 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

Returns the members of a specified interval in an ordered set by score

/ 10 、 ZRANK key member

Returns the index of the specified member in an ordered collection

/ 11 、 ZREM key member [member...]

Remove one or more members from an ordered set

/ 12 、 ZREMRANGEBYLEX key min max

Removes all members of a given dictionary interval in an ordered set

/ 13 、 ZREMRANGEBYRANK key start stop

Removes all members of a given ranking interval in an ordered set

/ 14 、 ZREMRANGEBYSCORE key min max

Remove all members of a given fraction interval in an ordered set

/ 15 、 ZREVRANGE key start stop [WITHSCORES]

Returns the members of the specified interval in an ordered set, with the score from high to low by index

/ 16 、 ZREVRANGEBYSCORE key max min [WITHSCORES]

Returns the members in the specified score range in an ordered set, sorted from high to low

/ 17 、 ZREVRANK key member

Returns the ranking of the specified members in an ordered set, sorted by decreasing scores (from large to small)

/ 18 、 ZSCORE key member

Returns the score value of the member in an ordered set

/ 19 、 ZUNIONSTORE destination numkeys key [key...]

Calculates the union of one or more given ordered sets and stores them in a new key

/ 20 、 ZSCAN key cursor [MATCH pattern] [COUNT count]

Elements in an iterative ordered set (including element members and element scores)

Redis publish and subscribe commands

/ 1 、 PSUBSCRIBE pattern [pattern...]

Subscribe to one or more channels that match a given pattern.

/ 2 、 PUBSUB subcommand [argument [argument...]]

View the status of the subscription and publishing system.

/ 3 、 PUBLISH channel message

Sends the information to the specified channel.

4 、 PUNSUBSCRIBE [pattern [pattern...]]

Unsubscribe from all channels in a given mode.

/ 5 、 SUBSCRIBE channel [channel...]

Subscribe to information for a given channel or channels.

/ 6 、 UNSUBSCRIBE [channel [channel...]]

To unsubscribe from a given channel.

IX. Redis transaction order

1 、 DISCARD

Cancels the transaction and discards all commands within the transaction block.

2 、 EXEC

Execute commands within all transaction blocks.

3 、 MULTI

Marks the beginning of a transaction block.

4 、 UNWATCH

Unmonitor all key from the WATCH command.

5 、 WATCH key [key...]

Monitor one (or more) key, and if the key (or these) is altered by other commands before the transaction is executed, the transaction will be interrupted.

10. Redis script commands

1 、 EVAL script numkeys key [key...] Arg [arg...]

Execute the Lua script.

/ 2 、 EVALSHA sha1 numkeys key [key...] Arg [arg...]

Execute the Lua script.

/ 3 、 SCRIPT EXISTS script [script...]

Check to see if the specified script has been saved in the cache.

4 、 SCRIPT FLUSH

Removes all scripts from the script cache.

/ 5 、 SCRIPT KILL

Kill the currently running Lua script.

/ 6 、 SCRIPT LOAD script

The script script is added to the script cache, but the script is not executed immediately.

11. Redis server command

/ 1 、 BGREWRITEAOF

Asynchronously perform an AOF (AppendOnly File) file rewrite operation

/ 2 、 BGSAVE

Asynchronously save the data of the current database to disk in the background

/ 3 、 CLIENT KILL [ip:port] [ID client-id]

Close client connection

/ 4 、 CLIENT LIST

Get a list of client connections to the server

/ 5 、 CLIENT GETNAME

Get the name of the connection

/ 6 、 CLIENT PAUSE timeout

Stop running commands from the client within a specified period of time

/ 7 、 CLIENT SETNAME connection-name

Sets the name of the current connection

/ 8 、 CLUSTER SLOTS

Get the mapping array of cluster nodes

/ 9 、 COMMAND

Get an array of Redis command details

/ 10 、 COMMAND COUNT

Get the total number of Redis commands

/ 11 、 COMMAND GETKEYS

Gets all the keys for a given command

/ 12 、 TIME

Returns the current server time

/ 13 、 COMMAND INFO command-name [command-name...]

Gets the array described by the specified Redis command

/ 14 、 CONFIG GET parameter

Gets the value of the specified configuration parameter

/ 15 、 CONFIG REWRITE

Rewrite the redis.conf configuration file specified when starting the Redis server

/ 16 、 CONFIG SET parameter value

Modify redis configuration parameters without reboot

/ 17 、 CONFIG RESETSTAT

Reset some statistics in the INFO command

/ 18 、 DBSIZE

Returns the number of key in the current database

/ 19 、 DEBUG OBJECT key

Get debugging information for key

/ 20 、 DEBUG SEGFAULT

Let the Redis service crash

/ 21 、 FLUSHALL

Delete all key for all databases

/ 22 、 FLUSHDB

Delete all key of the current database

/ 23 、 INFO [section]

Get all kinds of information and statistical values of Redis server

/ 24 、 LASTSAVE

Returns the last time Redis successfully saved data to disk, expressed in UNIX timestamp format

/ 25 、 MONITOR

Print out the commands received by the Redis server in real time for debugging

/ 26 、 ROLE

Returns the role to which the master-slave instance belongs

/ 27 、 SAVE

Save data to hard disk synchronously

/ 28 、 SHUTDOWN [NOSAVE] [SAVE]

Save data asynchronously to the hard disk and shut down the server

/ 29 、 SLAVEOF host port

Converts the current server to a slave server (slave server) of the specified server

/ 30 、 SLOWLOG subcommand [argument]

Manage slow logs for redis

/ 31 、 SYNC

Internal commands for the copy function (replication)

The difference between 3.4.5Redis and memchache

1. Both Redis and Memcache store data in memory, and both are in-memory databases. But memcache can also be used to cache other things, such as pictures, videos, etc.

2. Redis not only supports simple KBH data, but also provides storage of data structures such as list,set,hash.

3. Virtual memory-Redis when physical memory is used up, you can swap some value that has not been used for a long time to disk

4. Expiration policy-memcache is specified when set, for example, set key1 008, that is, never expires. Redis can be set by, for example, expire, such as expire name 10

5. Distributed-set memcache cluster and use magent to do one master and many slaves; redis can be one master and multiple slaves, that is, all can be one master and one slave

6. Storage data security-after memcache is hung up, the data is gone; redis can be saved to disk on a regular basis (persistence)

7. Disaster recovery-after the memcache is down, the data cannot be recovered; after the redis data is lost, it can be recovered through aof

8. Redis supports data backup, that is, data backup in master-slave mode.

9. Application scenarios are different: Redis can be used not only as NoSQL database, but also as message queue, data stack and data cache. Memcached is suitable for caching SQL statements, data sets, user temporary data, deferred query data, session and so on.

10. The best way to use Redis is to in-memory all data.

Redis is more often used as a replacement for Memcached.

Redis is more appropriate when you need more data type support than key/value.

It is more appropriate to use Redis when the stored data cannot be deleted.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report