日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

redis 用戶登錄次數(shù)

 丶平上 2016-12-14
  1. <?php  
  2.   
  3. /** 
  4.  * 
  5.  * User: shikiliu 
  6.  * Date: 14-8-27 
  7.  */  
  8.   
  9. class ActiveDate  
  10. {  
  11.   
  12.   
  13.     private $redisConf = array('host' => 'localhost''port' => 6379);  
  14.   
  15.     private $redis = null;  
  16.   
  17.     private $userPrefix = 'user_active_';  
  18.   
  19.   
  20.     /** 
  21.      * 設(shè)置用戶某天登錄過 
  22.      */  
  23.     public function setActiveDate($userId$time = null)  
  24.     {  
  25.   
  26.         if (emptyempty($time)) {  
  27.   
  28.             $time = time();  
  29.         }  
  30.   
  31.         $redis = $this->getRedis();  
  32.   
  33.         $redis->setBit($this->userPrefix . $userId . '_' . date('Y-m'$time), intval(date('d'$time)) - 1, 1);  
  34.   
  35.         return true;  
  36.     }  
  37.   
  38.   
  39.     /** 
  40.      * 得到用戶本月登錄天數(shù) 
  41.      * redis >= 2.6.0 才可以 
  42.      */  
  43.     public function getActiveDatesCount($userId$time = null){  
  44.   
  45.         if (emptyempty($time)) {  
  46.   
  47.             $time = time();  
  48.         }  
  49.   
  50.         $redis = $this->getRedis();  
  51.   
  52.         return $redis->bitcount($this->userPrefix . $userId . '_' . date('Y-m'$time));  
  53.   
  54.     }  
  55.   
  56.   
  57.     /** 
  58.      * 得到用戶某月所有的登錄過日期 
  59.      */  
  60.     public function getActiveDates($userId$time = null)  
  61.     {  
  62.   
  63.   
  64.         $result = array();  
  65.   
  66.         if (emptyempty($time)) {  
  67.   
  68.             $time = time();  
  69.         }  
  70.   
  71.         $redis = $this->getRedis();  
  72.   
  73.         $strData = $redis->get($this->userPrefix . $userId . '_' . date('Y-m'$time));  
  74.   
  75.         if (emptyempty($strData)) {  
  76.             return $result;  
  77.         }  
  78.   
  79.         $monthFirstDay = mktime(0, 0, 0, date("m"$time), 1, date("Y"$time));  
  80.   
  81.         $maxDay = cal_days_in_month(CAL_GREGORIAN, date("m"$time), date("Y"$time));  
  82.   
  83.         $charData = unpack("C*"$strData);  
  84.   
  85.         for ($index = 1; $index <= count($charData); $index++) {  
  86.   
  87.             for ($bit = 0; $bit < 8; $bit++) {  
  88.   
  89.                 if ($charData[$index] & 1 << $bit) {  
  90.   
  91.                     //$intervalDay = ($index - 1) * 8 + 8-$bit;  
  92.                     $intervalDay = $index  * 8 -$bit;  
  93.                     //如果數(shù)據(jù)有大于當(dāng)月最大天數(shù)的時(shí)候  
  94.                     if ($intervalDay > $maxDay) {  
  95.   
  96.                         return $result;  
  97.   
  98.                     }  
  99.   
  100.                     $result [] = date('Y-m-d'$monthFirstDay + ($intervalDay-1) * 86400);  
  101.   
  102.                 }  
  103.   
  104.             }  
  105.   
  106.         }  
  107.   
  108.         return $result;  
  109.   
  110.   
  111.     }  
  112.   
  113.   
  114.     /** 
  115.      *  redis連接 
  116.      */  
  117.     private function getRedis()  
  118.     {  
  119.   
  120.         if (emptyempty($this->redis)) {  
  121.   
  122.   
  123.             $redis = new Redis();  
  124.   
  125.             if (!$redis->connect($this->redisConf['host'], $this->redisConf['port'])) {  
  126.   
  127.                 throw new Exception("Error Redis Connect", 100);  
  128.   
  129.             }  
  130.   
  131.             $redis->select(3);  
  132.   
  133.             $this->redis = $redis;  
  134.   
  135.   
  136.         }  
  137.   
  138.         return $this->redis;  
  139.   
  140.     }  
  141.   
  142.   
  143. }  
  144.  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多