Redis Cluster introduction and five data types-1

Source: Internet
Author: User
Tags delete key redis cluster install redis

Introduction to Redis:

Redis 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.

Redis and memcached comparison:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/18/wKioL1Yy0AjDD0zqAAHJ1UsUaF8076.jpg "title=" 123. PNG "alt=" wkiol1yy0ajdd0zqaahj1usuaf8076.jpg "/>

Noun Explanation:

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 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.

First, 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.gztar-xvf redis-3.0.4.tar.gzmake make install cd/opt/ Redis-3.0.4/src/make Test

3. Configuring Redis

Cp/opt/redis-3.0.4/utils/redis_init_script/etc/init.d/redis #复制管理脚本chmod +x/etc/init.d/redismkdir/etc/rediscp/op T/redis-3.0.4/redis.conf/etc/redis/6379.conf This is started when it is started in the foreground, change him to start in the background vim/etc/redis/6379.confdaemonize No to Daemonize Yes added to system service vim/etc/init.d/redis#chkconfig:3595 #添加至文件推出执行命令: Chkconfig–add Redischkconfigredis on

Second, Redis basic operation

Set Setting key

Get to determine the value of key

exists determine if key exists

Keys shows all keys.

Del Delete the specified key

Type get key types

Note: Redis is case-insensitive, and commands are best capitalized so that you can differentiate between commands or Parameters!!!!

Example:

1, set of the example:

192.168.0.201:6379> SET Hello heheok192.168.0.201:6379> GET hello "hehe"

2. Set multiple key value and use the keys * to see all

192.168.0.201:6379> set Hello1 hehe1ok192.168.0.201:6379> set Hello2 hehe2ok192.168.0.201:6379> KEYS *) " Hello1 "2)" Hello "3)" Hello

Key matching method:

? Match a single

* Match All


3. Determine if key exists

Determine if key is in use: EXISTS He returned is plastic: 0 does not exist, 1 exists

192.168.0.201:6379> EXISTS Hello (integer) 1192.168.0.201:6379> EXISTS hehe (integer) 0

4. Delete key

192.168.0.201:6379> del Hello (integer) 1 #这里的1是数量删除多个测试下:192.168.0.201:6379> DEL hello1 Hello2 (integer) 2

5. View Type

As long as the set type is the string. View Type command with type

192.168.0.201:6379> TYPE hellostring

6, Keyspace

Redis is a default of up to 16 multiple instances, and you can modify the configuration file to support more!

Use the info command to view!

# Keyspacedb0:keys=1,expires=0,avg_ttl=0db0: This can be understood as a namespace. Support up to 16, use Select to switch 192.168.0.201:6379> select 1OK try adding a key-valueset db1 hehe and then use info to see # Keyspacedb0:keys=1, Expires=0,avg_ttl=0db1:keys=1,expires=0,avg_ttl=0

7. Configuring the Redis configuration file

LogFile "/var/log/redis.log" specifies that the log file will be output in the console if it is not specified

Port 6379

Databases 16 default supported up to 16 database can be modified

Dir./ This means that the default persistence profile is placed there! Suggested changes under!

Pidfile/var/run/redis_6379.pid #如果起多个实例的话上面的都需要改


Save Exit Stop Service

[[Email protected]]$/etc/init.d/redis Stop

/var/run/redis_6379.pid does not exist, process was not running

This is the configuration file that should be specified in the 6379.conf config file is not correct, and/etc/init.d/redis different repair needs to be modified under!

Pidfile/var/run/redis_6379.pid

Vim/etc/redis/6379.conf

Then kill the Redis process under test!start | Stop

Iii. 5 Big Data types for Redis

He uses different commands to distinguish what type of data you want to manipulate.

Types cannot be nested and cannot be mixed! But there's a king-fry: Set can change all types to string types

1. String Type

SET

GET

Del

APPEND appended to the value

Set can be reset but if you want to add it, use append best for example:

192.168.0.201:6379> SET hehe hellook192.168.0.201:6379> GET hehe "Hello" 192.168.0.201:6379> APPEND hehe, world (integer) 11192.168.0.201:6379> GET hehe "Hello,world"

You can set multiple values and query values at the same time with Mset and Mset

192.168.0.201:6379> MSET key1 v1 key2 v2 key3 v3ok192.168.0.201:6379> MGET key1 key2 key31) "v1" 2) "V2" 3) "V3"

Get string length

192.168.0.201:6379> STRLEN hehe (integer) 11

If the string is in Chinese, he will output 1 characters, such as 3 strings, according to the UTF-8 format.

192.168.0.201:6379> SET Key "hehe" ok192.168.0.201:6379> GET key "\xe5\x91\xb5\xe5\x91\xb5"

2. Self-increment type

For example, under +1, it is not realistic to use set to modify the set every single point. All Redis has a self-increment type: INCR

192.168.0.201:6379> INCR num #默认如果没有这个值的话, INCR will automatically create a value that defaults to zero, and when you do not do it once, he will be +1 (integer) 1192.168.0.201:6379> INC R num (integer) 2192.168.0.201:6379> INCR num (integer) 3192.168.0.201:6379> INCR num (integer) 4192.168.0.201:6379 > INCR num (integer) 5192.168.0.201:6379> INCR num (integer) 6

If you want to add more: Incrby

192.168.0.201:6379> incrby num (integer) 57192.168.0.201:6379> incrby num (integer) 67192.168.0.201:6379> Incrby num (integer) 77192.168.0.201:6379> incrby num (integer) 87192.168.0.201:6379> incrby num (integer) 97 192.168.0.201:6379> incrby num (integer) 107

Minus it? Decr

192.168.0.201:6379> decr num (integer) 106192.168.0.201:6379> decr num (integer) 105192.168.0.201:6379> DECR Num (integer) 104

If we lose more: Decrby

192.168.0.201:6379> Decrby num 5 (integer) 97192.168.0.201:6379> decrby num 5 (integer) 92192.168.0.201:6379> Decrby num 5 (integer) 87192.168.0.201:6379> decrby num 5 (integer) 82

To support decimal points:

Incrbyfloat key 0.1192.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

Hash

Like a table stored in a database, a table is not a field, 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

To set the hash type:

192.168.0.201:6379> hset Shouji name iphone (integer) 1192.168.0.201:6379> hset Shouji Co red (integer) 1192.168.0.201:6379> hset Shouji price 8888 (integer) 1

Inquire:

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 shouji1) "name" 2) "iphone" 3) "CO" 4) "Red" 5) "Price" 6) "8888"

It's not really looking right now, but he's got some API calls to the page, and the values from the typography are pretty good.

192.168.0.201:6379> hmset Diannao name ThinkPad Co black price 30ok192.168.0.201:6379> Hmget Diannao name Co price1) "ThinkPad" 2) "BLACK" 3) "30"

4. List type

List type: He is storing an ordered list of strings when this "orderly" is coming in!

List you add to the left and add to the right his time complexity is the same! O1 (complexity of time)

It can be understood as: my speed does not increase with the number of increase!    Like 1000 rows and 10,000 rows, he spends the same time! University data structure in the study


Complexity of Time:

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 the algorithm analysis is to select the suitable algorithm and the improved algorithm.

In computer science, the time complexity of an algorithm is a function that quantitatively describes the time it takes to run the algorithm.


But he has a flaw, for example, there are 10,000 keys in it. You want to find the No. 999 one. He counted from 1 to 999.

Advantages, you read the top 100, the card read directly to the head is very fast


Lpush key value [value ...]

Rpush key value [value ...]

Lpop Key

Rpop Key

Lrange Key Start stop

Lrem Key Count value


Add key from left

192.168.0.201:6379> lpush num 0 (integer) 1192.168.0.201:6379> lpush num 1 (integer) 2192.168.0.201:6379> LPUSH Num 2 (integer) 3

Now it's from the left side plus

2 1 0

Start 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

If you want to get the length, use Lne! Get list type length is: Llen

192.168.0.201:6379> llen num (integer) 5

Take key from the left.

Take this key out of the list type (it's gone) and take the first left from the left.

192.168.0.201:6379> lpop num "2"

The first one on the left is 2 then this key becomes the key after you take it out.

1 0 3 5

Take the key from the right and the first one on the right (the 5 is taken out)

192.168.0.201:6379> rpop num "5"

Now look at the length of this key

192.168.0.201:6379> llen num (integer) 3

Get a range of a list:

Now this is the value

1 0

3

192.168.0.201:6379> lrange num 0 1 #取0-1 value 1) "1" 2) "0"

# # #这个值的输出结果是这样的: By default you are the value from the left, then the value of 0-1 is like this, the first element on the left is 1 (corresponding to 0 of this identifier), the left side of the second element is 0 (corresponding to 1 this identity bit)

This is not very good understanding a bit around, then add two values to explain in more detail

192.168.0.201:6379> lpush num 2 (integer) 4192.168.0.201:6379> rpush num 4 (integer) 5
192.168.0.201:6379> lrange num 0-1 #这里的 (-1) indicates left first 1) "2" 2) "1" 3) "0" 4) "3" 5) "4"

Gets the value of the specified element:

Get the first value on the right:

192.168.0.201:6379> LINDEX num-1 "4"

Gets the second value of the left side:

192.168.0.201:6379> LINDEX num-2 "3"

What about the 3?

192.168.0.201:6379> LINDEX num-3 "0"

This is the 3rd value from the right number!!!!!


Get values from the left

192.168.0.201:6379> LINDEX num 0 "2" 192.168.0.201:6379> LINDEX num 1 "1"

Keep only specified data


Keep only 0 to 2 of the data

192.168.0.201:6379> LTRIM num 0 2OK See results:192.168.0.201:6379> lrange num 0-11) "2" 2) "1" 3) "0"


What's the use of this:

Write the log, I this buffer, only keep the last 100 logs!

Like what:

192.168.0.201:6379> lpush logs Newloghehe (integer) 1192.168.0.201:6379> LTRIM num 0 99OK

In this case my list will always only 100, I only see the last 100 logs!!

5. Collection type

A set is a tall school, the first semester is a collection of learning

Intersection ∩, ∪, collection, etc. 0 0!


The elements of the collection are of no type!

Use of the collection type of application is: (Sina Weibo shared a lot of redis applications)

For example: Focus on micro-blog, such as whether we have a common concern about a person and so on.

Add Collection

192.168.0.201:6379> Sadd jihe1 a b C (integer) 3

View Collection Contents

192.168.0.201:6379> smembers jihe11) "C" 2) "a" 3) "B"

Determine whether the collection element exists

192.168.0.201:6379> sismember jihe1 d (integer) 0192.168.0.201:6379> sismember jihe1 A (integer) 1 returns 0 description not present return 1 description existing

Inter-collection operations

Support: Intersection, Difference set, and set

Differential set operation:

192.168.0.201:6379> Sdiff jihe1 jihe21) "a"
Jihe1 A B C
Jihe2 B C D

Jihe1 minus Jihe2 minus the same B C, Jihe1 is still a.

Similarly:

Jihe2 minus Jihe1

192.168.0.201:6379> Sdiff jihe2 jihe11) "D"

The difference set operation can set up multiple


Intersection operation:

192.168.0.201:6379> SINTER jihe1 jihe21) "C" 2) "B"
The intersection can be set multiple: Add a jihe3192.168.0.201:6379> sadd Jihe3 d E F (integer) 3192.168.0.201:6379> SINTER jihe1 jihe2 jihe3 ( Empty list or set)

#这个是因为他是jihe1和jihe2先做交集运算, and then do the intersection operation with Jihe3.

The set operation

192.168.0.201:6379> sunion jihe1 jihe21) "a" 2) "C" 3) "B" 4) "D"

You can also set multiple

The above collection is unordered, and Redis supports an ordered collection his name is as follows

Zadd key Score member add element

Zscore key member gets the fraction of the element

Zrange key start stop [Withscores]

Zrangebyscore Key min Max


Add an Ordered collection

192.168.0.201:6379> Zscore Youxu A (nil) 192.168.0.201:6379> Zadd youxu bayi B (integer) 1 can add multiple 192.168.0.201:6379& Gt Zadd Youxu-C-D (integer) 2

Get score

192.168.0.201:6379> Zscore Youxu A "up" 192.168.0.201:6379> zscore youxu B "Bayi" 192.168.0.201:6379> ZSCORE youxu C "192.168.0.201:6379>" Zscore Youxu D "83"

Get an ordered collection range

192.168.0.201:6379> zrange Youxu 0 3 #参考列表集合的0 3 elements from 0 to 3 1) "a" 2) "B" 3) "C" 4) "D"

In an example:

192.168.0.201:6379> zadd youxu E (integer) 1192.168.0.201:6379> zrange youxu 0) "E" 2) "a" 3) "B" 4) "C" 5) "D

##### #e在前面因为他的score小!

Top articles that can be sorted with this, I can set a score for him!


This article is from the "Vibrating Heartstrings" blog, please be sure to keep this source http://shuainotebook.blog.51cto.com/1271282/1707966

Redis Cluster introduction and five data types-1

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.