Install
Releases · tporadowski/redis · GitHub
Directly choose the msi format, window-style installation, step by step.
There is an option during the installation process that asks you whether you need to configure the environment variables. Select this option. If not, you need to configure the environment variables yourself.
Check whether the installation and configuration are successful:
Enter in cmd
redis-cli
There are 15 databases by default, and the default is the 0th database. You can use select to switch databases.
introduce
Redis: It is a Key-Value type NoSQL database developed in C language. The entire database is loaded in the memory and operated, and the data in the database is flushed to the hard disk through asynchronous operations at regular intervals for storage.
NoSQL database: It is a general database other than relational databases such as Mysql.
All redis stores are strings
In Redis, data is stored in the form of strings, without distinguishing whether the data is numbers or strings. Redis is a schema-less database that treats all data as binary strings. This means that anything Redis stores is a string, and Redis itself does no checking of data types.
Since all the data stored are strings, why can some data still increase and decrease?
The reason why Redis can perform increment (INCR) and decrement (DECR) operations even though the data stored are all strings is because Redis does not actually treat all strings as ordinary strings. Redis internally determines whether a string can be interpreted as an integer based on its contents.
Specifically, when you perform increment and decrement operations, Redis will check whether the value of the key meets the following conditions:
- The value of key must be a valid integer string.
- The value of the key must be within the range of integers that can be represented by Redis.
If the key's value meets these two conditions, Redis will interpret it as an integer and perform an increment or decrement operation. Otherwise, Redis will return an error indicating that the key's value cannot be interpreted as an integer.
advantage
- Good performance, fast access
- Complete supported data types
- Support persistence and distribution
shortcoming
- Memory limitations: Since data is stored in memory, the amount of data in Redis is limited by physical memory. If the data set is too large, it may cause out of memory issues.
- Single-threaded model: Redis executes commands in a single thread, which means performance may be limited when handling a large number of client requests.
Applicable scene
- Cache: The most commonly used one is as a cache layer, which stores frequently accessed data in memory to reduce the burden on databases and servers and speed up access.
- Ranking list: popular posts are placed in the cache and accessed frequently
- Counter: This thing has a very high access frequency. Each user clicks and counts +1. It is better to store it in the memory.
- Like, follow, comment: these operations with high access frequency
- Message queue: You can build this lightweight message queue system for processing real-time events and task queues.
Why is Redis fast?
- In-memory storage: Redis stores data in memory instead of traditional disk storage. In-memory storage means data can be read directly from RAM without disk I/O operations, making read and write operations very fast.
- Single-threaded: Redis uses a single-threaded model to process command requests. It does not require locking and synchronization operations between multiple threads. Each request is processed sequentially, which reduces contention and context switches and improves performance.
- Data structure optimization: Redis supports a variety of high-performance data structures, such as hash tables and ordered sets, which are carefully designed and optimized to perform operations quickly in memory. These data structures make Redis ideal for a variety of use cases, such as counters, leaderboards, and message queues.
Data types, commands
Stored data types: key and value. The key value stored in the form of value is String type and the value value is in the following five forms.
There are 5 commonly used types , and there are other types, such as bitmap, super log and other types.
(Lists can be repeated, sets cannot be repeated)
- The string type is: key value: value1, which is equivalent to a string
- The hash type is: key value: key1:value1,key2:value2,key3:value3, which is equivalent to a collection of key-value pairs
- The list is: key value: value1, value2, value3, equivalent to a string list
- Sets and ordered sets are: key values: value1, value2, value3, equivalent to string collections
Common operations for all types
Start: redis-cli
Database conversion: select 11 (0-15, 16 databases in total)
Query all keys: keys *
Query all keys starting with test: keys test*
Check the type of a key: type key value
Clear the data in all libraries (use with caution: flushall
Delete a key: del key value
Determine whether the value exists: exists key value 1, key value 2...
Check the remaining time: ttl key value
Or set the expiration time directly: expire key value seconds
1.String string
set: Format: set key value value1 ex seconds, followed by the set expiration time
Get value: get key value
Get the length of the value: strlen key value
Value decrement: decr key value (must be a number)
Value auto-increment: incr key value (must be a number)
2.hash hash set
The key is not repeatable: the small key in each key-value pair of the hash set key value cannot be repeated, but the value can be repeated.
Storage: hset key value key value (small) value value (small)
Set multiple small keys and values at the same time: hmset key value key value (small) value value (small) key value (small) value value (small)....
Value: hget key value key value (small)
Example:
#存了一个key是user:1的哈希集合,集合里面存了一个键值对 name:Alice
hset user:1 name "Alice"
#取出name的值
hget user:1 name
Get all values: hgetall key value
Check if it exists: hexists key value key value (small)
Get all the keys of the collection: hkeys key value
Get all values of the collection: hvals key value
3.list list
Orderly and repeatable
Like a horizontal rectangle, data can be added and subtracted from the left and right sides.
Store data from the left lpush key value value1 value2 value3 value4 value5 ..
Deposit it in: value5 value4 value3 value2 value1
Store data starting from the right rpush key value value1 value2 value3 value4 value5 ..
Deposit it in: value1 value2 value3 value4 value5
Get and delete data from the left: lpop key value
Get and delete data from the right: rpop key value
Get the data of a certain subscript: lindex key value index value
View length: llen key value
View data with subscripts from 0-2: lrange key value 0 2
4.set collection
Similar to hashset, unordered and non-repeatable
Store data: sadd key value value1 value2 value3...
Delete data: srem key value value1
Randomly obtain and delete data: spop key value quantity (randomly removing data can be used for lottery draws)
Get all members: smembers key value
Get the number of members: scard key value
Union operation: suntion key value 1 key value 2
Intersection operation: sinter key value 1 key value 2
5.zset ordered collection
Orderly and non-repetitive
It is sorted by score.
Each element is associated with a score of type double. Redis uses scores to sort the members of the collection from small to large.
The members of zset (value) are unique, but the scores (score) can be repeated.
Add: zadd key score1 value1 [score2 value2 ...] (update score if exists)
Delete: zrem key value1 [value2 ...]
Get the score: zscore key value1
Get the number of elements: zscore key
Add points and subtract points: zincrby key add a few points (number) value1 (can be a negative number to reduce the score)
Get the ranking: zrank key value1 (the subscript starts from 0)
Get the reverse ranking zrevrank key value1
Range acquisition: zrange key starting subscript and ending subscript [withscores] (adding withscores will output the scores at the same time)
Unsorted range acquisition: zrevrange key starting subscript ending subscript [with scores]
If you think the article is good, please give it a like