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'); }
 

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element