当前位置:网站首页>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');
}边栏推荐
- be based on. NETCORE development blog project starblog - (14) realize theme switching function
- 【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
- It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
- C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
- Gee: create a new feature and set corresponding attributes
- Swagger2 quick start and use
- HackTheBox-baby breaking grad
- swagger中响应参数为Boolean或是integer如何设置响应描述信息
- 功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
- GUI application: socket network chat room
猜你喜欢

How can enterprises optimize the best cost of cloud computing?

A-Frame虚拟现实开发入门

“疫”起坚守 保障数据中台服务“不打烊”

Network layer - routing

Since the "epidemic", we have adhered to the "no closing" of data middle office services

Make drop-down menu

Future source code view -juc series

How to set the response description information when the response parameter in swagger is Boolean or integer

Is it really possible that the monthly salary is 3K and the monthly salary is 15K?

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
随机推荐
2-Redis架构设计到使用场景-四种部署运行模式(下)
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
What insurance products should be bought for the elderly?
C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
In the process of seeking human intelligent AI, meta bet on self supervised learning
技術實踐|線上故障分析及解决方法(上)
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Future源码一观-JUC系列
CLP information - how does the digital transformation of credit business change from star to finger?
Summary of JWT related knowledge
Introduction to A-frame virtual reality development
Msp32c3 board connection MSSQL method
Day05 表格
From functools import reduce -- see the use of reduce function from typical examples
Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
MySQL uses the view to report an error, explain/show can not be issued; lacking privileges for underlying table
The culprit of unrestrained consumption -- Summary
Employees' turnover intention is under the control of the company. After the dispute, the monitoring system developer quietly removed the relevant services
[common error] custom IP instantiation error