开发思路就是用redis得有序组合功能
1.先自增到reids中
$redisGetKey = 'ciku_promoter_click_number'; $jyId = 2; // 文章id $redis = RedisService::build()->getInstance(); $videoPlayNumber = $redis->zScore($redisGetKey, $jyId);
// 如果该文章id存在,则新增,否则自增1 if ($videoPlayNumber) { $redis->zAdd($redisGetKey, intval($videoPlayNumber + 1), $jyId); }else{ $redis->zAdd($redisGetKey, 1, $jyId); }
2.再从redis取出并匹配自增
// 统计redis中的点击量,写入数据库中 public function setPromoterClickNumber() { $redis = RedisService::build()->getInstance(); $redisKey = 'ciku_promoter_click_number'; // 获取有序集合中的全部数据 $newArticleList = []; $articleIdArr = $redis->zRange($redisKey, 0, -1); foreach ($articleIdArr as $key => $value) { $redisKeyNumber = $redis->zScore($redisKey, $value); if ($redisKeyNumber) { $newArticle['id'] = $value; $newArticle['click_number'] = intval($redisKeyNumber); $newArticleList[] = $newArticle; } } $articleList = PromoterUser::build()->where('id','in',$articleIdArr)->column('click_number','id'); // 更新文章的点击量 $updateArticleList = []; foreach ($articleList as $key => $value) { foreach ($newArticleList as $key1 => $value1) { if ($key == $value1['id']) { $updateArticle['id'] = $key; $updateArticle['click_number'] = $value + $value1['click_number']; $updateArticleList[] = $updateArticle; } } } // 组合sql语句,批量更新 if ($updateArticleList) { $updateField = ['click_number']; $this->sqlBatchUpdate('cy_promoter_user', $updateArticleList, $updateField, 'id'); } // 删除有序集合中的全部数据 $redis->del($redisKey); }
3.组合sql语句,批量更新
/** * 数据用sql批量更新 * * @param [type] $tableName 表名,包含表前缀 * @param [type] $updateData 待更新数据列表 * @param [type] $updateField 更新字段数组列表,顺序需要与数据列表一致 * @param [type] $whereId 更新条件,id、order_sn等 * @return void */ public function sqlBatchUpdate($tableName, $updateData, $updateField, $whereId) { $updateValueSql = ''; $updateIdSql = ''; $updateKey = $updateField; foreach ($updateKey as $key) { $n = 0; foreach ($updateData as $value) { $n++; if ($n == 1) { $updateValueSql .= $key . " = CASE $whereId "; } $updateValueSql .= 'WHEN \'' . $value[$whereId] . '\' THEN \'' . $value[$key] . '\''; if ($n != count($updateData)) { $updateValueSql .= " "; } else { $updateValueSql .= " END,"; } } } $updateValueSql = substr($updateValueSql, 0, strlen($updateValueSql) - 1); $n = 0; foreach ($updateData as $value) { $n++; if ($n == 1) { $updateIdSql .= "("; } $updateIdSql .= ' \'' . $value[$whereId] . '\' '; if ($n != count($updateData)) { $updateIdSql .= ","; } else { $updateIdSql .= ")"; } } if (count($updateData)) { $update_sql = 'UPDATE ' . $tableName . ' SET ' . $updateValueSql . ' WHERE ' . $whereId . ' IN ' . $updateIdSql . ';'; Db::query($update_sql); // 执行更新语句 } }
标签:播放,Redis,redis,value,click,updateValueSql,key,php,id From: https://www.cnblogs.com/seanpan/p/17657332.html