Redis Getting Started (b)

Sequence

SORT command using the set of operations classes, and lists and ordered collections; SORT key Alpha lexicographically arranged non-numeric. Key DESC SORT
  BY parameter, you can sort hash types, even the string type (according to the wildcard + name)
  Gets the value after get sorted
  save store
  optimization: the Sort redis is one of the most powerful and most complex commands. Use good can easily become a performance bottleneck. , n sort command time complexity of O (n + mlogM): the number of elements of the list sorted, returns the number of elements m. n is large, the lower sort performance.
  Minimize the number of elements 1 to be in the sort key (the n small)
  2 after use limit parameter fetch required data (m so small)
  3 If a large amount of data to sort, store the parameter as the result of using the cache.

expire date

expire key seconds to return 1 for success, 0 for failure. (No keys can not be set expired). Key Milli pexpire
  TTL Key, returns the remaining life of the bond, there is no return -2. Permanent (default) returns -1. Key Pttl
  presist, cancel the expiration time, make it permanent. Use set, getset will eliminate expired . Other instructions will not eliminate. watch do not think the key is deleted due to be changed.
  expireat, pexpireat; Unix time used as a parameter.
  Application: Implementing restrict access frequency. (String 1 using the incre; 2 list types: the recording time, number of times to calculate a difference)

Implement caching

In order to improve site load capacity, usually you need some high frequency of visits, but consume a large result of the operation of the CPU or IO resources cached and stale data regularly updated again. For example Weibo trending edition, five minutes expired key, re-evaluated once.

  However, some scenarios, if the use of a large cache, and long expiration time, then take up too much memory; if the expiration time is short, can lead to cache hit rate is too low and a big waste of memory in vain idle. Then:

  (Memory priority, need to eliminate rules): Modify maxmemory , Redis limit the maximum available memory (in bytes), exceeding this limit will not need to delete the key parameter specifies the basis maxmemory-policy strategy.

  LRU:Least Recently Used

Affairs

A transaction is a collection of commands, like the commands, Redis is the smallest unit of execution.
  multi open affairs, the next command will be put in the queue, exec submit, started queue commands. Transactions can make command in order to perform a transaction, so the client A wants to execute a few commands, the client sends a command B, A opened the transaction, then B will not be inserted into a few commands A client's execution.

Error Handling

  Syntax error , if there is a command syntax error, not the EXEC time.
  Run-time error , there is no rollback, you can only clean up their own mess.
  Watch : Observation of a change in the transaction key (because the transaction is performed once all of the instructions, the final result at this time can not return to the next step in accordance with a result of the instruction.) Watch may monitor one or more keys, once one of which keys were modified after the transaction (or delete) will not be executed, after executing exec, will cancel the monitoring of all keys. If you do not want to monitor the implementation of unwatch we can use to cancel monitoring. PS, after the multi command is executed after the exec.

notification

When a page needs to send a message, complex calculations, will block the rendering of the page. Avoid wait too long, consider using other asynchronous processes to achieve results. Here you can see P87
  Features: loose coupling, easy to expand.

Redis achieve the task queue :

Use Rpop can always take, of course, avoid no data has also been performed using BRpop can set timeout. Over time, it will return nil; 0 means no restriction wait, do not go on forever blocked.
  Priority Queue: Bpop key [key ...] 0 a plurality of keys detected, if all of the key elements is not blocked. News key priority in front of the consumer.

Published subscription model:

publish channel message sends a message to a channel, only the first subscription, after the announcement to acquire.
  subcribe channel subscription, can only be performed subcribe, ubsubbirbe, pSubcribe, punSubcribe command subscription status.
  Message received, there are three: subcribe, message, unSubcribe

Subscribe by the rules:

You can use pSubcribe, rules support glob style. Can be repeated subscription. Unsubscribe: pUnSubcribe (only unsubscribe psubcribe empathy for unsubcribe)
  pSubcribe Channel *?

pipeline

And redis client using the TCP protocol link, here total elapsed time, become round-trip delay.
  (The reason single-threaded?) In the implementation of multi-command, need to wait for the completion of a command (ie, receive the results redis returned) to perform.
  redis the underlying communication protocol provides support for the pipeline, the multi-hop transmission may be disposable and disposable conduit command returns the result in the implementation of the latter. By reducing the number of pipe in communication with the Redis. The fifth chapter talked about the programming language technology in the development pipeline.


Save memory:

Streamlining key names and values

Internal code optimization,

redis data type for each provide two internal coding. Hash type is achieved by a hash table. (P94)
  view object encoding intra-coded manner
  strings: Use REDIS_ENCODING_EMBSTR string code storage (similar to the encoding REDIS_ENCODING_RAW) also based sdshdr achieved, but the structure is in a contiguous memory space.
  Hash type:  Use HT and ZIPLIST saved if the field does not exceed the number of hash-max-ziplist-entries, and field length does not exceed the hash-max-ziplist-value is saved with ZIPLIST. HT hash table is stored, and values can be assigned to achieve O (1) time complexity. Fields and field values which are used redisObject, all optimization can be optimized by strings. ZIPLIST coding is compact, the expense of some of the reading, change to highly space efficient, less suitable for use in the element.

  List: LINKEDLIST ZIPLIST
  Collection Type: HT INTSET
  indexed collections SKIPLIST ZIPLIST

Guess you like

Origin www.cnblogs.com/RobertLionLin/p/11415854.html