Reprinted from Http://blog.nosqlfan.com/html/2942.html?ref=rediszt
Phoenix Sword, Peacock Ling, Jasper Knife, passionate ring, parting hook, Overlord gun, fist is Cologne's seven kinds of weapons, and this article intends to use several ways of Redis Strings,hashs,Lists, sets, Sorted sets, Pub/sub andtransactions are also compared to seven weapons, explaining the seven characteristics of Redis and listing the appropriate applications.
Strings
The Strings data structure is a simple key-value type, and value is not only a string, it can also be a number. With the strings type, you can fully implement the current Memcached functionality and be more efficient. You can also enjoy Redis's timed persistence, operation logs, and replication functions. In addition to providing operations like get, set, INCR, DECR, and so on, Redis provides the following Memcached:
- Get string length
- Append content to a string
- Set and get a section of a string
- Set and get one of the strings (bit)
- Bulk set the contents of a series of strings
Hashs
In memcached, we often package structured information into HashMap, which is stored as a string value after the client is serialized, such as the user's nickname, age, gender, integral, and so on, when it is necessary to modify one of these items, it is usually necessary to remove all values after deserialization, Modify the value of an item, and then serialize the store back. This not only increases the overhead, but also does not apply to some scenarios where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The hash structure of Redis allows you to modify only one item property value just as you would update a property in a database.
Lists
Lists are linked lists, and people who believe that they have a knowledge of data structures should be able to understand their structure. With the lists structure, we can easily achieve the latest message ranking and other functions. Another application of the lists is Message Queuing, which can take advantage of the lists push operation to present the task in lists, and then the worker thread then takes the task out of execution with a pop operation. Redis also provides an API for manipulating a section of lists, where you can directly query and delete elements from a section of lists.
Sets
A sets is a collection, and the concept of a collection is a combination of a bunch of distinct values. Using the sets data structure provided by Redis, you can store some aggregated information, such as in a microblog application, where you can have a collection of all the followers of a user and a collection of all their fans. Redis also provides for the collection of intersection, set, difference sets and other operations, can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection.
Sorted Sets
Compared with sets, Sorted sets added a weight parameter score, so that the elements in the collection can be ordered by score, such as a storage of the class students Sorted sets, the collection value can be the student's number, And score can be their test scores, so that when the data is inserted into the collection, it has been a natural sort. Also can use sorted sets to do with the weight of the queue, such as the normal message score is 1, the important message of the score is 2, and then the worker can choose to press score reverse order to get work tasks. Let important tasks take precedence.
Pub/sub
Pub/sub literally is the release (Publish) and Subscription (Subscribe), in Redis, you can set a key value for message publishing and message subscription, when a key value on a message published, all subscribed to its client will receive the corresponding message. The most obvious use of this function is to use it as a real-time messaging system, such as regular live chat, group chat, and other functions.
Transactions
Who says NoSQL does not support transactions, although Redis's transactions provides not strictly acid transactions (such as a string of commands executed with Exec execution, in the execution of the server down, then there will be a part of the command execution, the rest is not executed), However, this transactions provides the basic command package execution function (in case the server does not have a problem, you can ensure that a series of commands are executed together in sequence, there will be other client commands inserted to execute). Redis also provides a watch function, you can watch a key, and then execute transactions, in the process, if the value of this watched is modified, then this transactions will find and refuse to execute.
Okay, so here's what this article is all about, these are just some of the uses of the Redis API, which is the tip of the iceberg, like a Swiss Army knife (or a more versatile Chinese army shovel), which creates a series of data structures and APIs that are closer to the application scenario. The goal is to make the storage design more straightforward based on the application scenario. More application scenarios and modular applications await your own exploration. or pay more attention to this site (nosqlfan.com), you can also find some interesting redis-based applications. You are also welcome to share your use of Redis with more friends through Nosqlfan.
Reproduced Seven types of Redis weapons