Redis-cluster cluster "First article": Redis installation and Redis data types

Source: Internet
Author: User
Tags install redis


Introduction to Redis: First, IntroductionRedis is an open source, Key-value database that is written in C and that supports network interaction and can be persisted based on memory. Redis source code is very simple, as long as there is time to see rectification C language, to see the source of Redis can read 50-60%. The current largest cluster of Redis should be Sina. Redis is currently supported by Vmvaer, and many of the open source software needs to be supported by some organizations. If an open source software doesn't have the money to support it, it's hard to go long. second, Redis and memcache contrast




Persistence: E-commerce example, session with Memcache to do, shopping cart with Redis to do, when you exit will be prompted in the shopping cart items will be after you exit to continue to save. Relatively memcache storage is more single! Master-slave replication:Redis's master-slave replication is similar to MySQL's master-slave replication but the principle is different! virtual Memory: plainly is to put some of the memory of some unused things on the hard disk, it is best not to use, reduce efficiency, now memory is relatively inexpensive. Redis Installation & Basic operations:


One, Redis installation

1.Check the configuration environment
Check if gcc is installed, if not installed: yum -y install gcc

2.Download and install Redis
cd / opt /
wget http://download.redis.io/releases/redis-3.0.4.tar.gz
#Download here to log in to the official website to view the latest Redis
tar -xvf redis-3.0.4.tar.gz
make
make install
cd /opt/redis-3.0.4/src/
make test


Problems you may encounter during installation:
zmalloc.h: 50: 31: error: jemalloc / jemalloc.h: No such file or directory
zmalloc.h: 55: 2: error: #error "Newer version of jemalloc required"

Allocator
-------------------------------------------------- -------------------------------------------------- -----
Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.
To force compiling against libc malloc, use:
% make MALLOC = libc
To compile against jemalloc on Mac OS X systems, use:
% make MALLOC = jemalloc

allocator (allocation operator), if there is an environment variable called MALLOC, this environment variable will be used to create Redis.
And libc is not the default allocator, the default is jemalloc!
Because jemalloc is proven to have fewer fragmentation problems than libc.

But if you don't have jemalloc and only libc, of course make fails. So add such a parameter.
make MALLOC = libc
-------------------------------------------------- -------------------------------------------------- -----

3.Configure redis
cp /opt/redis-3.0.4/utils/redis_init_script /etc/init.d/redis #Copy management script
chmod + x /etc/init.d/redis
mkdir / etc / redis
cp /opt/redis-3.0.4/redis.conf /etc/redis/6379.conf

4.Modify the redis startup mode
By default, Redis is started in the foreground when it starts. Change it to start in the background.
vim /etc/redis/6379.conf
daemonize no changed to daemonize yes

5.Redis joins the system service and is set to start at startup
First modify the Redis startup script:
vim /etc/init.d/redis
#chkconfig: 35 95 95 Just add in the third line

Add system service: chkconfig --add redis
Set boot on startup: chkconfig redis on
Check service status: chkconfig --list redis

6. Specify the log storage location & PID file & database file storage location (write persistence on the next side)
vim /etc/redis/6379.conf

logfile "/var/log/redis.log" specifies the log file to be output to the console if not specified
pidfile /var/run/redis_6379.pid
dir ./ This means that the default persistent configuration file is placed there! Suggested amendments!

pidfile will report an error if you do not modify the default:
The reason is that the default PID specified in /etc/init.d/redis is: PIDFILE = / var / run / redis _ $ {REDISPORT} .pid
But the default configuration file: /etc/redis/6379.conf (the default we copied from the decompression package is: pidfile /var/run/redis.pid)
 

 Second, the basic operation of Redis

SET Set Key GET Determine the value of Key EXISTS Determine whether Key exists KEYS * Display all Keys DEL Delete specified Key TYPE Get Key Type
Note: Redis is not case sensitive. It is best to use uppercase for the command to distinguish between commands and parameters!

 

1. Example of set:
192.168.0.201:6379> SET hello hehe
OK
192.168.0.201:6379> GET hello
"hehe"

2.Set multiple key values and use keys * to see all
192.168.0.201:6379> SET hello1 hehe1
OK
192.168.0.201:6379> SET hello2 hehe2
OK

192.168.0.201:6379> KEYS *
1) "hello1"
2) "hello"
3) "hello2"

KEY matching method:
? Match single
 * Match all

3. Determine if the key exists
Determine if Key exists Use: EXISTS What he returns is shaping: 0 does not exist, 1 exists
192.168.0.201:6379> EXISTS hello
(integer) 1
192.168.0.201:6379> EXISTS hehe
(integer) 0

4, delete KEY
192.168.0.201:6379> DEL hello
(integer) 1 #where 1 is the number
Delete multiple tests:
192.168.0.201:6379> DEL hello1 hello2
(integer) 2

5, view type TYPE
Just use the set type as a string. View type command with TYPE
192.168.0.201:6379> TYPE hello
string

6.Keyspace
Redis supports a maximum of 16 instances by default. You can modify the configuration file to support more!
Check with the INFO command!
# Keyspace
db0: keys = 1, expires = 0, avg_ttl = 0

db0: This can be understood as a namespace. Supports up to 16, use SELECT to switch
192.168.0.201:6379> SELECT 1
OK
Try adding a key-value
SET db1 hehe
Then look at using INFO
# Keyspace
db0: keys = 1, expires = 0, avg_ttl = 0
db1: keys = 1, expires = 0, avg_ttl = 0
 Redis data type:
He uses different commands to distinguish what data type you want to manipulate
Types cannot be nested or mixed! But there is a king of fried: set can change all types to string types!

1. String type:

SET
GET
DEL
APPEND appends after the value
set can be reset, but APPEND is best to add
192.168.0.201:6379> SET hehe hello
OK
192.168.0.201:6379> GET hehe
"hello"
192.168.0.201:6379> APPEND hehe, world
(integer) 11
192.168.0.201:6379> GET hehe
"hello, world"

Multiple values and query values can be set simultaneously with MSET and MSET
192.168.0.201:6379> MSET key1 v1 key2 v2 key3 v3
OK
192.168.0.201:6379> MGET key1 key2 key3
1) "v1"
2) "v2"
3) "v3"

Get string length
192.168.0.201:6379> STRLEN hehe
(integer) 11
If the string is Chinese, he will output 1 character and 3 strings according to UTF-8 format.)
192.168.0.201:6379> SET key "haha"
OK
192.168.0.201:6379> GET key
"\ xe5 \ x91 \ xb5 \ xe5 \ x91 \ xb5"
 2.Increment type

For example, if the voting point is +1, it would be unrealistic to use set to modify the set every time. All redis has an auto-increment type: INCR
192.168.0.201:6379> INCR num # By default, if there is no such value, INCR will automatically create a value default to zero. When you do not execute it once, it will +1
(integer) 1
192.168.0.201:6379> INCR num
(integer) 2
192.168.0.201:6379> INCR num
(integer) 3
192.168.0.201:6379> INCR num
(integer) 4


If you want to add more: INCRBY
192.168.0.201:6379> INCRBY num 10
(integer) 57
192.168.0.201:6379> INCRBY num 10
(integer) 67
192.168.0.201:6379> INCRBY num 10
(integer) 77


What about minus? DECR
192.168.0.201:6379> DECR num
(integer) 106
192.168.0.201:6379> DECR num
(integer) 105
192.168.0.201:6379> DECR num
(integer) 104

If you want to subtract more: DECRBY
192.168.0.201:6379> DECRBY num 5
(integer) 97
192.168.0.201:6379> DECRBY num 5
(integer) 92
192.168.0.201:6379> DECRBY num 5
(integer) 87

Want to support decimal points:
INCRBYFLOAT key 0.1
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.1"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.2"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.3"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.4"
 3. Hash type

Similar to the data inventory table, the table does not have fields, you can set a value for each field
HSET Key field value
HGET Key field
HMSET Key field value [field value ....]
HMGET Key field [field ...]
HGETALL Key
HDEL

192.168.0.201:6379> HSET shouji name iphone
(integer) 1
192.168.0.201:6379> HSET shoujico red
(integer) 1
192.168.0.201:6379> HSET shouji price 8888
(integer) 1

192.168.0.201:6379> HGET shouji name
"iphone"
192.168.0.201:6379> HGET shouji co
"red"
192.168.0.201:6379> HGET shouji price
"8888"
192.168.0.201:6379> HGETALL shouji
1) "name"
2) "iphone"
3) "co"
4) "red"
5) "price"
6) "8888"

In fact, it is not good looking now, but he calls it on the webpage through some APIs, and the value retrieved by typography is just great.
192.168.0.201:6379> HMSET diannao name thinkpad co black price 30
OK
192.168.0.201:6379> HMGET diannao name co price
1) "thinkpad"
2) "black"
3) "30"
 List types

List type: He stores an ordered list of strings. When did this "ordered" come in!


The time complexity of the list you add to the left and the right to add him is the same! O1 (time complexity)
Can be understood as: my speed does not increase with the increase in number! For example, his time overhead is the same for 1000 lines and 10,000 lines! Learned in university data structures

time complexity:
The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. The purpose of algorithm analysis is to select appropriate algorithms and improve them.
In computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of the algorithm.

But he has a disadvantage, for example, there are 10,000 keys in it, and you want to find the 98th one.
                Advantages, you can read the first 100 cards directly and read the head directly.


command:
LPUSH key value [value ...]
RPUSH key value [value ...]
               LPOP key
               RPOP key
     LRANGE key start stop
     LREM key count value
4.1,
Add key from left
192.168.0.201:6379> LPUSH num 0
(integer) 1
192.168.0.201:6379> LPUSH num 1
(integer) 2
192.168.0.201:6379> LPUSH num 2
(integer) 3

Is now added from the left
2 1 0

Add from the right
192.168.0.201:6379> RPUSH num 3
(integer) 4
2 1 0 3

192.168.0.201:6379> RPUSH num 5
(integer) 5

2 1 0 3 5 5


Use LNE if you want to get the length! Get list type length is: LLEN
192.168.0.201:6379> LLEN num
(integer) 5

4.2 Take the key from the left
Take this key from the list type (there is no more), take the first from the left
192.168.0.201:6379> LPOP num
"2"

The first one on the left is 2, so after taking it out, the key becomes
1 0 3 5

Take the key from the right and the first from the right (the 5 is taken out)
192.168.0.201:6379> RPOP num
"5"

Now looking at the length of this key
192.168.0.201:6379> LLEN num
(integer) 3

4.3,
Get a range of the list:
Now this value
1 0 3

192.168.0.201:6379> LRANGE num 0 1 # Take the value of 0-1
1) "1"
2) "0"

### This list is similar to the list in python, 0 -1 is equivalent to the index in the list.

192.168.0.201:6379> LPUSH num 2
(integer) 4
192.168.0.201:6379> RPUSH num 4
(integer) 5


192.168.0.201:6379> LRANGE num 0 -1 # (-1) means the first one on the left
1) "2"
twenty one"
3) "0"
4) "3"
5) "4"


4.4. Get the value of the specified element

Get the first value on the right:
192.168.0.201:6379> LINDEX num -1
"4"
Get the second value on the left:
192.168.0.201:6379> LINDEX num -2
"3"

What about -3?
192.168.0.201:6379> LINDEX num -3
"0"
This is the third value from the right! !! !! !! !!

Get value from left
192.168.0.201:6379> LINDEX num 0
"2"
192.168.0.201:6379> LINDEX num 1
"1"

He was a bit messy for the first time on both sides! He needs attention on both sides! !! !!

4.5, retain only the specified data

Keep only 0 to 2 data
192.168.0.201:6379> LTRIM num 0 2
OK
Look at the results:
192.168.0.201:6379> LRANGE num 0 -1
1) "2"
twenty one"
3) "0"

What's the use of this:
When writing logs, I only keep the last 100 logs in this buffer!
such as:
192.168.0.201:6379> LPUSH logs newloghehe
(integer) 1

192.168.0.201:6379> LTRIM num 0 99
OK

In this case, my list will always only have 100 entries, and I only look at the latest 100 entries! !!
 5.Collection types

Sets are learned in high school. The first semester is the set of studies.
Intersection ∩, union ∪, collection, etc. 0 0!

The elements of a collection are untyped!
Applications that use collection types are: (Sina Weibo shared a lot of redis applications)
For example: follow Weibo, such as whether the two of us are following a certain person.

5.1,
Add collection
192.168.0.201:6379> SADD jihe1 a b c
(integer) 3

5.2,
View collection content
192.168.0.201:6379> SMEMBERS jihe1
1) "c"
2) "a"
3) "b"
5.3, judging whether the collection elements exist
192.168.0.201:6379> SISMEMBER jihe1 d
(integer) 0
192.168.0.201:6379> SISMEMBER jihe1 a
(integer) 1
Returns 0 if it doesn't exist, returns 1 if it exists

5.4 Inter-set operations
Support: intersection, difference, union

Difference set operation:
192.168.0.201:6379> SDIFF jihe1 jihe2
1) "a"
jihe1abcjihe2b cd

jihe1 minus jihe2 minus the same b c, jihe1 is left with a

Similarly:
jihe2 minus jihe1
192.168.0.201:6379> SDIFF jihe2 jihe1
1) "d"

Difference operation can set multiple

Intersection operation:
 192.168.0.201:6379> SINTER jihe1 jihe2
1) "c"
2) "b"

Intersection can be set multiple:
Adding a jihe3
192.168.0.201:6379> SADD jihe3 d e f
(integer) 3

192.168.0.201:6379> SINTER jihe1 jihe2 jihe3
(empty list or set)
#This is because he is jihe1 and jihe2 first do the intersection operation, and then do the intersection operation with jihe3

Union operation
192.168.0.201:6379> SUNION jihe1 jihe2
1) "a"
2) "c"
3) "b"
4) "d"
You can also set multiple


5.4. The above sets are unordered. Redis supports ordered sets. His name is as follows.
ZADD key score member
ZSCORE key member
ZRANGE key start stop [WITHSCORES]
ZRANGEBYSCORE key min max

Add an ordered collection
192.168.0.201:6379> ZSCORE youxu 80 a
(nil)
192.168.0.201:6379> ZADD youxu 81 b
(integer) 1
Can add multiple
192.168.0.201:6379> ZADD youxu 82 c 83 d
(integer) 2

Get score
192.168.0.201:6379> ZSCORE youxu a
"80"
192.168.0.201:6379> ZSCORE youxu b
"81"
192.168.0.201:6379> ZSCORE youxu c
"82"
192.168.0.201:6379> ZSCORE youxu d
"83"

Get ordered collection range
192.168.0.201:6379> ZRANGE youxu 0 3 #Reference list set of 0 3 elements from 0 to 3
1) "a"
2) "b"
3) "c"
4) "d"

For example:
192.168.0.201:6379> ZADD youxu 79 e
(integer) 1

192.168.0.201:6379> ZRANGE youxu 0 4
1) "e"
2) "a"
3) "b"
4) "c"
5) "d
###### ee in front because his score is small!
Popular articles, you can use this to sort, I can set a score for him! !! !! !! !!
 

Redis-cluster cluster [Part 1]: Redis installation and redis data type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.