当前位置:网站首页>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');
}边栏推荐
- Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
- How to set the response description information when the response parameter in swagger is Boolean or integer
- Flutter local database sqflite
- CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
- 中电资讯-信贷业务数字化转型如何从星空到指尖?
- gslb(global server load balance)技术的一点理解
- What insurance products should be bought for the elderly?
- Future source code view -juc series
- [common error] UART cannot receive data error
- Conditional test, if, case conditional test statements of shell script
猜你喜欢

It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction

中电资讯-信贷业务数字化转型如何从星空到指尖?

Beijing invites reporters and media

Mobile asynchronous sending SMS verification code solution -efficiency+redis

How can enterprises optimize the best cost of cloud computing?

Analysis and solution of lazyinitializationexception

Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing

Luogu p1309 Swiss wheel

Sequence list and linked list

1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
随机推荐
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
[dynamic programming] leetcode 53: maximum subarray sum
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习
MySQL uses the view to report an error, explain/show can not be issued; lacking privileges for underlying table
7.1 学习内容
不得不会的Oracle数据库知识点(三)
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
Severity code description the project file line prohibits the display of status error c4996 fopen ('fscanf ', StrCmp): this function or variable may be unsafe The most comprehensive solution
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Sequence list and linked list
[error record] configure NDK header file path in Visual Studio
QML add gradient animation during state transition
Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
swagger中响应参数为Boolean或是integer如何设置响应描述信息
Day05 table
不得不会的Oracle数据库知识点(四)
MySQL deadly serial question 2 -- are you familiar with MySQL index?