Redis Learning Summary 1

Source: Internet
Author: User

Redis is an open source API that is written in ANSI C, supports the Web , can be persisted in memory, key-value databases, and provides multiple languages. Unlike the normal key-value structure, the Redis key supports a flexible data structure, with the exception of strings, hashes, lists, sets, and sorted sets.

  1. Data type

As a key-value database, Redis also provides a mapping of key (key) and key value (value). However, in addition to the regular numeric value or string, the Redis key value can also be one of the following forms:

Lists (list)

Sets (collection)

Sorted sets (Ordered collection)

Hashes (hash table)

The data type of the key value determines the operation that the key value supports. Redis supports advanced atomic operations such as the intersection of lists, sets, or ordered sets, set, and set, and, if the type of the key value is a normal number, Redis provides self-increment atomic operations.

2. master-Slave synchronization

Redis supports synchronizing data to multiple slave libraries, a feature that is useful for improving read performance.

3, the following is the classic Redis application scenario

  (1), take the latest n data operation

For example, the most recent article on your website, in the following way, we can put the latest 5,000 reviews of the ID in the Redis list collection, and will be out of the collection part from the database.

Use the Lpush latest.comments command to insert data into the list collection

Insert complete and then use the LTrim latest.comments 0 5000 command to keep the last 5,000 IDs forever

Then we can use the following logic when retrieving a page comment from the client

FUNCTION get_latest_comments (start,num_items):
Id_list = Redis.lrange ("latest.comments", start,start+num_items-1)
IF Id_list.length < Num_items
Id_list = sql_db ("Select ... ORDER by Time LIMIT ... ")
END
RETURN id_list
END

If you have a different filter dimension, such as the newest n for a category, you can build a list that is categorized by this category, and Redis is very efficient if you save the ID.

 (2), leaderboard application, take top n operation

This requirement differs from the above requirements in that the preceding operation takes the time as the weight, this is the weight of a certain condition, such as the number of times by the top, then we need our sorted set to go, set the value you want to sort into the score of sorted set, Set the specific data to the corresponding value, each time only need to execute a zadd command.

(3), there is a requirement: Redis has some hash, each hash field corresponds to its count Num. In 10 minutes, some field-corresponding NUM will have a different number of additions.

The question is: I want to count num for this 10 minute change. and count into the database.

OK, then you can take advantage of the ordered set of sorted set. The elements of the collection are hash, and the collection is sorted by time.

  

    $key= ' ad| '.$id; $Redis->hincrby ($key, ' Num ',$incre);//$key the corresponding num increments each time $incre      $store= ' store| '. ' 2015-4-27 '; $score= Time().Rand(10000,99999); $Redis->zadd ($store,$score,$key);//$key by timestamp in the Redis ordered set//information is retained for 10 days    $keey=864000; $keeptime= Time()+$keep; $Redis->expireat ($store,$keeptime);

(4), pub/sub build real-time message system

Redis's pub/sub system can build real-time messaging systems, such as many examples of real-time chat systems built with Pub/sub.

  (5), build the queue system

Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.

Four, the basic operation of Redis

Commands related to connection operations

    • Quit: Close connection (connection)
    • Auth: simple Password Authentication


Persistence of

    • Save: Synchronize data to disk
    • Bgsave: Asynchronously saving data to disk
    • Lastsave: Returns the UNIX timestamp when the data was last successfully saved to disk
    • Shundown: Save data synchronously to disk, and then close the service


Remote Service Control

    • Info: Provide information and statistics about the server
    • Monitor: Live dump of received requests
    • slaveof: Changing Replication policy settings
    • Config: Configure the Redis server at run time


commands for the value operation

    • Exists (key): Verify that a key exists
    • Del (key): Delete a key
    • Type (key): Return value types
    • Keys (pattern): Returns all keys that satisfy the given pattern
    • Randomkey: Randomly returns one of the key spaces
    • Keyrename (Oldname, newname): Rename key
    • Dbsize: Returns the number of keys in the current database
    • Expire: Set the active time of a key (s)
    • TTL: Get the active time of a key
    • Select (Index): Query by index
    • Move (Key, Dbindex): Moves the key in the current database to the Dbindex database
    • FLUSHDB: Delete all keys in the currently selected database
    • Flushall: Delete all keys in all databases


commands for string manipulation

    • Set (key, value): Assigns a value to a string named key in the database
    • Get (Key): Returns the value of a string named key in the database
    • Getset (key, value): Assigns the last value to a string named key
    • Mget (Key1, Key2,..., key N): Returns the value of multiple strings in the library
    • SETNX (key, Value): Add string, name key, value
    • Setex (Key, Time, value): Adds a string to the library, sets the expiration times
    • Mset (key n, value N): Sets the value of multiple strings in bulk
    • Msetnx (key n, value N): If all strings with the name key I do not exist
    • INCR (Key): 1 operation with a string named key
    • Incrby (Key, Integer): String with Name Key added integer
    • DECR (key): string minus 1 operation with name key
    • Decrby (Key, Integer): string with the name key reduced by integer
    • Append (key, value): String value with the name key appended value
    • SUBSTR (key, start, end): Returns a substring of the value of string with the name key


commands for the list operation

    • Rpush (key, value): Adds an element of value to the list at the end of the name key
    • Lpush (key, value): Adds an element of value to the list header with the name key
    • Llen (Key): Returns the length of the list named key
    • Lrange (key, start, end): Returns the element between start and end in list named key
    • LTrim (key, start, end): Intercepts the list named key
    • Lindex (key, index): Returns the element of the index position in the list named key
    • LSet (key, index, value): Assigns a value to the element of the index position in the list named key
    • Lrem (Key, Count, value): Deletes the value in the list of Count key elements
    • Lpop (Key): Returns and removes the first element in a list named key
    • Rpop (Key): Returns and Deletes the tail element in the list named key
    • Blpop (Key1, Key2,... key N, timeout): The block version of the Lpop command.
    • Brpop (Key1, Key2,... key N, timeout): The block version of Rpop.
    • Rpoplpush (Srckey, Dstkey): Returns and removes the tail element of the list named Srckey, and adds the element to the head of the list named Dstkey


commands for set operations

    • Sadd (Key, member): Adds an element to a set named Key member
    • Srem (Key, member): Removes the element in the set named Key member
    • Spop (key): Randomly returns and deletes an element in a set with the name key
    • Smove (Srckey, Dstkey, member): Move to the collection element
    • SCard (Key): Returns the cardinality of a set named key
    • Sismember (Key, member): Member is a set element with the name key
    • Sinter (Key1, Key2,... key N): Intersection
    • Sinterstore (Dstkey, (keys)): Seek the intersection and save the intersection to the collection of Dstkey
    • Sunion (Key1, (keys)): Seek and set
    • Sunionstore (Dstkey, (keys)): Sets and saves the assembly to the Dstkey collection
    • Sdiff (Key1, (keys)): Differential set
    • Sdiffstore (Dstkey, (keys)): Differential set and save the difference set to the collection of Dstkey
    • Smembers (Key): Returns all elements of a set with the name key
    • Srandmember (key): Randomly returns an element of a set with the name key


commands for hash operations

      • Hset (Key, field, value): Adds an element to a hash named key field
      • Hget (Key, field): Returns the value of field corresponding to the hash named key
      • Hmget (Key, fields): Returns the value of field I in the hash named key
      • Hmset (Key, fields): Adds an element to a hash named key field
      • Hincrby (Key, field, integer): Adds an integer to the value of field in the hash named key
      • Hexists (Key, field): A field with the key field in the hash named key
      • Hdel (Key, field): Delete The field with the key field in the hash named key
      • Hlen (Key): Returns the number of elements in a hash with the name key
      • Hkeys (Key): Returns all keys in a hash with the name key
      • Hvals (Key): Returns the value corresponding to all keys in a hash with the name key
      • Hgetall (Key): Returns all keys (field) and their corresponding value in a hash with the name key

 

Part of the information excerpt from: http://www.cnblogs.com/oubo/archive/2011/09/07/2394568.html

Thank

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