当前位置:网站首页>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');
}
边栏推荐
- Conditional test, if, case conditional test statements of shell script
- MySQL - use of aggregate functions and group by groups
- Gauss elimination method and template code
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
- Related configuration commands of Huawei rip
- Msp32c3 board connection MSSQL method
- Future源码一观-JUC系列
- Which insurance products can the elderly buy?
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- Oracle database knowledge points (I)
猜你喜欢
The FISCO bcos console calls the contract and reports an error does not exist
[common error] custom IP instantiation error
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
Technical practice online fault analysis and solutions (Part 1)
What is the GPM scheduler for go?
How to be a professional software testing engineer? Listen to the byte five year old test
In the process of seeking human intelligent AI, meta bet on self supervised learning
Infiltration learning diary day19
随机推荐
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
Since the "epidemic", we have adhered to the "no closing" of data middle office services
The FISCO bcos console calls the contract and reports an error does not exist
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
OS interrupt mechanism and interrupt handler
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
Future source code view -juc series
Print diamond pattern
What is the GPM scheduler for go?
be based on. NETCORE development blog project starblog - (14) realize theme switching function
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
QML add gradient animation during state transition
机器学习基础:用 Lasso 做特征选择
Introduction to thread pool
Understanding of Radix
Swagger2 quick start and use
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
技術實踐|線上故障分析及解决方法(上)
求esp32C3板子连接mssql方法
Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction