拼团活动开发秘籍:PHP+Redis实现暂存成团信息,提升效率!
在用户发起成团,与用户入团时需要保存其成团信息(主要是活动id与团长、团员openid),暂存在redis中,后期需要保存到sql中,以便查询。
tuan_redis.php<?php
include_once('/opt/*****ub/redis.php');//redis配置文件
$redis = new RedisClient;
$redis->connect(array('host'=>'127.0.0.1','port'=>6380));
$redis->selectDB(3);
function judgeTuan($openid,$act_id){global $redis;if($redis->get($act_id.$openid)){ //判断是否已开过团$tuan_yuan = $redis->get($act_id.$openid);$people_arr = explode(',',$tuan_yuan);return $people_arr[0]; //返回之前保存的团长openid(第一个)}else{return false; //返回false,之前未有过}
}function startTuan($openid,$act_id,$mediaID){ //保存成团信息global $redis;//$redis->set($act_id.$openid,$mediaID,86400*10);$redis->set($act_id.$openid,$mediaID,3600*5); //测试保存5个小时(已$act_id.$openid为键名)return $mediaID;
}function joinTuan($p_openid,$act_id,$openid){ //保存参团人员信息global $redis;if($p_openid == $openid){return 4; //扫自己本人发出的二维码无效}$tuan_yuan = $redis->get($act_id.$p_openid);$people_arr = explode(',',$tuan_yuan);$peo_num = count($people_arr);if($peo_num <= 9){ //八人团,加第一个图片地址,为9(第一个为团长信息)if(strpos($tuan_yuan,$openid) === false){$tuan_yuan .= ','.$openid;//$redis->set($act_id.$p_openid,$tuan_yuan,86400*10);$redis->set($act_id.$p_openid,$tuan_yuan,3600*5); //测试保存5个小时addTime($p_openid,$act_id,3600*5); //保存对应的入团时间return 1; //进团成功}else{return 3; //已进过此团}}else{return 2; //此团已满}
}
function addTime($openid,$act_id,$time){global $redis;$now_time = date('Y-m-d H:i:s');$join_time = $redis->get($act_id.$openid.'time');if($join_time){$join_time .= ','.$now_time;$redis->set($act_id.$openid.'time',$join_time,$time);}else{$redis->set($act_id.$openid.'time',$now_time,$time);} }