laravelÔõô×ö¾²Ì¬Ò³Ã滺´æ
laravelÊÇphp¿ª·¢ÕßʹÓÃ×îÆÕ±éµÄ¿ò¼ÜÖ®Ò»£¬Ëü¾ßÓÐÇ¿Ê¢µÄ¹¦Ð§ºÍÎÞаµÄÉèÖᣵ±ÎÒÃǹ¹½¨Ò»¸ö»ùÓÚlaravelµÄÓ¦ÓóÌÐòʱ£¬ÓÐʱ¼äÎÒÃÇÐèÒª»º´æһЩ¾²Ì¬Ò³ÃæÒÔÌá¸ßÍøÕ¾µÄÐÔÄÜ¡£
ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃǽ«ÑÝʾÔõÑùʹÓÃLaravelµÄ»º´æ¹¦Ð§À´»º´æ¾²Ì¬Ò³Ã棬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍÏìӦʱ¼ä¡£ÎÒÃǽ«ÊµÑéÔÚÁ½¸ö°ì·¨ÖÐÍê³ÉÕâ¸öʹÃü£ºÊ×ÏÈ£¬ÎÒÃǽ«ÉèÖÃLaravelµÄ»º´æÇý¶¯³ÌÐò£¬È»ºóÎÒÃǽ«½¨ÉèÒ»ÆäÖÐÑë¼þÀ´»º´æ¾²Ì¬Ò³Ãæ¡£
Ò»¡¢ÉèÖûº´æÇý¶¯³ÌÐò
LaravelÖ§³Ö¶àÖÖ»º´æÇý¶¯³ÌÐò£¬ÈçRedis¡¢Memcached¡¢APCºÍÎļþ»º´æ¡£ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÇéÐÎÀ´Ñ¡ÔñÊʺÏÎÒÃǵĻº´æÇý¶¯³ÌÐò¡£ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Ê¹ÓÃÎļþ»º´æÇý¶¯³ÌÐòÀ´»º´æ¾²Ì¬Ò³Ãæ¡£
Ê×ÏÈ£¬ÔÚconfig/cache.phpÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒª½«Ä¬ÈϵĻº´æÇý¶¯³ÌÐò¸ÄΪÎļþ»º´æÇý¶¯³ÌÐò£¬´úÂëÈçÏ£º
'default' => env('CACHE_DRIVER', 'file'),
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÔÚconfig/cache.phpÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒªÖ¸¶¨»º´æÎļþµÄ´æ´¢Â·¾¶£¬´úÂëÈçÏ£º
'path' => storage_path('framework/cache/data'),
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇÒѾÉèÖÃÁËLaravelµÄ»º´æÇý¶¯³ÌÐò£¬ÎÒÃÇ¿ÉÒÔ×îÏȹ¹½¨Ò»ÆäÖÐÑë¼þÀ´»º´æ¾²Ì¬Ò³Ãæ¡£
¶þ¡¢½¨É軺´æÒ³ÃæµÄÖÐÑë¼þ
ÔÚLaravelÖУ¬ÖÐÑë¼þÊÇ´¦ÀíHTTPÇëÇóµÄÒ»¸öÖ÷Òª×é³É²¿·Ö¡£ÎÒÃÇ¿ÉÒÔʹÓÃÖÐÑë¼þÀ´¹ýÂËÇëÇó¡¢Ìí¼ÓÍ·ÐÅÏ¢¡¢ÉèÖÃcookieµÈµÈ¡£ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«½¨ÉèÒ»ÆäÖÐÑë¼þÀ´»º´æ¾²Ì¬Ò³Ãæ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÃûΪ¡°CachePage¡±µÄÖÐÑë¼þ£¬ÓÃÓÚ»º´æ¾²Ì¬Ò³Ãæ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÌìÉúеÄÖÐÑë¼þ£º
php artisan make:middleware CachePage
µÇ¼ºó¸´ÖÆ
µ±ÎÒÃÇÔËÐÐÉÏÊöÏÂÁîʱ£¬Laravel½«×Ô¶¯½¨ÉèÃûΪ¡°CachePage.php¡±µÄÎļþ²¢½«Æä°²ÅÅÔÚapp/Http/MiddlewareĿ¼ÖС£·¿ª¸ÃÎļþ£¬²¢ÔÚhandleÒªÁìÖÐÌí¼ÓÒÔÏ´úÂ룺
public function handle($request, Closure $next) { // ¼ì²é´ËÇëÇóÊÇ·ñ¿ÉÒÔ»º´æ if (! $this->shouldCache($request)) { return $next($request); } // »ñÈ¡´ËÇëÇóµÄ»º´æ¼ü $key = $this->getCacheKey($request); // ʵÑé´Ó»º´æÖлñÈ¡´ËÇëÇóµÄÒ³Ãæ if (Cache::has($key)) { return response(Cache::get($key)); } // ÈôÊÇûÓдӻº´æÖлñÈ¡µ½£¬ÄÇô¾Í´¦ÀíÇëÇó²¢½«Ð§¹û»º´æÏÂÀ´ $response = $next($request); if ($this->shouldCacheResponse($response)) { Cache::put($key, $response->getContent(), $this->getCacheLifetime()); } return $response; } // ¼ì²é´ËÇëÇóÊÇ·ñ¿ÉÒÔ»º´æ protected function shouldCache($request) { if (!$request->isMethod('get')) { return false; } if ($request->query->count()) { return false; } if (auth()->check()) { return false; } if (config('app.env') === 'local') { return false; } return true; } // »ñÈ¡´ËÇëÇóµÄ»º´æ¼ü protected function getCacheKey($request) { return 'cache:' . $request->url(); } // ¼ì²é´ËÇëÇóµÄÏìÓ¦ÊÇ·ñ¿ÉÒÔ»º´æ protected function shouldCacheResponse($response) { return $response->status() === 200; } // »ñÈ¡Ò³Ã滺´æµÄÉúÑÄʱ¼ä protected function getCacheLifetime() { return config('cache.page_cache_lifetime'); }
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÈÃÎÒÃÇÖð²½Ú¹ÊÍÒ»ÏÂÉÏÃæµÄ´úÂ룺
ÎÒÃÇÊ×Ïȼì²é´ËÇëÇóÊÇ·ñ¿ÉÒÔ»º´æ¡£ÎÒÃÇÖ»ÐèÒª»º´æGETÇëÇó¡¢Ã»ÓÐÈκÎÅÌÎʲÎÊýµÄÇëÇó¡¢Î´¾ÓÉÉí·ÝÑéÖ¤µÄÇëÇóºÍ·ÇÍâµØÇéÐÎϵÄÇëÇó¡£
ÈôÊÇÎÒÃÇÈ·¶¨´ËÇëÇó¿ÉÒÔ»º´æ£¬ÎÒÃǾͻñÈ¡»º´æ¼ü¡£ÔÚ±¾ÀýÖУ¬ÎÒÃÇʹÓÃÇëÇóµÄURL×÷Ϊ»º´æ¼ü¡£
È»ºó£¬ÎÒÃÇʵÑé´Ó»º´æÖлñÈ¡´ËÒ³Ãæ¡£ÈôÊÇÎÒÃÇÄܹ»´Ó»º´æÖлñÈ¡µ½´ËÒ³Ã棬Ôò·µ»ØËü²¢ÖÐÖ¹ÆäËüÀú³Ì¡£
ÈôÊÇ´Ó»º´æÖлñÈ¡²»µ½Ò³Ã棬Ôò´¦ÀíÇëÇó²¢»º´æЧ¹û¡£×îºó£¬·µ»Ø´¦ÀíµÄЧ¹û¡£
ÏÖÔÚ£¬ÔÚKernel.phpÎļþÖÐ×¢²áÕâÆäÖÐÑë¼þ¡£ÎÒÃǽ«ËüµÄ×¢²á´úÂëÌí¼Óµ½$routeMiddlewareÊý×éÖУ¬´úÂëÈçÏ£º
'cache.page' => \App\Http\Middleware\CachePage::class,
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Ã¿µ±ÎÒÃǵÄÓ¦ÓóÌÐò´¦ÀíÒ»¸öÆ¥ÅäCachePageÖÐÑë¼þµÄ·ÓÉʱ£¬¸ÃÖÐÑë¼þ½«»á»º´æ´¦ÀíЧ¹û£¬²¢½«Æä´æ´¢ÔÚÎļþϵͳÖС£µ±ÏÂÒ»´ÎÇëÇóµ½À´Ê±£¬ÖÐÑë¼þ½«´Ó»º´æÖмìË÷Ò³Ã棬²¢¿ìËÙ·µ»ØЧ¹û¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇչʾÁËÔõÑùʹÓÃLaravelµÄ»º´æ¹¦Ð§À´»º´æ¾²Ì¬Ò³Ãæ¡£ÎÒÃÇÊ×ÏÈÉèÖÃÁËLaravelµÄ»º´æÇý¶¯³ÌÐò£¬È»ºó½¨ÉèÁËÒ»ÆäÖÐÑë¼þÀ´»º´æ¾²Ì¬Ò³Ãæ¡£ÔÚÕû¸öÀú³ÌÖУ¬ÎÒÃǹØ×¢ÁËһЩ×î¼Ñʵ¼ùºÍÏÖʵʹÓð¸Àý£¬ÎªÄúÑÝʾÁËÔõÑùÓÅ»¯ÄúµÄÓ¦ÓóÌÐò£¬È·±£Æä×î¼ÑÐÔÄܺÍÏìӦʱ¼ä¡£
ÒÔÉϾÍÊÇlaravelÔõô×ö¾²Ì¬Ò³Ã滺´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡