用redis实现用户登录计数
来源:互联网 发布:清除的数据怎么恢复 编辑:程序博客网 时间:2024/06/06 07:09
最近有个问题 :实现显示用户本月登录日期 ,累计天数到一定数目可参加活动,和下图展示的功能类似。
过去看过使用redis bitmap进行活跃用户统计的例子,觉得和本功能非常match,决定用redis的bitset来实现。
每个用户每月产生一个key,字段意义如下 0 位表示某月1日登录 ,1表示某月2日登录,以此类推。
每个用户一月的活跃度需要8byte,1W用户每年需要1W*12*8byte 约1M数据
用户每次登录系统,需要用setBit设置用户本日登录,可用bitcount的到用户本月登录天数总和,因为PHP原生不对bitset进行支持,采用get得到字符串进行操作得到用户登录日期
测试代码如下:
- <?php
- /**
- *
- * User: shikiliu
- * Date: 14-8-27
- */
- class ActiveDate
- {
- private $redisConf = array('host' => 'localhost', 'port' => 6379);
- private $redis = null;
- private $userPrefix = 'user_active_';
- /**
- * 设置用户某天登录过
- */
- public function setActiveDate($userId, $time = null)
- {
- if (empty($time)) {
- $time = time();
- }
- $redis = $this->getRedis();
- $redis->setBit($this->userPrefix . $userId . '_' . date('Y-m', $time), intval(date('d', $time)) - 1, 1);
- return true;
- }
- /**
- * 得到用户本月登录天数
- * redis >= 2.6.0 才可以
- */
- public function getActiveDatesCount($userId, $time = null){
- if (empty($time)) {
- $time = time();
- }
- $redis = $this->getRedis();
- return $redis->bitcount($this->userPrefix . $userId . '_' . date('Y-m', $time));
- }
- /**
- * 得到用户某月所有的登录过日期
- */
- public function getActiveDates($userId, $time = null)
- {
- $result = array();
- if (empty($time)) {
- $time = time();
- }
- $redis = $this->getRedis();
- $strData = $redis->get($this->userPrefix . $userId . '_' . date('Y-m', $time));
- if (empty($strData)) {
- return $result;
- }
- $monthFirstDay = mktime(0, 0, 0, date("m", $time), 1, date("Y", $time));
- $maxDay = cal_days_in_month(CAL_GREGORIAN, date("m", $time), date("Y", $time));
- $charData = unpack("C*", $strData);
- for ($index = 1; $index <= count($charData); $index++) {
- for ($bit = 0; $bit < 8; $bit++) {
- if ($charData[$index] & 1 << $bit) {
- //$intervalDay = ($index - 1) * 8 + 8-$bit;
- $intervalDay = $index * 8 -$bit;
- //如果数据有大于当月最大天数的时候
- if ($intervalDay > $maxDay) {
- return $result;
- }
- $result [] = date('Y-m-d', $monthFirstDay + ($intervalDay-1) * 86400);
- }
- }
- }
- return $result;
- }
- /**
- * redis连接
- */
- private function getRedis()
- {
- if (empty($this->redis)) {
- $redis = new Redis();
- if (!$redis->connect($this->redisConf['host'], $this->redisConf['port'])) {
- throw new Exception("Error Redis Connect", 100);
- }
- $redis->select(3);
- $this->redis = $redis;
- }
- return $this->redis;
- }
- }
- $activeDate = new ActiveDate();
- var_dump($activeDate->setActiveDate(514540767,1406822600));// 2014/8/1 0:3:20
- var_dump($activeDate->setActiveDate(514540767,1407225600));// 2014/8/5 16:0:0
- var_dump($activeDate->setActiveDate(514540767,1409472000));// 2014/8/31 16:0:0
- var_dump($activeDate->getActiveDates(514540767));
- var_dump($activeDate->getActiveDatesCount(514540767));
阅读全文
0 0
- 用redis实现用户登录计数
- 用redis实现用户登录计数
- redis实现网站最新登录用户功能
- Redis用户登录场景
- Redis缓存Mysql模拟用户登录Java实现实例
- Redis缓存Mysql模拟用户登录Java实现实例
- Redis自增实现计数
- node.js + redis 在线用户计数
- 利用Redis实现亿级别用户登录统计(活跃度以及登录次数统计)
- 利用Redis实现亿级别用户登录统计(活跃度以及登录次数统计)
- Django+redis 实现登录
- Redis实现单点登录
- 面试题:用Redis和任意语言实现一段恶意登录保护的代码,限制1小时内每用户Id最多只能登录5次
- 面试题:用Redis和任意语言实现一段恶意登录保护的代码,限制1小时内每用户Id最多只能登录5次
- 用模式一实现用户登录功能
- 【mfc】用对话框分页实现用户登录
- 树莓派实现用pi用户自动登录
- 用xPath技术来实现用户登录
- 人工智能笔记(一)
- Raft分布式一致性协议
- php中单例模式不提示解决
- 了解项目流程
- Python的列表介绍和冒泡,希尔排序算法例子
- 用redis实现用户登录计数
- HDFS常用命令参数介绍
- android ClipDrawable实现进度条
- 一例需要排除框架中的依赖.
- java中静态初始化块、初始化块和构造方法的理解 (转载)
- 3自定义View系列课程学习笔记——之Measure测量
- Java--常用源码--排序算法
- 文件写入、读取的指针问题
- Windows 环境下使用 GCC