/** * 执行签到 * @param string $act 活动token * @param string $user openId * @return int 签到排名,失败返回false */ public static function checkin($act, $user, $nickname) { $item = array('openid' => $user, 'time' => date("Y-m-d H:i:s", time()), 'nickname' => $nickname); $cache = Vera_Cache::getInstance(); $key = 'rollcall_' . $act . '_list'; $rank = false; do { $list = $cache->get($key, NULL, $cas); //使用Memcached特性cas,保证高并发时的准确性 if ($cache->getResultCode() == Memcached::RES_NOTFOUND) { if ($list = Library_File::load($act)) { if (!($rank = self::getRank($list, $user))) { $list[] = $item; $rank = count($list); } $newList = $list; } else { $newList = array($item); //两层Array $rank = 1; } $cache->add($key, $newList, 86400); //原子性的插入,签到记录的缓存保存二十四小时 } else { if (!($rank = self::getRank($list, $user))) { $list[] = $item; $cache->cas($cas, $key, $list); $rank = count($list); } } } while ($cache->getResultCode() != Memcached::RES_SUCCESS); return $rank; }
public function run() { $acts = self::getActs(); $cache = Vera_Cache::getInstance(); foreach ($acts as $act) { $key = 'rollcall_' . $act['md5'] . '_list'; if ($list = $cache->get($key)) { Library_File::write($act['md5'], $list); } } return true; }
/** * 获取签到结果 * @param string $act 活动token * @return array 签到结果数组 */ public static function getCheckinList($act) { $cache = Vera_Cache::getInstance(); $key = 'rollcall_' . $act . '_list'; $list = $cache->get($key); if ($cache->getResultCode() == Memcached::RES_NOTFOUND) { if (!($list = Library_File::load($act))) { return false; } //无需set载入的结果,有人checkin的时候自然会set } return $list; }