About Redis
REDIS Storage Structure Type: String type, hash type, list type, collection type, ordered collection type.
Redis memory storage and persistence: The data in the Redis database is stored in memory and the memory reads and writes faster than the hard disk, so Redis has a distinct advantage over those databases that store data on the hard disk. Redis can read and write 100,000 key values within a second. Redis provides persistent support for asynchronously writing in-memory data to the hard disk so that the data is not lost and does not affect the continued service delivery of the business.
Redis Rich features: Although Redis is developed as a database, more and more people use it as a cache, queue system, etc. because of its rich functionality. Redis is a veritable generalist. Redis can set the lifetime (time to Live,ttl) for each key, and the key will be deleted automatically when the lifetime expires. This feature enables Redis to be used as a caching system with excellent performance, and because Redis supports persistence and rich data types, it makes it a strong contender for another very popular cache system memcached. Discussing the pros and cons of Redis and memcached has always been a hot topic. In performance, Redis is a single-threaded model, and memcached supports multithreading, so the latter performance is higher on multicore servers. However, as mentioned earlier, the performance of Redis is excellent enough that in most cases its performance will not be a bottleneck. So in the use of more should be concerned about the difference between the two, if you need to use advanced data types or persistent functions, Redis will be a good substitute for memcached. As a caching system, Redis can also limit the maximum amount of memory space used by data, and can automatically retire unwanted keys after the data has reached space limits. In addition to this, the Redis list type key can be used to implement queues and support blocking reads, which makes it easy to implement a high-performance priority queue. At the same time, Redis supports the "Publish/Subscribe" message pattern on a higher level, which can be built on the basis of this system such as chat room ①.
System environment:
[[Email protected] ~] # uname-a # 1 SMP Fri Jul 8 17:36:59 EDT x86_64 x86_64 x86_64 gnu/linux [[Email protected] ~] # Red Hat Enterprise Linux Server release 5.7 (Tikanga)
To install Redis:
TAR-XF redis-3.0. 0.tar.gz cd Redis-3.0. 0 make make install
To edit the Redis profile redis.conf, add the following:
CP redis.conf/etc/redis.conf
" /var/log/redis.log " 6380 # The default Redis port is 6379pidfile/var/run/redis.pid
To start Redis:
Redis-server/etc/redis.conf &
To view a process:
[[Email protected] ~] # ps-ef|grep redisroot 6635 3519 0 11:42 pts/0 00:00:00 redis-server *:6380 Root 6641 6537 0 11:43 pts/1 00:00:00 redis-cli-p 6380root 6677 3519 0 12:23 pts/0 00:00:00 grep redis
To connect Redis using the REDIS-CLI client:
[[Email protected] ~] # redis-cli-p 6380127.0.0.1:6380> echo hi"hi"127.0.0.1:6380> Set Foo 1OK
To turn off Redis:
Redis-cli-p 6380 shutdown
Reply type
1. Status reply
State reply (status reply) is the simplest kind of reply, such as when a set command is sent to Redis to set a value for a key, Redis responds with a status OK indicating that the setting was successful. In addition, the reply to the ping command previously demonstrated Pong is also a status reply. Status reply displays status information directly, for example:
Redis>ping
PONG
2. Error reply
Redis returns an error reply (Error reply) When a command does not exist or if there is an error in the format of the command. The error reply starts with (error) and follows the error message. If you execute a non-existent command:
Redis>errorcommend
(Error) ERR unknown command ' errorcommend '
3. Integer reply
Although Redis does not have an integer type, it provides commands for integer operations, such as the INCR command, which increments the key value, to return an incremented key value as an integer. In addition, some other commands return integers, such as the Dbsize command, which can get the number of keys in the current database. Integer reply begins with (integer) and follows the integer data:
REDIS>INCR Foo
(integer) 1
4. A string reply string reply (bulk Reply) is the most common type of reply that is given a string reply when a key value of a string type key or an element in another type key is requested. String replies are wrapped in double quotation marks:
Redis>get Foo
"1"
The special case is that when the requested key value does not exist, an empty result will be displayed as (nil). Such as:
Redis>get noexists
(nil)
5. Multi-line String reply
Multi-line string replies (Multi-bulk Reply) are also common, such as when you request a list of elements of a non-string type key, you receive a multiple-line string reply. Each line string in a multiline string reply begins with an ordinal, such as:
Redis> KEYS *
1) "Bar"
2) "Foo"
Command
1, assignment and value
Set key value
Get key
127.0.0.1:6380> set foo 1OK127.0.0.1:6380> get foo"1" 127.0.0.1:6380> get Name (nil)
Returns a null value when the key does not exist.
2. Increment the number
INCR Key
127.0.0.1:6380>1127.0.0.1:6380>2127.0.0.1:6380> get num"2 "127.0.0.1:6380> set foo Cheeronok127.0.0.1:6380>is notor out of Range
The default key value is 0 when the key of the operation does not exist, so the first increment is 1, and Redis prompts an error when the key value is not an integer.
3, add the specified integer
Incrby Key Increment
Decreases the specified integer
DECR key
Decrby Key Decrement
127.0.0.1:6380> get num"2"127.0.0.1:6380> incrby num 3 5127.0.0.1:6380> get num"5"127.0.0.1:6380>4127.0.0.1:6380 > Decrby num 22127.0.0.1:6380> get num"2"
4. Increase the specified floating point number
Incrbyfloat Key Increment
127.0.0.1:6380> get num"2"127.0.0.1:6380> incrbyfloat num 2.4" 4.4 "127.0.0.1:6380> get num"4.4"
5. Append value to Tail
APPEND Key value
Get string length
STRLEN Key
127.0.0.1:6380> Set key Hellook127.0.0.1:6380>10127.0.0.1:6380> get key" HelloWorld"127.0.0.1:6380>10127.0.0.1:6380> Set Key I'm bronze OK 127.0.0.1:6380>12
When the key value does not exist, the return length of 0,redis received is UTF-8 encoded in Chinese, UTF-8 the length of the encoded Chinese is 3
6. Get/Set multiple values at the same time
MSET key Value Key value ...
MGET Key1 Key2 Key3
127.0.0.1:6380> mset num1 1 num2 2 num3 3OK127.0.0.1:6380> mget num1 num2 num3"1"" "2""3"
Redis Installation and Learning