In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the basic knowledge points of Redis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is Redis?
Let's first take a look at the introduction given on Redis's official website:
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
In short, Redis is an open source log database written in ANSI C language, complies with BSD protocol, supports network, can be memory-based and persistent, and provides a variety of data types.
2. Father of Redis
The father of Redis is Salvatore Sanfilippo, a programmer from Sicily, Italy, who is more accustomed to calling him Antirez. If you are interested in him, you can visit his blog or follow his github.
3. What are the advantages of Redis
Speed: Redis uses memory to store datasets and supports Pipelining commands, which can send more than one command at a time.
Persistence: data in memory can be saved on disk and can be reloaded and used when restarted.
Atomicity: all operations are atomic and support transactions.
Rich data structure: support strings, lists, hashes, collections and ordered collections to meet most usage requirements.
Support for multiple languages: Redis supports many languages, such as C, C++, C #, Go, Java, JavaScript, PHP, and so on.
A variety of features: Redis also supports publish/subscribe, notification, key expiration and other features.
4. What can Redis do
Because of the high speed of exchanging data, Redis is often used to store some data that needs to be fetched frequently in the server. Compared with directly reading the disk to obtain data, using Redis can save a lot of time and improve efficiency. For example:
1 million people visit the recommended video column on the home page of a video website every day. If they are all read from database queries, there will be at least 1 million more database query requests every day. If Redis is used, the frequently called data is stored in memory, saving 0.1 second each time, 100000 seconds for 1 million times, which greatly improves the speed and overhead.
In short, Redis has a wide range of application scenarios and is of great value, so start to learn the basics now.
5. Install Redis
If you want to do a good job, you must first sharpen its tools. The first step in learning Redis must be to install Redis. Since my environment is a Windows system, I will only demonstrate the installation of Redis under Windows. If you don't want to install Redis, but want to try it, you can go to the online testing site provided by the official website, which also includes simple introductions and tutorials.
Install Redis under Windows here we download the Redis-x64-3.2.100.zip package file (if you have an updated version, you can download the recently updated stable version).
Download
After downloading, extract it to your own folder. For example, I unzipped to D:\ redis.
Unzip
Open a cmd window, use the cd command to change the directory to the unzipped folder path (for example, I change the directory to D:\ redis), and then run the command:
Redis-server.exe redis.windows.conf .
After typing, the following interface is displayed:
Redis-server
So we open a redis server, and we can see some information from the figure, such as the port number is 6379. If you want to use the redis server, we need to open another cmd window, do not close the original, otherwise you will not be able to access the server. Also change the path to the redis directory, and then run:
Redis-cli.exe-h 127.0.0.1-p 6379
Store a key-value pair:
Set firstkey "hello redis"
Take out the key-value pair:
Get firstkey
Close the connection:
Quit
First_use
In this way, we have completed the installation of Redis under Windows and experienced Redis's key-value storage for the first time.
6. Redis data structure
Redis supports five data structures: String (string), Hash (hash), List (list), Set (collection), and SortedSet (ordered collection). Let's take a brief look at each data structure and their basic commands.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
StringString is the most basic data structure of Redis, and it is also a necessary data type for any storage system. String types are binary-safe. To put it simply, you can basically store everything in strings. You can store the contents of image files or serialized objects as strings. The maximum value of the String type can be stored in 512MB, which is indeed enough for almost everything.
> set mykey "hello world" OK > gey mykey "hello world" > getrange mykey 6 10 "world" > getrange mykey 0 20 "hello world" > getrange mykey 0-1 "hello world"
Tips: from the above examples, we can easily see that the string starts at 0; if end is greater than the string length, the complete string is returned; and when end is-1, end is the last character of the string.
> getset database "mysql" (nil) > get database "mysql" > getset database "redis"mysql" > get database "redis"
Tips: returns (nil) when the key is not set.
> strlen mykey (integer) 11 > append mykey ", hello redis" 24 > get mykey "hello world, hello redis" > set incr_num 10 OK > get incr_num "10" > incr incr_num (integer) 11 > incrby incr_num 4 (intrger) 15 > incrbyfloat incr_num 0.515.5
Tips: the integer value will be displayed as integer, but will not be prompted as float when it becomes floating-point.
> set decr_num 10 OK > get decr_num "10" > decr decr_num (integer) 9 > decrby decr_num 4 (integer) 5
Tips:redis does not have a command to reduce a given floating-point value by a numeric value. If we want to reduce decr_num by 2.5, we can use the incrbyfloat command to execute incrbyfloat decr_num-2.5.
> incrbyfloat decr_num-2.5 2.5
Decrby key decrement: integer value minus a given integer value (decrement)
Decr key: integer value-1
Incrbyfloat key increment: numeric value increases a given floating point value (increment)
Incrby key increment: an integer value increases a given integer value (increment)
Ncr key: integer value + 1
Append key value: if it can already exist and is a string, the specified value is added to the end of the original value, and the string length after the operation is returned.
Strlen key: returns the length of the string value stored by the key
Getset key value: sets the new value of the specified key and returns the old value
Getrange key start end: returns a substring of a string in key
Get key: gets the value of the specified key
Set key value: sets the value of the specified key
2. HashHash stores the mapping relationship between field and value, which is suitable for users to store objects. For example, to store a user's name, age, address, and so on, you can use Hash. Each Hash can store 232 >-1 field-value pair (4294967295).
> hset myhash name "test" (integer) 1 > hget myhash name "NPC" > hset myhash name "NPC" (integer) 0
Tips: using the hset command, 1 is returned after creating a new field and setting the value successfully, and 0 is returned if the value of an existing field is modified.
> hmset myhash age "20" country "China" OK > hexists myhash name (integer) 1 > hexists myhash phone (integer) 0
Tips: hash table key contains the field field returns 1, does not contain or the corresponding key does not exist and returns 0.
> hmget myhash name age phone 1) "NPC" 2) "20" 3) (nil) > hgetall myhash 1) "name" 2) "NPC" 3) "age" 4) "20" 5) "country" 6) "China" > hkeys myhash 1) "name" 2) "age" 3) "country" > hvals myhash 1) "NPC" 2) "20" 3) "China" > hlen myhash 3 > hdel myhash age (integer) 1
Hdel key field1: delete a field from the hash table key
Hlen key: gets the number of fields in the hash table key
Hvals key: get all the value in the hash table key
Hkeys key: get all the field in the hash table key
Hgetall key: gets all the field-value pairs in the hash table key
Hmget key field1 [field2]: gets the value of all given field in the hash table key
Hexists key field: check whether field exists in the hash table key
Hmset key field1 value1 [field2 value2]: sets multiple field-value pairs in hash table key at the same time.
Hset key field value: sets the value of field in key in the hash table to value
Hget key field: gets the value corresponding to field in the hash table key
3. The List type of ListRedis is a simple string list, which is equivalent to a linked list in the underlying implementation. We can add values to the head (left) or tail (right) of the list. The list can store up to 232 >-1 elements (4294967295 billion).
> lpush mylist "a"b" (integer) 2 > rpush mylist "c"d" (integer) 4
Tips: the length of the list returned after the lpush and rpush commands are executed.
> llen mylist (integer) 4 > lrange mylist 0-11) "b" 2) "a" 3) "c" 4) "d" > lrange mylist 1-21) "a" 2) "c"
Tips: from the above example, we can easily see that the start and end parameters in the lrange command are index values, where 0 represents the first element and-1 represents the last element.
> lindex mylist 0 "b" > lpop mylist "b" > rpop mylist "d" > rpush rem "hello"hello"redis"hello" (integer) 4 > lrange rem 0-11) "hello" 2) "hello" 3) "redis" 4) "hello" > lrem rem-2 "hello" (integer) 2 > lrange rem 0-11) "hello" 2) "redis"
Count > 0: search the list from left to right and remove the elements equivalent to value. The number is count. Count-1 (4294967295).
> sadd myset1 "hello"redis" (integer) 2 > sadd myset1 "hello" (integer) 0
Tips: returns 0 when a duplicate member is added to the collection
> scard myset1 2 > smembers myset1 1) "hello" 2) "redis" > sadd myset2 "hello"world" (integer) 2 > sdiff myset1 myset2 1) "redis" > sdiff myset2 myset1 1) "world" > sinter myset1 myset2 1) "hello" > sunion myset1 myset2 1) "hello" 2) "redis" 3) "world" > sadd myset1 "NPC" (integer) 1 > spop myset1 "redis" > smembers myset1 1) "NPC" 2) "hello"
Spop key: removes and returns a random element in the collection
Sunion key1 [key2]: returns the union of all given collections
Sinter key1 [key2]: returns the intersection of all given sets
Sdiff key1 [key2]: returns the difference of all given sets
Smembers key: returns all members of the collection
Scard key: gets the number of collection members
Sadd key member1 [member2]: add one or more members to the collection
5. SortedSet in addition to unordered sets (Set), Redis also provides ordered sets (SortedSet). Ordered sets do not allow duplicate members, and each different member is associated with a score of double type. Redis sorts members from small to large through these scores. Ordered collections are sometimes called ZSet because their commands begin with the letter Z.
> zadd myzset 10 "one" 20 "two" 30 "three" (integer) 3 > zcard myzset 3 > zscore myzset "one" 10.0 > zrange myzset 0-11) "one" 2) "two" 3) "three" > zrange myzset 0-1 withscores 1) "one" 2) 10.03) "two" 4) 20.05) "three" 6) 30.0 > zrevrange Myzset 0-1 withscores 1) "three" 2) 30.03) "two" 4) 20.05) "one" 6) 10.0 > zrank myzset "one" 0 > zrank myzset "three" 2 > zrevrank myzset "one" 2 > zrevrank myzset "three" 0 > zcount myzset 15402 > zrange myzset 0-1 withscores 1) "one" 2) 10.03) "two" 4) 20.05) "three" 6) 30.0 > zincrby myzset 40 "one" 50.0 > zrange myzset 0-1 withscores 1) "two" 2) 20.03) "three" 4) 30.05) "one" 6) 50.0
Zincrby key increment member: increases the score of a specified member by increment
Zcount key min max: returns the number of members with scores between min and max
Zrank key member: returns the ranking of specified members, sorted from small to large
Zrevrank key member: returns the ranking of specified members, from largest to smallest
Zrange key start end [withscores]: returns members from small to large by indexing start and end
Zrevrange key start end [withscores]: returns members from large to small by indexing start and end
Zscore key member: returns the score of the specified member
Zcard key: gets the number of members of an ordered collection
Zadd key score1 member1 [score2 member2]: add one or more members to an ordered collection, or update existing member scores
This is the end of the content of "what are the basic knowledge points of Redis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.