redis-seconds Kill Scene Application

Source: Internet
Author: User

Redis Util implementation

Package Test.jedis;import Java.util.list;import Java.util.set;import redis.clients.jedis.jedis;import Redis.clients.jedis.transaction;public class Jedisutil {public static Jedis Redis = new Jedis ("localhost", 6379);//Connect Red isstatic{//initialization of shopping data Redis.set ("Buy:number:1", "10000");} /** * The process of executing a transaction other clients change the key value, resolve the data consistency problem * through watch key monitoring to implement other client modification data, transaction cancellation * Usage first use watch to start the monitoring of key in the open transaction, the order must first monitor in the execution of the transaction * /public synchronized static string Buy_trans (String userId) throws Interruptedexception{//1, the keys are designed to guarantee that each user can purchase only one if ( !redis.exists (userId)) {///2, when the number is not purchased prompt seconds to kill if (Integer.parseint (Redis.get ("buy:number:1")) >0) {//3, REDIS transaction monitoring number of        Change key=buy:number:1 monitor number of goods 1th change Redis.watch ("buy:number:1");         4. Open transaction Transaction tx = Redis.multi ();        5. Successful purchase of TX.INCR (USERID) successfully purchased by the user;        6, set the number minus 1 tx.decr ("buy:number:1");        7, the execution transaction list<object> results = Tx.exec ();    }else{return "Insufficient quantity";     } redis.disconnect (); Return "snapping up purchase Success";} Else{retUrn "The user has already purchased";}} public static set<string> Getkeys (String keys) {return redis.keys (keys);}}

Controller-Side implementation

package Com.sf.fs.view;import Java.util.set;import Javax.servlet.http.httpservletrequest;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import com.sf.fs.service.seckillservice;/** * Seckill Test * */@Controllerpublic class Seckillcontroller {private Seckillservice seckillservice;public void Setseckillservice ( Seckillservice seckillservice) {this.seckillservice = Seckillservice;} /** * * */@RequestMapping ("/buying") public String Buying (httpservletrequest req) {//test for easy testing with SessionID as user ID String sessionid=req.getsession (). GetId ();//The stored key is user:sessionidstring msg=seckillservice.buying ("User:" + SESSIONID); SYSTEM.OUT.PRINTLN ("Returned operation result:" +msg); return "Succeed";} /** * Test the number of users successfully snapped, whether there will be oversold * */@RequestMapping ("/result") public void GetResult (HttpServletRequest req) {set<string > sets=seckillservice.getusers ("user:*"); System.out.println ("Number of users successfully killed:" +sets.size ());}} 

Service implementation

Package Com.sf.fs.service.impl;import Java.util.list;import Java.util.set;import com.sf.fs.service.SeckillService; Import Test.jedis.jedisutil;public class Seckillserviceimpl implements Seckillservice {public static int number=100; Private Jedisutil jedisutil;public void Setjedisutil (Jedisutil jedisutil) {this.jedisutil = Jedisutil;} @Overridepublic  String Buying (string key) {try {return Jedisutil.buy_trans (key)} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();} Return "operation failed";} Public set<string> getusers (String prefixkey) {return Jedisutil.getkeys (Prefixkey);}}

  

redis-seconds Kill Scene Application

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.