当前位置:网站首页>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');
}
边栏推荐
- Design of database table foreign key
- GUI application: socket network chat room
- [common error] custom IP instantiation error
- Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
- What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
- Beijing invites reporters and media
- AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
- What is the GPM scheduler for go?
- OS interrupt mechanism and interrupt handler
- 0 basic learning C language - nixie tube dynamic scanning display
猜你喜欢
GUI 应用:socket 网络聊天室
Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
In the process of seeking human intelligent AI, meta bet on self supervised learning
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
2-Redis架构设计到使用场景-四种部署运行模式(下)
How to be a professional software testing engineer? Listen to the byte five year old test
A-Frame虚拟现实开发入门
How to delete MySQL components using xshell7?
Sorry, Tencent I also refused
Conditional test, if, case conditional test statements of shell script
随机推荐
手机异步发送短信验证码解决方案-Celery+redis
Future source code view -juc series
Introduction to thread pool
A malware detection method for checking PLC system using satisfiability modulus theoretical model
OS interrupt mechanism and interrupt handler
Summary of common tools and technical points of PMP examination
不得不会的Oracle数据库知识点(二)
What is the GPM scheduler for go?
Since the "epidemic", we have adhered to the "no closing" of data middle office services
[prefix and notes] prefix and introduction and use
MySQL - use of aggregate functions and group by groups
Sequence list and linked list
长文综述:大脑中的熵、自由能、对称性和动力学
Oracle database knowledge points that cannot be learned (III)
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
[software testing] you haven't mastered these real interview questions of big companies?
Beijing invites reporters and media
All in one 1412: binary classification
[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)
Luogu p1309 Swiss wheel