ÔõÑùÔÚthinkphpÖÐɾ³ý»º´æ
ÔÚʹÓÃthinkphp¾ÙÐпª·¢Ê±£¬Ê¹Óûº´æ¿ÉÒÔÌá¸ßÍøÕ¾ÐÔÄÜ£¬ïÔÌÊý¾Ý¿âÅÌÎʵĴÎÊý£¬ÌáÉýÍøÕ¾ÏìÓ¦ËÙÂÊ¡£¿ÉÊÇ£¬ÈôÊÇ»º´æÓâÆÚʱ¼äÉèÖò»µ±»òÕß»º´æÊý¾Ý±¬·¢×ª±ä£¬¾ÍÐèÒª¾ÙÐлº´æɾ³ý²Ù×÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphpÖÐɾ³ý»º´æ¡£
»º´æÓâÆÚʱ¼äÉèÖÃ
ÔÚʹÓÃthinkphp¾ÙÐлº´æ²Ù×÷ʱ£¬ÐèÒªÖ¸¶¨»º´æÓâÆÚʱ¼ä¡£¿ÉÒÔÔÚconfigĿ¼ÏµÄcache.phpÎļþÖоÙÐÐÉèÖᣴúÂëÈçÏ£º
return [ // ĬÈÏÉèÖõĻº´æ 'default' => env('cache.driver', 'file'), // »º´æÅþÁ¬²ÎÊý 'stores' => [ // Îļþ»º´æ 'file' => [ 'driver' => 'file', 'path' => env('cache.path', app()->getRuntimePath() . 'cache'), ], // Redis»º´æ 'redis' => [ 'driver' => 'redis', 'host' => env('cache.redis.host', '127.0.0.1'), 'port' => env('cache.redis.port', 6379), 'password' => env('cache.redis.password', ''), 'select' => env('cache.redis.select', 0), 'timeout' => env('cache.timeout', 0), 'expire' => env('cache.expire', 0), 'persistent' => env('cache.redis.persistent', false), 'prefix' => '', 'serialize' => [], ], ], // »º´æÉèÖà 'cache' => [ // Çý¶¯·½·¨ 'type' => 'File', // »º´æÉúÑÄĿ¼ 'path' => '../runtime/cache/', // »º´æǰ׺ 'prefix'=> '', // »º´æÓÐÓÃÆÚ 0ÌåÏÖÓÀÊÀ»º´æ 'expire'=> 0, ], ];
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬cache.phpÎļþÖеÄÉèÖÃÏºÜÊÇÏÔ×Å¡£¸÷¸öÉèÖÃÏîµÄ¼ÄÒåÈçÏ£º
default£ºÄ¬ÈϵĻº´æÀàÐÍ£¬¿ÉÒÔƾָ֤¶¨ÇéÐαäÁ¿ÀïµÄcache.driverµÄֵΪfile¡¢redisµÈ¡£
stores£ºÖ§³Ö¶àÖÖÀàÐ͵Ļº´æ£¬Îļþ»º´æºÍRedis»º´æ¡£
driver£ºÖ¸¶¨»º´æÀàÐÍ£¬Èçfile¡¢redisµÈ¡£
cache£ºÉèÖûº´æÊôÐÔ¡£
ÔÚÏêϸµÄÓªÒµ´úÂëÖУ¬Ê¹ÓÃCacheÀà¾ÙÐлº´æ²Ù×÷¡£ÀýÈ磬ÔÚÉèÖûº´æʱ£¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺
use think\facade\Cache; // ÉèÖûº´æ Cache::set('key', 'value', 3600);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬setÒªÁìµÄµÚÒ»¸ö²ÎÊýΪkey£¬µÚ¶þ¸ö²ÎÊýΪvalue£¬µÚÈý¸ö²ÎÊýΪ»º´æÓâÆÚʱ¼ä£¬µ¥Î»ÊÇÃë¡£ÉèÖÃÍ껺´æºó£¬¿ÉÒÔʹÓÃgetÒªÁì»ñÈ¡»º´æÊý¾Ý¡£ÀýÈ磺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
use think\facade\Cache; // »ñÈ¡»º´æ $value = Cache::get('key');
µÇ¼ºó¸´ÖÆ
»º´æɾ³ý
µ±»º´æÊý¾ÝÓâÆÚ»òÕß±¬·¢×ª±äʱ£¬ÐèÒª¾ÙÐлº´æɾ³ý²Ù×÷¡£thinkphpÌṩÁËdeleteÒªÁìÓÃÓÚɾ³ý»º´æ¡£ÀýÈ磺
use think\facade\Cache; // ɾ³ý»º´æ Cache::delete('key');
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬deleteÒªÁìµÄ²ÎÊýΪ»º´æµÄkey£¬¼´ÎªÐèҪɾ³ýµÄ»º´æµÄÃû³Æ¡£
ÔÚ¾ÙÐÐÄ£¿é»¯¿ª·¢Ê±£¬ÐèҪɾ³ýij¸öÄ£¿éϵÄËùÓлº´æÊý¾Ý¡£ÀýÈ磺
use think\facade\Cache; // ɾ³ýij¸öÄ£¿éϵÄËùÓлº´æÊý¾Ý Cache::clear('module');
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬clearÒªÁìµÄ²ÎÊýΪģ¿éÃû³Æ£¬¼´ÎªÐèҪɾ³ý»º´æµÄÄ£¿éÃû¡£ÔÚ¾ÙÐлº´æɾ³ý²Ù×÷ʱ£¬ÐèҪȷ±£É¾³ýµÄ»º´æÃû³ÆºÍÉèÖûº´æʱµÄÃû³ÆÒ»Ö£¬²»È»ÎÞ·¨É¾³ýÒÑÉèÖõĻº´æ¡£
»º´æʧЧ
ÔÚijЩÇéÐÎÏ£¬ÐèÒªÈÃËùÓеĻº´æÊý¾ÝʧЧ£¬ÕâʱÐèҪʹÓûº´æµÄflushÒªÁì¡£ÀýÈ磺
use think\facade\Cache; // ʧЧËùÓлº´æÊý¾Ý Cache::clear();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬clearÒªÁì²»´ø²ÎÊý£¬¼´¿ÉʧЧËùÓеĻº´æÊý¾Ý¡£ÔÚ¾ÙÐлº´æÇå¿Õ²Ù×÷ʱ£¬ÐèÒªÉóÉ÷²Ù×÷£¬×èÖ¹Îóɾ»º´æÊý¾Ý¡£
×ܽ᣺
ÔÚʹÓÃthinkphp¾ÙÐпª·¢Ê±£¬»º´æÊÇÌá¸ßÍøÕ¾ÐÔÄܵÄÖ÷ÒªÊֶΡ£ÔÚ»º´æÓâÆÚ»òÕßÊý¾Ý±¬·¢×ª±äʱ£¬ÐèÒª¾ÙÐлº´æɾ³ý²Ù×÷¡£thinkphpÌṩÁËɨ³ýij¸öÄ£¿é»òËùÓÐÄ£¿éµÄ»º´æÒªÁ죬ÒÔ¼°Ê§Ð§ËùÓлº´æÊý¾ÝµÄÒªÁì¡£ÔÚ¾ÙÐлº´æɾ³ý²Ù×÷ʱ£¬ÐèҪȷ±£É¾³ýµÄ»º´æÃû³ÆºÍÉèÖûº´æʱµÄÃû³ÆÒ»Ö¡£ÔÚ¾ÙÐлº´æÇå¿Õ²Ù×÷ʱ£¬ÐèÒªÉóÉ÷²Ù×÷£¬×èÖ¹Îóɾ»º´æÊý¾Ý¡£
ÒÔÉϾÍÊÇÔõÑùÔÚthinkphpÖÐɾ³ý»º´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡