thinkphp Using cache can improve system performance effectively , Save database operation and memory cost .

Simple example :
<?php use think\Cache; if (Cache::has('user_data')) { $users =
Cache::get('user_data') } else { $users = User::where('status', 1)->select();
// Cache user data 5 minute Cache::set('user_data', $users, 300); }
The above code can also be simplified to :
<?php use think\Cache; $users = Cache::remember('user_data', function(){
return User::where('status', 1)->select(); },300);

Here's the problem , What is the cache time 5 minute , If it's here 5 Within minutes, yes user The user table was added , modify , Delete and other operations , You will find that the cache has not changed , The website can't get the most real data in time . What should we do ?

resolvent :

Add at the end of execution , modify , Delete, etc , call Cache::clear(), as follows :
<?php use think\Cache; public function add(){ try{ User::create([ 'name' =>
"tom", 'age' => 18 ]); } catch (\Exception $e) { $this->error(' Add failed '); } // Clean up cache
Cache::clear(); $this->success(' Added successfully '); }
supplement : Clear cache , Methods of template cache and log cache
namespace app\cache\controller; use think\Controller; use think\Cache;
// Clear template cache without deleting cache catalog ; public function clear_sys_cache() { Cache::clear();
$this->success(' Cleared successfully ', 'Index/index'); } // Clear template cache without deleting temp catalog ; public function
clear_temp_ahce() { $path = glob(TEMP_PATH . '*.php'); array_map('unlink',
$path); //rmdir(TEMP_PATH .'temp/');// delete temp catalog $this->success(' Cleared successfully ',
'Index/index'); } // Clear log cache and delete log Empty directory ; public function clear_log_chache() { $path
= glob(LOG_PATH . '*'); foreach ($path as $item) { //dump(glob($item .DS.
'*.log')); array_map('unlink', glob($item . DS . '*.log')); rmdir($item); }
$this->success(' Cleared successfully ', 'Index/index'); }

©2019-2020 Toolsou All rights reserved,
The 11th Blue Bridge Cup python The real topic of the University Group National Games JavaSwing To achieve a simple Lianliankan games 【Spring Source code analysis 】42-@Conditional Detailed explanation element-ui Step on pit record 2019PHP Interview questions ( Continuously updated )PHPJava Misunderstanding —— Method overloading is a manifestation of polymorphism ? First issue 500 100 million , Set up a new Department , What is Tencent going to do ? Google chrome The browser can't open the web page , But what if other browsers can open it ? Regression of dependent variable order categories (R language )【Golang Basic series 10 】Go language On conditional sentences if