Application of Redis set: an example of the implementation of a single courteous function

Source: Internet
Author: User
Tags data structures redis

For the majority of the parents can be good chop hands, we are also spelled, "Double 11" once, we began to prepare "double 12", the big promotion activity has many, this article takes "the next single has the propriety" the function realization, talks about Redis several uses, like Redis collection, the concrete application which increases. Here just a small example, in a large web site multiple terminals, to consider a variety of situations and scenarios.


Instructions for the next single courteous request:

Activity time: December 11 10:00 ~ December 14 09:59

Activity content:

1, during the activity, the user buys the commodity, the first order amount is full 68 yuan and above, may obtain a 68 Yuan Coupon gift Package (each user can attend only 1 times)

2, coupons gift package issued to distinguish between new and old customers

New 68 Yuan Gift package contains coupons: 30 minus 5, 129 minus 15, full 188 minus 20, full 269 minus 28

Old Guest 68 Yuan Gift package contains coupons: 99 minus 5, 179 minus 15, full 269 minus 20, full 359 minus 28

3, the activity only in the app, micro-letter, M station do not participate in the event

Requirements Flowchart:

A small demand, there are still a lot of questions to consider:

The Gift package Condition: The first Order amount >=68 yuan, during the activity a user can only receive once the gift package
Judgment for vest, suspected vest users do not issue gift packs
Coupon package issued to distinguish between new and old customers (the corresponding voucher package ID is different), the new customer refers to the registration of the purchase of the user
Mobile End Advertising Picture Display configuration:

The user satisfies the activity condition, and presents the gift package to be successful, displays "receives the gift bag" the picture
Vest, suspected vest users, show "bag has been robbed" picture
The gift package is all led to the light, showing "the bag has been robbed" picture
The user who does not receive the gift package displays the active picture
Click on the pictures to share the need to bury the dots. Record the successful receipt of the gift package user UID, voucher package ID, order amount, time of receipt and other information.

Basic ideas:

From the app side (Ios,android) a successful and real payment of more than 68 yuan users, deposit Redis hash and the number of times.
To determine whether the user meets the gift package conditions, and to determine whether the new and old users, new users to send new users gift packs, old users to send old user gift package, and the user ID to the different Redis set.
Provided to the mobile end of the interface, pass in the UID parameter, return the following user status: ① New user successfully issued a gift package ② old users successfully issued a gift package ③ vest, suspected vest ④ package has been issued 5, other (not meet the gift package conditions of the next single successful users)
Web Site A large number of use of redis, in order to solve the high concurrent performance pressure, you can do data caching, and has a wealth of data structures for different scenarios to choose from, this function to use the hash, the collection of data agencies, as well as the functions of the function features.

To implement this requirement, create the following Redis key:

com.xx.logic.ordergift_151212
Because to judge "first", so need to use the Redis count, with the hash structure, key for the user Id,value to meet the condition times. The data structure is as shown in the figure:

Com.xx.ordergift_151212_newuser
This is a collection that holds the new user ID for the first time satisfying the condition and distributing the newcomer's gift package.

Com.xx.ordergift_151212_olduser
This is a collection that holds the user ID for the first time satisfying the condition and distributing the old customer gift package.

Com.xx.ordergift_151212_fail
This is a collection of gift packs that are robbed, or a gift packet interface exception to the user ID.

These collections are primarily intended to provide the mobile end of the API interface to return to the next successful user status, the mobile end needs to be in the mobile phone according to different user status show different effects and other processing.

(Note: The following code section uses pseudo code, including Redis key, and method naming calls are modified)

Provides methods to invoke the mobile-side API interface:


/**
* Judge the order complete user status
* Added by Tuntun 2015.11.20
* @param int $uid User ID
* @return int 1. The new user successfully issued the gift Package 2. The old user successfully issued the Gift bag 3. Vest, suspected vest 4, gift package has been issued 5, other (not meet the gift package conditions of the next single successful users)
*/
Public Function Couponuserstatus ($uid) {
$is _majia = logic (' Majia ')->ismajia ($UID);
if (In_array ($is _majia,array (1,3)) return 3;

$is _new = $this->couponuserisnew ($uid, true);
if ($is _new) return 1;

$is _old = $this->couponuserisnew ($uid, false);
if ($is _old) return 2;

$is _fail = $this->_redis->sismember ($this->_payalertkeys[' Order_coupon151212_fail '), $uid);
if ($is _fail) return 4;

return 5;
}

The following code is the next single payment successful callback in the interface to invoke the double 12 activity under the single courteous method, to meet the conditions of the user Redis count, to judge the new and old users to distribute different gift packages, coexist to the corresponding collection.


/**
* Double 12 promotion activities during the app end of a single initial payment full of 68 yuan to send a gift package
* To meet the conditions of the order of the orders successfully, with the Redis count. Judge to meet the conditions of new and old users to send the corresponding gift bag, vest not hair.
* Added by Tuntun 2015.11.20
* @param int $uid User ID
* @param int $order _no order number
* @param float $real _payment Real Payment
* @param int $pass _id payment end source only allow Alipay app, micro-letter app, UnionPay app, micro-trust buddy to pay
* @return BOOL
*/
Public Function coupon151212 ($uid, $order _no, $real _payment, $pass _id) {
if (! $uid | |! $order _NO | | $real _payment < | |!in_array ($pass _id, Array (5,6,7,10,11))) {
Logger::info (' Invalid params! '). Json_encode (Func_get_args ()), ' Coupon1212 ');
return false;
}

App side Alipay Go is the web version, this kind of situation also count, check order form confirmation source
if ($pass _id = = 5) {
$pay _form = logic (' pay ')->getpayfrom ($order _no);
Judge whether it comes from xx android,xx iphone,xx android,xx iphone
if (!in_array ($pay _form, Array (3,4,6,7)) {
Logger::info (' Alipay don't from App. Payfrom: '. $pay _form, ' Coupon1212 ');
return false;
}
}

Determine whether the active time is valid
if ($this->checkactivitystatus (' coupon151212 ')) {
68 $ Redis count for real payments
$key = $this->_payalertkeys[' coupon151212 '];
$this->_redis->hincrby ($key, $uid, 1);
$this->_redis->expire ($key, $this->_pay_key_expire);
}else{
Logger::info (' Activity time are over. ', ' Coupon1212 ');
return false;
}

$hasCoupon = $this->hascoupongift ($uid), whether or not to meet the gift package gift eligibility, to determine whether the number of eligible 68 yuan is 1
$coupon _fail= false;//Voucher failure identification
if ($hasCoupon) {
$user _type = User->checkusertype ($uid); 1 new guest 2 old guest
if (1 = = $user _type) {
$res = logic (' Coupon ')->recivecouponbag (Self::new_user_coupon_bagid, $uid);
if ($res [' code '] = = 1000) {
Logger::info (' Send newer coupon success. Uid: '. $uid, ' Coupon1212 ');
$this->_redis->sadd ($this->_payalertkeys[' order_coupon_new '), $uid);
$this->_redis->expire ($this->_payalertkeys[' order_coupon_new '), $this->_pay_key_expire);
}else{
Logger::info (' Send new coupon fail. Uid: '. $uid, ' Coupon1212 ');
$coupon _fail = true;
}
}elseif (2 = = $user _type) {
$res = logic (' Coupon ')->recivecouponbag (Self::old_user_coupon_bagid, $uid);
if ($res [' code '] = = 1000) {
Logger::info (' Send old Coupon success. Uid: '. $uid, ' Coupon1212 ');
$this->_redis->sadd ($this->_payalertkeys[' Order_coupon_old '), $uid);
$this->_redis->expire ($this->_payalertkeys[' Order_coupon_old '), $this->_pay_key_expire);
}else{
Logger::info (' Send old coupon fail. Uid: '. $uid, ' Coupon1212 ');
$coupon _fail = true;
}
}

Voucher failure put in collection
if ($coupon _fail = = True) {
$this->_redis->sadd ($this->_payalertkeys[' Order_coupon_fail '), $uid);
$this->_redis->expire ($this->_payalertkeys[' Order_coupon_fail '), $this->_pay_key_expire);
}
}else{
Logger::info (' Can not get coupon gift. Uid: '. $uid);
Remove the UID from the collection of new and old users who can issue a gift package
$this->_redis->srem ($this->_payalertkeys[' order_coupon_new '), $uid);
$this->_redis->srem ($this->_payalertkeys[' Order_coupon_old '), $uid);
}

return true;
}

To determine whether there is a gift package eligibility, is to determine the value of the Redis hash is 1:


/**
* Double 12 activities to determine whether the user has a gift package eligibility
* @param int $uid User ID
* @return BOOL
*/
Private Function Hascoupongift ($uid) {
$key = $this->_payalertkeys[' order_coupon '];
$exists = $this->_redis->exists ($key);
if ($exists) {
$value = $this->_redis->hget ($key, $uid);
return $value = = 1;
}

return false;
}
The use of several Redis

The following are the Redis methods used by this feature:

Hincrby (or decrement) the value of a key for a hash structure data (provided the value is an integral type).

For example: Hincrby key Test 1, create automatically if key does not exist. The INCR of a string-type number can be in another way: a.

Hget, gets a value for the key of a hash result.

As in the example above, you can get 1 with the Hget key test by adding a value of the key to test.

Sadd, put a value in a set.

such as Sadd collection 3,4,5,6,7, put these values into the collection set, the Sadd collection is not duplicated.

Sismember to determine whether a value belongs to a collection.

If you put a few numbers above to the set collection, use Sismember collection 3, return 1, to indicate that 3 exists in the collection.

The role of the set is very powerful, Redis can be convenient for the collection and set, the difference set, intersection, such as micro-bo common friends and other functions to achieve.

Srem, removes a value from a collection.

If Srem Collection 3, remove 3 from the collection.

exists, to determine whether there is a key.

Expire, set key expiration time.

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.