Redis Learning II

Source: Internet
Author: User

Redis Learning II

tags (space delimited): Redis

One, link list structure

1,lpush key value (Rpush inserted into the tail of the linked list)
Role: Inserting values into the link header

2,rpop Key (Lpop key returns and deletes the head element of the linked list)
Function: Returns and deletes the chain footer element

3,lrange Key Start stop
Function: Returns the element in the list [Start, stop]
Rule: The left number starts at 0 and the right number starts at 1

link02link0 -1

4,lrem Key Count value
Function: Removes the value from the key linked list
Note: Delete the absolute value of count after the end of value
Count>0 Remove from table header
Count < 0 Delete from footer

5,lindex Key Index
Function: Returns the value on the index
such as Lindex Key 2

6,llen Key
Function: Count the number of elements in the linked table
Redis 127.0.0.1:6379> Llen Task
(integer) 3
Redis 127.0.0.1:6379>

7,linsert Key After|before Search value
Function: Search for ' search ' in the key list, and before the search value |, insert value
Note: Once a search is found, the command ends, so no more than one value is inserted

8,rpoplpush Source Dest
Function: Take out the tail of source, put it on the head of Dest,
and returns the cell value

Business logic:
1:rpoplpush Task Bak
2: Receive the return value and do business processing
3: If successful, Rpop bak cleanup task. If unsuccessful, take the task from the Bak table next time

9,brpop, Blpop key timeout
Function: Wait for the tail/head element of the popup key,
Timeout is the wait timeout time
If timeout is 0, wait

Scenario: Long polling ajax, when chatting online, can use

Second, Example: Bitmap method Statistics User Login situation

Three, set of SET command

The nature of a set: uniqueness, disorder, certainty

Note: In the string and link commands, you can access a certain number of characters in a string or several elements by using range
However, because of the unordered nature of the collection, some elements cannot be accessed by subscript or scope.
So want to see the elements, either randomly first, or select all

1,sadd Key value1 value2
Function: Add elements to the set key

2,srem value1 value2
Function: Deletes the set of elements in the collection as Value1 value2
Return value: The number of elements that were actually deleted after ignoring the nonexistent element

3,spop Key
Function: Returns and deletes 1 random elements in a key in the collection
Random – a manifestation of disorder

4,srandmember Key
Function: Returns a random 1 element in the set key.

5,sismember Key value
Function: Determine if value is in the key set
is return 1, no return 0

6,smembers Key
Function: Returns all elements in the set

7,scard Key
Function: Returns the number of elements in the collection

8,smove Source Dest Value
Function: Remove the value from source and add it to the Dest collection

9,sinter Key1 Key2 Key3
Function: Find the intersection of Key1 Key2 Key3 three sets, and return

Redis 127.0.0.1:6379> Sadd S10 2 4 6(integer)4Redis127.0.0.1:6379> Sadd s21 2 3 4(integer)4Redis127.0.0.1:6379> Sadd S34 8 9  A(integer)4Redis127.0.0.1:6379> sinter S1 s2 S31)"4"Redis127.0.0.1:6379> sinter s3 S1 s21)"4"

10,sinterstore dest Key1 Key2 Key3
Function: Find the intersection of Key1 Key2 Key3 three sets, and assign to Dest

11,suion Key1 Key2. Keyn
Function: Find the Key1 key2 Keyn, and return

12,sdiff Key1 Key2 Key3
Function: Find out the difference between Key1 and Key2 Key3
namely Key1-key2-key3

Four, Order set command set

1,zadd key Score1 value1 score2 value2.
adding elements

redis 127.0.0.1:6379zaddstulilyhmmlileililei(integer) 3

2,zrem key value1 value2.
Action: Delete an element in a collection

3,zremrangebyscore Key min Max
Function: Delete elements according to Socre, delete score between [Min,max]

redis127.0.0.1:63794102127.0.0.1:63790 -11"f"

4,zremrangebyrank Key Start end
function: Delete the element by rank, delete the rank in [Start,end]

redis127.0.0.1:6379012127.0.0.1:63790 -11"c"2"e"3"f"4"g"

5,zrank Key Member
Query member rankings (up to 0 start)

6,zrevrank Key Memeber
Query member rankings (starting from 0)

7,zrange key start stop [Withscores]
Returns the elements of the rank [start,stop] after sorting the set
The default is the ascending order
Withscores is printing the score, too.

8,zrevrange Key Start stop
Function: Arranges the set in descending order, taking the element between the name [Start,stop]

9,zrangebyscore key min Max [withscores] limit offset N
Function: Set (ascending continuation) after sorting, take score in [Min,max] elements,
and skip the offset, take out n

Redis127.0. 0. 1:6379> Zadd stu1 a 3B4C9E AF theGinteger)6Redis127.0. 0. 1:6379> Zrangebyscore stu3  ALimit1 2Withscores1)"C"2)"4"3)"E"4)"9"

10,zcard Key
Returns the number of elements

11,zcount Key min Max
Returns the number of elements in the [Min,max] Interval

12,zinterstore destination Numkeys key1 [Key2 ...]
[WEIGHTS weight [weight ...]
[AGGREGATE sum| min| MAX]
To find the intersection of Key1,key2, the weights of Key1,key2 were weight1,weight2
Aggregation method by: Sum |min|max
Aggregated results, saved within the Dest collection

Note: Weights, how does aggregate understand?
Answer: If there is intersection, the intersection element and Socre,score how to deal with it?
Aggregate sum->score Add, min for min score, Max Max score

Another: can be set by weigth the weight of different keys, intersection, Socre * weights

See the following example

Redis 127.0.0.1:6379> Zadd Z12A3B4C (Integer)3Redis127.0.0.1:6379> Zadd Z22.5A1B8D (Integer)3Redis127.0.0.1:6379> Zinterstore tmp2Z1 Z2 (integer)2Redis127.0.0.1:6379> Zrange tmp0-11)"B"2)"a"Redis127.0.0.1:6379> Zrange tmp0-1Withscores1)"B"2)"4"3)"a"4)"4.5"Redis127.0.0.1:6379> Zinterstore tmp2Z1 z2 aggregate sum (integer)2Redis127.0.0.1:6379> Zrange tmp0-1Withscores1)"B"2)"4"3)"a"4)"4.5"Redis127.0.0.1:6379> Zinterstore tmp2Z1 z2 aggregate min (integer)2Redis127.0.0.1:6379> Zrange tmp0-1Withscores1)"B"2)"1"3)"a"4)"2"Redis127.0.0.1:6379> Zinterstore tmp2Z1 Z2 weights1 2(integer)2Redis127.0.0.1:6379> Zrange tmp0-1Withscores1)"B"2)"5"3)"a"4)"7"
Five, transactions in Redis

Redis supports simple transactions. Comparison of Redis and MySQL transactions

rollback与discard 的区别如果已经成功执行了2条语句, 第3条语句出错.Rollback后,前2条的语句影响消失.Discard只是结束本次事务,前2条语句造成的影响仍然还在注:在mutil后面的语句中, 语句出错可能有2种情况1: 语法就有问题, 这种,exec时,报错, 所有语句得不到执行2: 语法本身没错,但适用对象有问题. 比如 zadd 操作list对象Exec之后,会执行正确的语句,并跳过有不适当的语句.
Thinking: I'm buying a ticket ticket-1, Money- -and the ticket only1Zhang, if after I multi, and exec before, the ticket was bought by others---namely ticket becomes0. How can I observe this situation, and no longer submit pessimistic thoughts: the world is full of danger, someone must rob me, lock ticket, only I can operate. [Pessimistic lock] optimistic idea: not so people and I rob, therefore, I just need to pay attention,--there is no one to change the value of ticket can be [optimistic lock]redis transactions, enabled is optimistic lock, only responsible for monitoring key has not been changed. "Specific commands----Watch Command example: Redis127.0. 0. 1:6379> Watch Ticketokredis127.0. 0. 1:6379> Multiokredis127.0. 0. 1:6379> DECR Ticketqueuedredis127.0. 0. 1:6379> Decrby Money -Queuedredis127.0. 0. 1:6379> exec (Nil)//return nil, indicating that the monitoring ticket has changed and the transaction is canceled. Redis127.0. 0. 1:6379> Get Ticket"0"Redis127.0. 0. 1:6379> Get Money" the"Watch Key1 Key2...Keyn function: Monitor Key1 key2. Keyn There is no change, if there is a change, then the transaction cancels the Unwatch effect: cancel all watch monitoring
Six, message Subscription

Subscriber: Subscribe Channel name
Publisher: Publish channel name publish content

Client Example:

Redis127.0. 0. 1:6379>Subscribe newsreading Messages...(Press Ctrl- C  toQuit1)"Subscribe"2)"News"3) (integer)11)"Message"2)"News"3)"Good Good study"1)"Message"2)"News"3)" Day-day-up"

Service-side examples:

127.0.0.1:6379‘good good study‘(integer1127.0.0.1:6379‘day day up‘(integer1
Seven, the persistence of an RDB

There are 2 ways to persist Redis 1 Snapshot 2 is log

Snapshot persistence:

Configuration options for RDB snapshots

9001      // 900内,有1条写入,则产生快照 3001000   // 如果300秒内有1000次写入,则产生快照6010000  // 如果60秒内有10000次写入,则产生快照(这3个选项都屏蔽,则rdb禁用)
stop-writes-on-bgsave-error yes  //后台备份进程出错时,主进程停不停止写入?rdbcompression yes    //导出的rdb文件是否压缩Rdbchecksum   yes     //导入rbd恢复时数据时,要不要检验rdb的完整性dbfilename dump.rdb  //导出来的rdb文件名./  //rdb的放置路径

AOF Persistence:

Configuration of the Aof

appendonlyno# 是否打开 aof日志功能appendfsync always   # 每1个命令,都立即同步到aof. 安全,速度慢# 折衷方案,每秒写1次no      # 写入工作交给操作系统,由操作系统判断缓冲区大小,统一写入到aof. 同步频率低,速度快,no-appendfsync-on-rewrite  yes# 正在导出rdb快照的过程中,要不要停止同步aof100#aof文件大小比起上次重写时的大小,增长率100%时,重写#aof文件,至少超过64M时,重写

Note: What does aof rewrite mean?
A: AoF rewrite refers to the memory of the data, the inverse of the command, written to the. AoF log.
To resolve the problem that the AOF log is too large.

Q: If the Rdb file, and the AoF file are present, who will be the first to recover the data?
Answer: AOF

Q: Can 2 types be used at the same time?
A: Yes, and recommend it.

Q: Which of the RDB and AoF recovers quickly when recovering
A: The RDB is fast, because it is the memory map of the data, directly loaded into the memory, and AOF is the command, need to be executed

Redis Learning II

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.