Redis is an open source, advanced key-value store and a suitable solution for building high-performance, extensible Web applications.
Redis has three main features that make it superior to other key-value data storage systems-Redis keeps its databases fully in memory and only uses disk for persistence. Redis has a relatively rich set of data types compared to other key-value data stores. Redis can replicate data to any number of slave machines.
The Redis official web site is: http://www.redis.io/, as follows:
Benefits of Redis
Here are some of the benefits of Redis. Exceptionally fast-Redis is very fast and can perform set operations approximately 110,000 times per second, approximately 81,000 reads/get (GET) operations per second. Supports rich data types-Redis supports most data types commonly used by developers, such as lists, collections, sort sets, hashes, and so on. This makes it easy for redis to solve a variety of problems because we know which problems can be better used in which data types to handle the resolution. Operations are atomic-all redis operations are atomic, ensuring that if two clients are concurrently accessed, the Redis server can receive the updated values. Multi-utility-Redis is a multi-utility that can be used in a variety of use cases, such as caching, Message Queuing (Redis local support publish/subscribe), any short-term data in an application, such as a session in a Web application, a page hit count, and so on. Redis and other key-value storage systems
Redis is a different evolutionary route for a key-valued database system, and its values can contain more complex data types that define atomic operations on those data types.
Redis is a memory database, but it is persistent on the disk database, so it represents a different tradeoff in which very high write and read speeds are achieved under the constraints of datasets that cannot be larger than memory (memory).
Another advantage of the in-memory database is that it is easier to manipulate complex data structures in memory than the same data structures on disk. As a result, Redis can do very little internal complexity. 1. Redis Environment Installation Configuration
In this chapter, you will learn about and learn about Redis's environment installation settings. installing Redis on Ubuntu
To install Redis on Ubuntu, open the terminal and type the following command-
[yiibai@ubuntu:~]$ sudo apt-get update
[yiibai@ubuntu:~]$ sudo apt-get install redis-server
Shell
This will install Redis on the Ubuntu machine.
Start Redis
[yiibai@ubuntu:~]$ Redis-server [2988], Feb 17:09:42.485 # warning:no config file specified, using the default Config. In order to specify a config file with redis-server/path/to/redis.conf [2988], 17:09:42.488 # Unable to set the max
Number of files limit to 10032 (operation not permitted), setting the max clients configuration to 3984. [2988] Feb 17:09:42.490 # warning:32 bit instance detected but no memory lim _._ _.-' __ ' -._
_.-`` `. `_. "-._ Redis 2.8.4 (00000000/0).-".-". ' \ \ _.,_ '-._ (',.-' | ',) Running in stand alone mode | '-._ '-...-' __...-. '-._| ' ` _.-'| port:6379 | `-._ `._ / _.-' |
pid:2988 '-._ '-._ '-./_.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' | | `-._`-._ _.-'_.-' |
Http://redis.io '-._ '-._ '-.__.-' _.-' _.-' | '-._ '-._ '-.__.-' _.-' _.-' | |
`-._`-._ _.-'_.-' | `-._ '-._ '-.__.-' _.-' _.-, '-._ '-.__.-' _.-'-._ _.-'-.__.-' [2988] 17:09:42.581 # Server started, Redis version 2.8.4 [2988], Feb 17:09:42.582 # WARNING Overcommit_memory is set to 0! Background s ' to/etc/sysctl.conf And then reboot or run the command ' Sysctl vm.overcommit_m [2988], 17:09:42.582 * The server is now ready to accept
Connections on PO Shell
Check if Redis is working
[yiibai@ubuntu:~]$ redis-cli
Shell
This will open a redis hint, as shown below-
Redis 127.0.0.1:6379>
Shell
In the above hint, 127.0.0.1 is the IP address of the computer and 6379 is the port running the Redis server. Now type the following ping command.
Redis 127.0.0.1:6379> Ping
PONG
Shell
This indicates that Redis has successfully installed on your computer.
Installing Redis Desktop Management on Ubuntu
To install Redis Desktop Manager on Ubuntu, you can download the software package from Http://redisdesktop.com/download and install it.
Open the downloaded package and install it.
Redis Desktop Manager provides the UI for managing the keys and data for Redis. 2. Redis Configuration
In Redis, there is a configuration file (redis.conf) in the root directory of the Redis. Of course you can get and set all Redis configurations through the Redis Config command.
Grammar
The following is the basic syntax for the Config command in Redis.
Redis 127.0.0.1:6379> CONFIG GET config_setting_name
Shell
Example
Redis 127.0.0.1:6379> CONFIG GET loglevel
1) "LogLevel"
2) "Notice"
Shell
To get all configuration settings, use * instead of config_setting_name
Example
Redis 127.0.0.1:6379> CONFIG GET * 1) "Dbfilename" 2) "Dump.rdb" 3) "Requirepass" 4) "5)" Masterauth "6) "" 7) "Unixsocket" 8) "9" logfile "/var/log/redis/redis-server.log") "Pidfile"/var/run/redis/red Is-server.pid "MaxMemory") "3221225472" ("Maxmemory-samples") "3") "Timeout" (0) "Tcp-keep Alive "(0)") "(Auto-aof-rewrite-percentage") "(") "(auto-aof-rewrite-min-size)") "67108864" hash- Max-ziplist-entries "+") "Hash-max-ziplist-value" () "()" () "List-max-ziplist-entries") "L Ist-max-ziplist-value "(+)") "Set-max-intset-entries") "Zset-max-ziplist-entries" (36) "128" 37) "Zset-max-ziplist-value") "(") "(" Lua-time-limit ")" Slowlog-log-slower-than "" 10000 "slo "Wlog-max-len") "(") "(") "(" Port ")" 6379 "(" Databases ")" 51 ")" Repl-ping-slave-period "50)" (10 ")"
Repl-timeout "52)" 60 " "Repl-backlog-size" "1048576") "Repl-backlog-ttl" ("3600") "MaxClients" ("3984") watchdog-p
Eriod "0") "slave-priority" ("66") "Min-slaves-to-write" ("0") "10" ("Min-slaves-max-lag") ("Hz") "(No-appendfsync-on-rewrite)" () "(") "()" () "()" () "()" () "()" () ")" Slave-serve-stale-data "()" Yes "slave-read-on
Ly "") "Yes" ("Stop-writes-on-bgsave-error") "Yes" ("daemonize") "Yes" "rdbcompression" "Yes" Bayi) "Rdbchecksum" ("the") "Yes" ("activerehashing") "Yes") "Repl-disable-tcp-nodelay" ("No") "Aof-rewrit E-incremental-fsync "(") "Yes" ("appendonly") "No") "dir" ("/var/lib/redis") "Maxmemory-policy" 94) "Noeviction" ("Appendfsync") "Everysec") "Save" 98) "1" (10000 ")" LogLevel "101" notice " "Client-output-buffer-limit" 102) "normal 0 0 0 slave 268435456 67108864 pubsub 33554432 8388608" 103 "Unixsocket Perm "104)" 0 "(") "SLAVeof "106)" "107)" Notify-keyspace-events "108" "109)" bind "" 127.0.0.1 "Shell
Edit Configuration
To update the configuration, you can edit the redis.conf file directly, or you can update the configuration with the Config set command.
Grammar
The following is the basic syntax for the Config set command.
Redis 127.0.0.1:6379> CONFIG SET config_setting_name new_config_value
Shell
Example
Redis 127.0.0.1:6379> config SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel
1) " LogLevel "
2)" Notice "
Shell
3. Redis Data Type
Redis supports 5 types of data. string
A string in Redis is a sequence of bytes. The strings in Redis are binary safe, which means that their lengths are not determined by any special terminating characters. Therefore, you can store any content up to 512 megabytes in a string.
Example
Redis 127.0.0.1:6379> Set name "yiibai.com"
OK
redis 127.0.0.1:6379> get name
"yiibai.com"
Shell
In the example above, set and get are Redis commands, name is the key used in Redis, and yiibai.com is the value of the string stored in Redis.
Note-Redis commands are case insensitive, such as Set,set and set are the same commands. The maximum length of a string value is 512MB. Hash /Hash
A Redis hash/hash (hashes) is a collection of key-value pairs. A Redis hash/hash is a mapping between a string field and a string value. Therefore, they are used to represent objects.
Example
Redis 127.0.0.1:6379> hmset ukey username "yiibai" password "passswd123" points
Shell
In the above example, the hash/hash data type is used to store the user object that contains the user's basic information. Here Hmset,hgetall is the Redis command, and Ukey is the name of the key.
Each hash/hash can store up to 2^32-1 (more than 4 billion) of a health-value pair. List
The Redis list is just a list of strings, sorted by insert order. You can add elements to the head or tail of a redis list.
Example
Redis 127.0.0.1:6379> Lpush alist redis
(integer) 1
redis 127.0.0.1:6379> lpush alist MongoDB
( Integer) 2
redis 127.0.0.1:6379> lpush alist sqlite
(integer) 3
redis 127.0.0.1:6379> lrange alist 0 10
1) "SQLite"
2) "MongoDB"
3) "Redis"
Shell
The maximum length of a list is 2^32-1 elements (4294967295, each list can hold more than 4 billion elements). Collection
A Redis collection is an unordered collection of strings. In Redis, you can add, remove, and test the time that a member exists O (1) complexity.
Example
Redis 127.0.0.1:6379> sadd yiibailist redis
(integer) 1
redis 127.0.0.1:6379> sadd yiibailist MongoDB
(integer) 1
redis 127.0.0.1:6379> sadd yiibailist sqlite
(integer) 1
redis 127.0.0.1:6379> sadd Yiibailist sqlite
(integer) 0
redis 127.0.0.1:6379> smembers yiibailist
1) "SQLite"
2) "MongoDB"
3) "Redis"
Shell
Note-In the above example, SQLite was added two times, but because of the unique properties of the collection, it is added only once.
The maximum number of members in a collection is 2^32-1 (that is, 4294967295, the number of elements in each collection can be up to 4 billion). Sortable Collection
Redis sortable collections are similar to Redis collections and are non-repeating character sets. The difference is that each member of the sorted collection is associated with a fraction, which is used to sort the sorted collection by the minimum score to the maximum score. Although members are unique, the values can be duplicated.
Example
Redis 127.0.0.1:6379> zadd yiibaiset 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd yiibaiset 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd yiibaiset 1 sqlite
(integer) 1
redis 127.0.0.1:6379> zadd Yiibaiset 1 sqlite
(integer) 0
redis 127.0.0.1:6379> zrangebyscore yiibaiset 0
1) "MongoDB"
2) " Redis "
3" "SQLite"
Shell
Because the sort value of ' SQLite ' is 1, the other two elements have a sort value of 0, so ' SQLite ' is ranked in the last position.