mysql bitmap redis_PHP使用redis位图bitMap 实现签到功能

一、需求

记录用户签到,查询用户签到

二、技术方案

1、使用mysql(max_time字段为连续签到天数)

c0c2f1dac390183f91cebcef940aa064.png

思路:

(1)用户签到,插入一条记录,根据create_time查询昨日是否签到,有签到则max_time在原基础+1,否则,max_time=0

(2)检测签到,根据user_id、create_time查询记录是否存在,不存在则表示未签到

2、使用redis位图功能

思路:

(1)每个用户每个月单独一条redis记录,如00101010101010,从左往右代表01-31天(每月有几天,就到几天)

(2)每月8号凌晨,统一将redis的记录,搬至mysql,记录如图

c00a19aac3e00ec88ab96101299b084d.png

(3)查询当月,从redis查,上月则从mysql获取

3、方案对比

举例:一万个用户签到365天

方案1、mysql 插入365万条记录

· 频繁请求数据库做一些日志记录浪费服务器开销。

·  随着时间推移数据急剧增大

· 海量数据检索效率也不高,同时只能用时间create_time作为区间查询条件,数据量大肯定慢

方案2、mysql 插入12w条记录

· 节省空间,每个用户每天只占用1bit空间 1w个用户每天产生10000bit=1050byte 大概为1kb的数据

· 内存操作存度快

3、实现(方案2)

(1)key结构

前缀_年份_月份:用户id -- sign_2019_10:01

查询:

单个:keys sign_2019_10_01

全部:keys sign_*

月份:keys sign_2019_10:*

(2)mysql表结构

d5040748ccd10c834d486421bc39364b.png

(3)代码(列出1个调用方法,与三个类)

·签到方法

public static function userSignIn($userId)

{

$time = Time();

$today = date('d', $time);

$year = date('Y', $time);

$month = date('m', $time);

$signModel = new Sign($userId,$year,$month);

//1、查询用户今日签到信息

$todaySign = $signModel->getSignLog($today);

if ($todaySign) {

return self::jsonArr(-1, '您已经签到过了', []);

}

try {

Db::startTrans();

$signModel->setSignLog($today);

//4、赠送积分

if (self::SING_IN_SCORE > 0) {

$dataScore['order_id'] = $userId.'_'.$today;

$dataScore['type'] = 2;//2、签到

$dataScore['remark'] = '签到获得积分';

Finance::updateUserScore(Finance::OPT_ADD, $userId, self::SING_IN_SCORE, $dataScore);

}

$code = '0';

$msg = '签到成功';

$score = self::SING_IN_SCORE;

Db::commit();

} catch (\Exception $e) {

Db::rollback();

$code = '-2';

$msg = '签到失败';

$score = 0;

}

return self::jsonArr($code, $msg, ['score' => $score]);

}

·redis基类

namespace app\common\redis\db1;

/**

* redis操作类

*/

class RedisAbstract

{

/**

* 连接的库

* @var int

*/

protected $_db = 1;//数据库名

protected $_tableName = '';//表名

static $redis = null;

public function __construct()

{

return $this->getRedis();

}

public function _calcKey($id)

{

return $this->_tableName . $id;

}

/**

* 查找key

* @param $key

* @return array

* @throws \Exception

* @author wenzhen-chen

*/

public function keys($key)

{

return $this->getRedis()->keys($this->_calcKey($key));

}

/**

* 获取是否开启缓存的设置参数

*

* @return boolean

*/

public function _getEnable()

{

$conf = Config('redis');

return $conf['enable'];

}

/**

* 获取redis连接

*

* @staticvar null $redis

* @return \Redis

* @throws \Exception

*/

public function getRedis()

{

if (!self::$redis) {

$conf = Config('redis');

if (!$conf) {

throw new \Exception('redis连接必须设置');

}

self::$redis = new \Redis();

self::$redis->connect($conf['host'], $conf['port']);

self::$redis->select($this->_db);

}

return self::$redis;

}

/**

* 设置位图

* @param $key

* @param $offset

* @param $value

* @param int $time

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function setBit($key, $offset, $value, $time = 0)

{

if (!$this->_getEnable()) {

return null;

}

$result = $this->getRedis()->setBit($key, $offset, $value);

if ($time) {

$this->getRedis()->expire($key, $time);

}

return $result;

}

/**

* 获取位图

* @param $key

* @param $offset

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function getBit($key, $offset)

{

if (!$this->_getEnable()) {

return null;

}

return $this->getRedis()->getBit($key, $offset);

}

/**

* 统计位图

* @param $key

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function bitCount($key)

{

if (!$this->_getEnable()) {

return null;

}

return $this->getRedis()->bitCount($key);

}

/**

* 位图操作

* @param $operation

* @param $retKey

* @param mixed ...$key

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function bitOp($operation, $retKey, ...$key)

{

if (!$this->_getEnable()) {

return null;

}

return $this->getRedis()->bitOp($operation, $retKey, $key);

}

/**

* 计算在某段位图中 1或0第一次出现的位置

* @param $key

* @param $bit 1/0

* @param $start

* @param null $end

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function bitPos($key, $bit, $start, $end = null)

{

if (!$this->_getEnable()) {

return null;

}

return $this->getRedis()->bitpos($key, $bit, $start, $end);

}

/**

* 删除数据

* @param $key

* @return int|null

* @throws \Exception

* @author wenzhen-chen

*/

public function del($key)

{

if (!$this->_getEnable()) {

return null;

}

return $this->getRedis()->del($key);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值