欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP使用redis位圖bitMap 實現(xiàn)簽到功能

 更新時間:2019年10月08日 15:21:17   作者:Jtoman  
這篇文章主要介紹了PHP使用redis位圖bitMap 實現(xiàn)簽到功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

一、需求

記錄用戶簽到,查詢用戶簽到

二、技術(shù)方案

1、使用mysql(max_time字段為連續(xù)簽到天數(shù))

 

思路:

(1)用戶簽到,插入一條記錄,根據(jù)create_time查詢昨日是否簽到,有簽到則max_time在原基礎(chǔ)+1,否則,max_time=0

(2)檢測簽到,根據(jù)user_id、create_time查詢記錄是否存在,不存在則表示未簽到

2、使用redis位圖功能

思路:

(1)每個用戶每個月單獨一條redis記錄,如00101010101010,從左往右代表01-31天(每月有幾天,就到幾天)
(2)每月8號凌晨,統(tǒng)一將redis的記錄,搬至mysql,記錄如圖

 

(3)查詢當(dāng)月,從redis查,上月則從mysql獲取

3、方案對比

舉例:一萬個用戶簽到365天

方案1、mysql 插入365萬條記錄

· 頻繁請求數(shù)據(jù)庫做一些日志記錄浪費服務(wù)器開銷。
·  隨著時間推移數(shù)據(jù)急劇增大
· 海量數(shù)據(jù)檢索效率也不高,同時只能用時間create_time作為區(qū)間查詢條件,數(shù)據(jù)量大肯定慢

方案2、mysql 插入12w條記錄

· 節(jié)省空間,每個用戶每天只占用1bit空間 1w個用戶每天產(chǎn)生10000bit=1050byte 大概為1kb的數(shù)據(jù)
· 內(nèi)存操作存度快

3、實現(xiàn)(方案2)

(1)key結(jié)構(gòu)

前綴_年份_月份:用戶id -- sign_2019_10:01

查詢:

單個:keys sign_2019_10_01

全部:keys sign_*

月份:keys sign_2019_10:*

(2)mysql表結(jié)構(gòu)

 

(3)代碼(列出1個調(diào)用方法,與三個類)

·簽到方法

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, '您已經(jīng)簽到過了', []);
    }
    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基類

<?php
namespace app\common\redis\db1;
/**
 * redis操作類
 */
class RedisAbstract
{
  /**
   * 連接的庫
   * @var int
   */
  protected $_db = 1;//數(shù)據(jù)庫名
  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));
  }
  /**
   * 獲取是否開啟緩存的設(shè)置參數(shù)
   *
   * @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連接必須設(shè)置');
      }
      self::$redis = new \Redis();
      self::$redis->connect($conf['host'], $conf['port']);
      self::$redis->select($this->_db);
    }
    return self::$redis;
  }
  /**
   * 設(shè)置位圖
   * @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);
  }
  /**
   * 統(tǒng)計位圖
   * @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第一次出現(xiàn)的位置
   * @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);
  }
  /**
   * 刪除數(shù)據(jù)
   * @param $key
   * @return int|null
   * @throws \Exception
   * @author wenzhen-chen
   */
  public function del($key)
  {
    if (!$this->_getEnable()) {
      return null;
    }
    return $this->getRedis()->del($key);
  }
}

·簽到redis操作類

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/30
 * Time: 14:42
 */
namespace app\common\redis\db1;
class Sign extends RedisAbstract
{
  public $keySign = 'sign';//簽到記錄key
  public function __construct($userId,$year,$month)
  {
    parent::__construct();
    //設(shè)置當(dāng)前用戶 簽到記錄的key
    $this->keySign = $this->keySign . '_' . $year . '_' . $month . ':' . $userId;
  }
  /**
   * 用戶簽到
   * @param $day
   * @return int|null
   * @throws \Exception
   * @author wenzhen-chen
   */
  public function setSignLog($day)
  {
    return $this->setBit($this->keySign, $day, 1);
  }
  /**
   * 查詢簽到記錄
   * @param $day
   * @return int|null
   * @throws \Exception
   * @author wenzhen-chen
   */
  public function getSignLog($userId,$day)
  {
    return $this->getBit($this->keySign, $day);
  }
  /**
   * 刪除簽到記錄
   * @return int|null
   * @throws \Exception
   * @author wenzhen-chen
   */
  public function delSignLig()
  {
    return $this->del($this->keySign);
  }
}

· 定時更新至mysql的類

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/4
 * Time: 19:03
 */
namespace app\common\business;
use app\common\mysql\SignLog;
use app\common\redis\db1\Sign;
class Cron
{
  /**
   * 同步用戶簽到記錄
   * @throws \Exception
   */
  public static function addUserSignLogToMysql()
  {
    $data = [];
    $time = Time();
    //1、計算上月的年份、月份
    $dataTime = Common::getMonthTimeByKey(0);
    $year = date('Y', $dataTime['start_time']);
    $month = date('m', $dataTime['start_time']);
    //2、查詢簽到記錄的key
    $signModel = new Sign(0, $year, $month);
    $keys = $signModel->keys('sign_' . $year . '_' . $month . ':*');
    foreach ($keys as $key) {
      $bitLog = '';//用戶當(dāng)月簽到記錄
      $userData = explode(':', $key);
      $userId = $userData[1];
      //3、循環(huán)查詢用戶是否簽到(這里沒按每月天數(shù)存儲,直接都存31天了)
      for ($i = 1; $i <= 31; $i++) {
        $isSign = $signModel->getBit($key, $i);
        $bitLog .= $isSign;
      }
      $data[] = [
        'user_id' => $userId,
        'year' => $year,
        'month' => $month,
        'bit_log' => $bitLog,
        'create_time' => $time,
        'update_time' => $time
      ];
    }
    //4、插入日志
    if ($data) {
      $logModel = new SignLog();
      $logModel->insertAll($data, '', 100);
    }
  }
}

總結(jié)

以上所述是小編給大家介紹的PHP使用redis位圖bitMap 實現(xiàn)簽到功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • PHP實現(xiàn)的漢字拼音轉(zhuǎn)換和公歷農(nóng)歷轉(zhuǎn)換類及使用示例

    PHP實現(xiàn)的漢字拼音轉(zhuǎn)換和公歷農(nóng)歷轉(zhuǎn)換類及使用示例

    這篇文章主要介紹了PHP實現(xiàn)的漢字拼音轉(zhuǎn)換和公歷農(nóng)歷轉(zhuǎn)換類及使用示例,精心整理自網(wǎng)上的資源,需要的朋友可以參考下
    2014-07-07
  • ThinkPHP5實現(xiàn)JWT?Token認(rèn)證的過程(親測可用)

    ThinkPHP5實現(xiàn)JWT?Token認(rèn)證的過程(親測可用)

    這篇文章主要介紹了ThinkPHP5實現(xiàn)JWT?Token認(rèn)證,首先composer先掛載阿里云鏡像,安裝JWT擴(kuò)展,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • PHP分頁類集錦

    PHP分頁類集錦

    本文匯集了幾個比較好用的php分頁類,都是經(jīng)過廣大網(wǎng)友考驗的代碼了,小伙伴們可以直接拿來使用
    2014-11-11
  • 老生常談PHP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展

    老生常談PHP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展

    下面小編就為大家?guī)硪黄仙U凱HP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • php 在線導(dǎo)入mysql大數(shù)據(jù)程序

    php 在線導(dǎo)入mysql大數(shù)據(jù)程序

    我想很多人經(jīng)常會用phpmyadmin進(jìn)行數(shù)據(jù)的導(dǎo)入與導(dǎo)出,但是在很多使用虛擬主機的情況下,導(dǎo)出沒什么問題但是導(dǎo)入就存在很大的問題了,我想這里我也不多說了站長都會知道了.好了我們現(xiàn)在來看看php把mysql數(shù)據(jù)庫里面的數(shù)據(jù)導(dǎo)入程序吧.
    2015-06-06
  • php冒泡排序、快速排序、快速查找、二維數(shù)組去重實例分享

    php冒泡排序、快速排序、快速查找、二維數(shù)組去重實例分享

    這篇文章主要介紹了php冒泡排序、快速排序、快速查找、二維數(shù)組去重實例分享,需要的朋友可以參考下
    2014-04-04
  • twig里使用js變量的方法

    twig里使用js變量的方法

    這篇文章主要介紹了twig里使用js變量的方法,結(jié)合實例形式對比分析了在twig中使用js變量的相關(guān)調(diào)用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-02-02
  • 微信隨機生成紅包金額算法php版

    微信隨機生成紅包金額算法php版

    這篇文章主要為大家詳細(xì)介紹了php版的微信隨機生成紅包金額算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Laravel下生成驗證碼的類

    Laravel下生成驗證碼的類

    這篇文章主要為大家詳細(xì)介紹了Laravel下生成驗證碼的類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • php微信開發(fā)之上傳臨時素材

    php微信開發(fā)之上傳臨時素材

    這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之簡單實現(xiàn)上傳臨時素材的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評論