The practical application of Redis in e-commerce-java

Source: Internet
Author: User
Tags memcached

The sample code is written in Jedis. 1. Various counts, commodity dimension count and user dimension count

Speaking of e-commerce, must be inseparable from the goods, and the goods with a variety of counts (like number, number of comments, identification number, number of views, etc), Redis commands are atomic, you can easily use INCR,DECR and other commands to count.

    • Commodity dimension count (likes number, number of comments, number of appraisals, number of views, etc)

type of Redis: Hash . If you are not familiar with Redis data types, you can refer to Http://redis.io/topics/data-types-intro. Define a key for product product: , define HashKey for each value, such as the number of likeslike

   Jedis.hset ("Product:1", "Like", "5");   Jedis.hincrby ("Product:1", "like", 1); // like to add a   System.out.print (Jedis.hget ("Product:1", "like");
    • User dimension count (dynamic number, number of followers, number of fans, number of likes, number of posts, etc.)

The user dimension count is used with the commodity dimension Count Hash . Defines a key for the user user: , defines hashkey for each value, such as the number of followersfollow

   Jedis.hset ("User:1", "Follow", "5");   Jedis.hincrby ("User:1", "Follow", 1); // focus on the number plus one   System.out.print (Jedis.hget ("User:1", "Follow"));
2. Store Social relationships

For example, the user's friends/fans/concerns, can exist sorted set in a, score can be timestamp, so that the two people of the common friends of the operation, you may only need to use the intersection command.

 Jedis.zadd ("User:1000:friends", System.currenttimemillis (), "user:1001"  // uid for 1000 Users and UID for 1001 is friend, score Value set timestamp  jedis.zadd ("User:1000:friends", System.currenttimemillis (), "user:1002"                             // uid for 1000 users and UID for 1002 is friend, score value set timestamp   Jedis.zadd ( "User:2000:friends", System.currenttimemillis (), "user:10   ");   Jedis.zadd ( "User:2000:friends", System.currenttimemillis (), "user:1003"  "com_friends:1000:2000", "User:1000:friends", "User:2000:friends"    "com_friends:1000:2000", 0,-1)); 
3. Use as cache instead of memcached (item list, comment list, @ hint list, etc)

compared to memcached simple key-value storage, Redis's numerous data structures (list,set,sorted Set,hash, etc) make it easier to cache various business data, as well as memcached performance.

4. Anti-spam system (comments, release items, forum posts, etc)

As an e-commerce site by a variety of spam attacks is less unavoidable (spam reviews, release junk goods, ads, brush their own product rankings, etc.), for these spam to develop a series of anti-spam rules, some of which can use Redis to do real time

Analysis, such as: 1-minute reviews must not exceed 2 times, 5-minute reviews less than 5 times (more mechanisms/rules need to be combined with drools).

Take sorted set the last day of user action record (why not all records?). Save memory, all operations will be recorded to log, subsequent use of Hadoop for more comprehensive analysis of statistics)

    if(!jedis.exists ("User:1000:comment")) {//No Comments yetJedis.zadd ("User:1000:comment", System.currenttimemillis (), "UID 1000 Reviews"); System.out.println (Jedis.zrange ("User:1000:comment", 0,-1)); }Else{        //get a record of comments in a minuteset<string> result = Jedis.zrangebyscore ("User:1000:comment", System.currenttimemillis ()-60 * 1000, System.currenttimemillis ()); if(!Result.isempty ()) {System.out.println ("cannot be commented two times in 1 minutes"); }Else{Jedis.zadd ("User:1000:comment", System.currenttimemillis (), "UID 1000 reviews" +System.currenttimemillis ()); System.out.println (Jedis.zrange ("User:1000:comment", 0,-1)); }    }
5. User Timeline/feeds

For people, themes, brands, and columns,Redis is primarily used as a cache here.

    Jedis.zadd ("User:2000:feed:topic", System.currenttimemillis (), "13topic" + System.currenttimemillis ());     // score for timestamp UID 2000 focus on TID as 13 topic    Jedis.expire ("User:2000:feed:topic", ten);     // concern is valid for 10 seconds    System.out.println (Jedis.zrange ("User:2000:feed:topic", 0,-1));    
6. Latest Listings & Leaderboards

Used to record the latest list of items the user has just liked. Business scenarios such as or leaderboard

Jedis.zadd ("User:1000:product:like", System.currenttimemillis (), "003");    Thread.CurrentThread (); Thread.Sleep (10); Jedis.zadd ("User:1000:product:like", System.currenttimemillis (), "001"); Thread.Sleep (10); Jedis.zadd ("User:1000:product:like", System.currenttimemillis (), "004"); Thread.Sleep (10); Jedis.zadd ("User:1000:product:like", System.currenttimemillis (), "002"); Thread.Sleep (10); //default preferred time in ascending orderset<string> result = Jedis.zrange ("User:1000:product:like", 0,-1);        SYSTEM.OUT.PRINTLN (result); //Sort by like time descendingresult = Jedis.zrevrange ("User:1000:product:like", 0,-1); SYSTEM.OUT.PRINTLN (result);
[003, 001, 004, 002] [002, 004, 001, 003]
7. Message Notification

Use hash structure to count message notification business scenarios

 //  Set 1 unread system messages  Jedis.hset ("user:1000    : Message:num "," Sysmessagenum "," 1 " "User:1000:message:content", "First unread message"  //  unread system message +1  Jedis.hincrby ("User    : 1000:message:num "," Sysmessagenum ", 1 "User:1000:message:content", "second unread information"  //  View the number of all message notifications  SYSTEM.OUT.PRINTLN (    Jedis.hgetall ("User:1000:message:num"  //  View all message notification contents  SYSTEM.OUT.PRINTLN ( Jedis.smembers ("User:1000:message:content")); 
{sysmessagenum=2}[second unread information, first unread message]

The practical application of Redis in e-commerce-java

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.