当前位置:网站首页>ThinkPHP uses redis to update database tables
ThinkPHP uses redis to update database tables
2022-07-04 01:12:00 【fridayCodeFly】
One 、common.php
<?php
// Apply public files
use think\cache\driver\Redis;
use think\facade\Db;
/**
* @param $key [ To be deleted redis The key , It can be understood as virtual table name ]
*/
function delRedis($key){
$redis=new Redis();
$redis->delete($key);
}
/**
* @param $key [ To be updated or created redis Of key namely , Name of the table to be created ]
* @param $value [key Corresponding value , It can be many types of parameters ]
*/
function setRedis($key,$value){
$redis=new Redis();
$redis->set($key,$value);
}
/**
* @param $key [ To be extracted or queried redis Of key namely , A table name ]
* @return mixed|null
*/
function getRedis($key){
$redis=new Redis();
return $redis->get($key);
}
/**
* @param $tablename [ Database table name ]
* @return mixed|null
*/
function dbMy($tablename){
return Db::name($tablename);
}
/**
* @param $tablename [sqlite Database table name ]
* @return mixed|null
*/
function dbLite($tablename){
$prefix=config("database.connections.sqlite.prefix");
return Db::connect('sqlite')->table($prefix.$tablename);
}
function rePost($key){
return request()->post($key);
}Two 、database controller, An update 50 strip . You can also set more . When there's new data , Insert , When there is no new data , Update old data .
function renewDbData($table_name,$lite_table){
//rk=redisKey
$rk = 'renewData';
if(repost('table_name')){
$table_name=repost('table_name');
}
if(repost('lite_table')){
$lite_table=repost('lite_table');
}
// initialization redis surface ,rd=redisData
// If you run directly after the second time redis surface , And assign it to the variable
if(repost('initial')){
$this->renewInitial($rk,$table_name,$lite_table);
}
$rd = getRedis($rk);
$newData = dbLite($lite_table)->page($rd['page'],50)->select()->toArray();
foreach($newData as $key=>$item){
$newID = $item['id'];
$newDate = $item['partition_key'];
if(isset($rd['oldData'][$newID])){
// If the same data exists, skip , And add the updated quantity 1
if($newDate==$rd['oldData'][$newID]){
// Start to change redis The value of
$rd['sameCount']++;
$rd['alreadyRenewCount']=$rd['sameCount']+
$rd['insCount']+$rd['updateCount'];
$rd['progress'] =
round($rd['alreadyRenewCount']/$rd['renewCount'],
2);
unset($rd['oldData'][$newID]);
if($rd['progress']==1){
$rd['isEnd']=1;
setRedis($rk,$rd);
if(repost('lite_table')){
return json($rd);
}
return $rd;
}
setRedis($rk,$rd);
continue;
}
// If there is no data of the same version , be update
unset($rd['oldData'][$newID]);
dbMy($table_name)->save($item);
$rd['updateCount']++;
}
else{
$rd['insCount'] += dbMy($table_name)->insert($item);
}
$rd['alreadyRenewCount']=$rd['sameCount']+
$rd['insCount']+$rd['updateCount'];
$rd['progress'] =
round($rd['alreadyRenewCount']/$rd['renewCount'],
2);
setRedis($rk,$rd);
}
if($rd['progress']==1){
$rd['isEnd']=1;
}elseif($rd['page']*50<=$rd['renewCount']){
$rd['page']++;
}
setRedis($rk,$rd);
if(repost('lite_table')){
return json($rd);
}
return $rd;
}
function renewInitial($redisKey,$table_name,$lite_table){
// Delete the original data first
delRedis($redisKey);
$rd = ['renewCount'=>0,'sameCount'=>0,'alreadyRenewCount'=>0,
'page'=>1,'insCount'=>0,'updateCount'=>0,'oldData'=>[]];
$oldData=dbMy($table_name)->field('id,partition_key')->
select()->toArray();
$oldData=$this->oldData($oldData);
$newData = dbLite($lite_table)->field('count(*) count')->select()->toArray();
$count = $newData[0]['count']*1;
$rd['renewCount']=$count;
$rd['oldData']=$oldData;
setRedis($redisKey,$rd);
}
function oldData($data){
$res = [];
foreach($data as $key=>$item){
$value = $item['partition_key'];
if($value){
$res[$item['id']]=$value;
}
else{
$res[$item['id']]=' No data ';
}
}
return $res;
}
function progress(){
return getRedis('renewData');
}边栏推荐
- Related configuration commands of Huawei rip
- Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
- The first training of wechat applet
- 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
- MySQL deadly serial question 2 -- are you familiar with MySQL index?
- Unity Shader入门精要读书笔记 第三章 Unity Shader基础
- How to set the response description information when the response parameter in swagger is Boolean or integer
- [error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
- Fundamentals of machine learning: feature selection with lasso
猜你喜欢
![[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)](/img/44/aa4963d07d046deb2bc76eb59f8ff7.jpg)
[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)

Weekly open source project recommendation plan
![[common error] UART cannot receive data error](/img/77/6ba56ce6e64beeb73a77d04af5bd0f.jpg)
[common error] UART cannot receive data error

MySQL deadly serial question 2 -- are you familiar with MySQL index?

I don't care about you. OKR or KPI, PPT is easy for you

Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them

Function: find the approximate value of the limit of the ratio of the former term to the latter term of Fibonacci sequence. For example, when the error is 0.0001, the function value is 0.618056.

我管你什么okr还是kpi,PPT轻松交给你
![[dynamic programming] leetcode 53: maximum subarray sum](/img/f0/80357f9ffc556f3ed4d3aa0901bb1d.jpg)
[dynamic programming] leetcode 53: maximum subarray sum

How to set the response description information when the response parameter in swagger is Boolean or integer
随机推荐
Mongodb learning notes: command line tools
中电资讯-信贷业务数字化转型如何从星空到指尖?
Huawei rip and BFD linkage
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
A malware detection method for checking PLC system using satisfiability modulus theoretical model
From functools import reduce -- see the use of reduce function from typical examples
Force deduction solution summary 1189- maximum number of "balloons"
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Since the "epidemic", we have adhered to the "no closing" of data middle office services
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
[software testing] you haven't mastered these real interview questions of big companies?
不得不会的Oracle数据库知识点(二)
Network layer - routing
Oracle database knowledge points (I)
Fundamentals of machine learning: feature selection with lasso
[common error] UART cannot receive data error
String hash, find the string hash value after deleting any character, double hash
[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)