Cainiao watching Redis (1), cainiao watching redis

Source: Internet
Author: User
Tags set set redis cluster

Cainiao watching Redis (1), cainiao watching redis

I. Redis Introduction

Redis is an open-source software written in C language and can run on Linux. Currently, it does not support Windows. Redis is usually used for caching, data persistence, and message queue. Redis avoids memory data loss after the server fails. Redis supports five data structures:Strings, hashes, lists, sets, sorted setsAnd the operations on these data structures are atomic, which means that these sets of operations are thread-safe. Redis first places the dataset on the memory, you can set the persistent data to the hard disk at intervals. If you only need Redis for caching in the project, persistence can also be disabled. Redis supports master-slave synchronization like Mysql, data on the master server can be synchronized to N slave servers and read/write splitting is supported.

Redis can be used in most programming languages. C, C ++, Java, C #, and Python can use the Redis service through the IP address and port of the Redis server through corresponding APIs. The following section describes the simple operations on the Redis data structure using Java as an example.

 

Ii. Data Structures and simple operations supported by Redis

1. String

In the String-type data structure, Redis and Memcached both support Key-Value data storage (one Key corresponds to one Value, which is equivalent to HashMap in Java ), you can set a value for a Key, add a string to the Value under a specific Key, delete a specific Key, and perform atomic addition and subtraction on the numeric values of the corresponding Key.

 

Public class RedisClient {private static final String HOST = "192.168.146.129"; private static final int PORT = 6379; private final Jedis jedis = new Jedis (HOST, PORT ); /*** Test Redis for Key-Value access */@ Test public void testKeyValue () {jedis. set ("name", "boruoyihao"); System. out. println ("Get name from redis:" + jedis. get ("name"); System. out. println ("Is exist in redis:" + jedis. exists ("name"); jedis. append ("name", ", Hello"); // Add System under the same Key. out. println ("Get name from redis:" + jedis. get ("name"); jedis. del ("name"); // delete the Key System. out. println ("Get name from redis:" + jedis. get ("name"); // equivalent to jedis. set ("name", "boreyihao"); // jedis. set ("age", 26); jedis. mset ("name", "boreyihao", "age", "26"); System. out. println (jedis. get ("name") + "=" + jedis. get ("age"); // set the name validity period to 2 S jedis. setex ("name", 2, "boruo"); System. out. println ("Get name from redis:" + jedis. get ("name"); try {Thread. sleep (3000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} // name time-out. The retrieved data is null System. out. println ("Get name from redis:" + jedis. get ("name"); // atomic increment operation jedis. set ("age", "26"); jedis. incr ("age"); System. out. println ("Get age from redis:" + jedis. get ("age"); jedis. decrBy ("age", 3); System. out. println ("Get age from redis:" + jedis. get ("age "));}}

2. hashes

Redis Hash can be stored in a Map object with a key Value. It can obtain the entire Map object, the key set or Value set of the Map object, or the Value of the specified Field, you can also add or subtract the integer of a specified Field. Redis can store the attributes and attribute values of objects, but memcached can only be converted to strings. to modify the object value, you can only extract the string and convert it into an object, and then modify the object, converting it into a string and saving it back to memcached is costly, which is the advantage of Redis.

@ Test public void testHash () {// test hashmap Map <String, String> m = new HashMap <String, String> (); m. put ("name", "Tom"); m. put ("major", "Software"); m. put ("age", "43"); jedis. hmset ("m", m); List <String> name = jedis. hmet ("m", "name"); // If m is the key and name is the key of m, the return value is List System. out. println (name); System. out. println ("Get Hash Values =" + jedis. hvals ("m"); Iterator <String> it = jedis. hkeys ("m "). iterator (); while (it. hasNext () {String key = it. next (); System. out. println (key + ":" + jedis. hmet ("m", key);} System. out. println ("Get keys from redis:" + jedis. hkeys ("m"); System. out. println ("Get Values from redis:" + jedis. hvals ("m"); Map <String, String> map = jedis. hgetAll ("m"); System. out. println ("Get map from Redis:" + map); // Test hashset System. out. println ("--- test hash set"); jedis. hset ("s", "name", "Jack"); jedis. hset ("s", "age", "25"); System. out. println (jedis. hexists ("s", "name"); System. out. println (jedis. hget ("s", "name"); System. out. println (jedis. hgetAll ("s"); Map <String, String> smap = jedis. hgetAll ("s"); System. out. println (smap); System. out. println (jedis. hdel ("s", "name"); // clear the name attribute System. out. println (jedis. hincrBy ("s", "age", 3); System. out. println ("after incr:" + jedis. hgetAll ("s "));}

 

3. lists

List is a two-way linked List, because Redis supports insert and pop-up operations at both ends of the linked List. Therefore, the List of Redis can be used in both a two-way linked List, queue, and stack.

@ Test public void testList () {System. out. println ("test List"); jedis. del ("task"); jedis. rpush ("task", "do homework"); // Add jedis at the end of the linked list. rpush ("task", "clean housr"); jedis. lpush ("task", "rest"); // Add jedis to the head of the linked list. lpush ("task", "watch TV"); System. out. println ("length:" + jedis. llen ("task"); System. out. println ("Get all List:" + jedis. lrange ("task", 0,-1); //-1 indicates that the System is retrieved from start to end. out. println ("target Index:" + jedis. lindex ("task", 2); System. out. println (jedis. lpop ("task"); // retrieves the header data System. out. println ("Get all List:" + jedis. lrange ("task", 0,-1); System. out. println (jedis. rpop ("task"); // retrieves the tail data System. out. println ("Get all List:" + jedis. lrange ("task", 0,-1 ));}

 

 

 

4. sets

Redis's Set is equivalent to Java's hashset. The elements are arranged in an orderly manner. What's more, it can easily find the Union, intersection, and difference sets of two sets.

 

@ Test public void testSet () {System. out. println ("test Set"); jedis. del ("s1"); jedis. sadd ("s1", "4", "1", "3", "20"); // ordered System. out. println ("Get all s1:" + jedis. smembers ("s1"); // ordered System. out. println ("Get no s1:" + jedis. scard ("s1"); jedis. sadd ("s2", "4", "11", "13", "34", "3", "20"); System. out. println ("Get all s2:" + jedis. smembers ("s2"); // ordered System. out. println ("Get no s2:" + jedis. scard ("s2"); System. out. println (jedis. sinter ("s1", "s2"); // intersection System. out. println (jedis. sunion ("s1", "s2"); // Union System. out. println (jedis. sdiff ("s1", "s2"); // difference set}

 

 

 

5. sorted sets

Redis Sorted Sets are complementary to Set. They can be Sorted Based on weights and cannot be duplicated.

@ Test public void testSortedSet () {System. out. println ("testSortedset"); jedis. zadd ("ss", 12, "Tom"); // The Center is the weight, which is sorted by the weight. zadd ("ss", 1, "Jack"); jedis. zadd ("ss", 20, "David"); jedis. zadd ("ss", 3, "Jim"); System. out. println ("sortedset length:" + jedis. zcard ("ss"); // obtain the number of elements System. out. println ("sorted set:" + jedis. zrange ("ss", 0,-1); // The output lists jedis by weight. zadd ("ss1", 1, "Tom"); // The Intermediate Value is jedis. zadd ("ss1", 3, "Jimmy"); jedis. zadd ("ss1", 2, "Alex"); jedis. zadd ("ss1", 5, "Jim"); System. out. println ("sortedset length:" + jedis. zcard ("ss1"); // obtain the number of elements System. out. println ("sorted set:" + jedis. zrange ("ss1", 0,-1); // The System is sorted by weight. out. println (jedis. zscore ("ss1", "Tom"); // obtain the sorting weight of Tom}

3.Redis applications

Based on the above introduction to the Redis data structure, Redis can be used for caching and data persistence in the server development technology. The following aspects are the understanding of cainiao.

1. Message Queue

In the second part of this article, we introduced that Redis has a two-way linked List and supports dual-in and dual-out. Therefore, Redis can be used as a message queue, for example, notification messages, tasks, and emails can be stored in the message queue, and data in the queue can be consumed in most languages supported by Redis at the other end, this facilitates decoupling between system modules, reduces dependencies between modules, and converts synchronous Message Processing Methods to asynchronous processing without blocking queues. The Weibo user's Weibo list should be cached by Redis.

 

2. List the latest data entries in a list

This can also be handled using the List in Redis. For example, if a Weibo user has millions of comments, the page will certainly display only the latest dozens of comments. If there is a database, querying data using Mysql's SELECT limit will sort all the data, which is very costly. However, if you use Redis's List, you can use lrange ("key", 0,100) method to retrieve the last 100 comments.

 

3. counters

Because Weibo has the largest Redis cluster, we still use Weibo as an example. A star posted a microblog with the topic "we" and liked millions of users within two days. In this case, we can use the atomic addition and subtraction operations in Redis. in java, we know that I ++ or I -- is not atomic, which means it is not thread-safe, however, this problem does not occur when using Redis. You can use jedis. hincrBy ("weiboid", "likes", 1); this indicates that the number of likes on a microblog is increased by 1, which is thread-safe here.

 

4. ranking, top n data

Based on the Sorted Set introduced in the second part of this article, we can retrieve top n data, such as 100 million users participating in the competition, or million Weibo users. We want to retrieve the highest score, or the most popular 100 Weibo posts, where the game scores or Weibo popularity can all be weights (scores). You can perform operations in the cache without sorting them.

 

5. Arrange the sum of duplicates and the sum, difference, and Intersection

Redis uses Set, which has been introduced in the second part of the Java program. To remove duplicates, you only need to insert data into the set Set. Redis will automatically perform deduplication, in addition, the Union, intersection, and difference sets of the set in any two caches can be obtained.

 

6. Pub/Sub

Redis supports message publishing-subscription mode, which is very suitable for pushing chat messages.

 

Learning materials and references:

Http://try.redis.io/Redis beginners need to learn the basic commands, provides the command line interface.

Http://redis.io/topics/introduction Redis Basic Introduction

Application of http://www.csdn.net/article/2013-10-07/2817107-three-giant-share-redis-experience/1 Redis at Home and Abroad

Http://www.cnblogs.com/whoamme/p/3532129.html Redis basic operations

Http://www.360doc.com/content/15/0510/20/23016082_469494498.shtml Redis Application

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.