Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel CacheʵÏÖ»º´æ£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel cacheʵÏÖ»º´æ£¿
Ëæ×ÅWebÓ¦ÓóÌÐòµÄÉú³¤£¬ÐÔÄÜÎÊÌâÒѾ³ÉΪÏÖ´úWebÓ¦ÓóÌÐòµÄÒªº¦ÎÊÌ⡣ʹÓûº´æÊǽâ¾öWebÓ¦ÓóÌÐòÐÔÄÜÎÊÌâµÄÒ»ÖÖ³£¼ûÒªÁì¡£LaravelÌṩÁËÒ»ÖÖÎÞаµÄ»º´æ½â¾ö¼Æ»®£¬³ÆΪ»º´æ¡£»º´æÊÇÒ»ÖÖ½«Êý¾Ý´æ´¢ÔÚÔÝʱ´æ´¢ÖеÄÊÖÒÕ£¬ÒÔ±ãÒÔºó¿ÉÒÔ¸ü¿ìµØ¼ìË÷ºÍÏÔʾÊý¾Ý¡£±¾ÎĽ«Ì½ÌÖLaravel»º´æÔõÑùʵÏÖºÍʹÓá£
Laravel Cache¼ò½é
Laravel CacheÌṩÁËÒ»ÖÖͳһµÄAPIÀ´Ê¹ÓÃÖÖÖÖ»º´æºó¶Ë£¨ÈçMemcached£¬RedisºÍÎļþ»º´æ£©¡£ÓÉÓÚLaravel CacheÊÇLaravelµÄ×é¼þÖ®Ò»£¬Òò´Ë¿ÉÒÔʹ»º´æ±äµÃÔ½·¢ÈÝÒ׺ÍÀû±ã¡£Laravel Cache¿ÉÒÔÔÚÓ¦ÓóÌÐòÖеÄÈκεط½Ê¹Óã¬ÒÔÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡£
Laravel CacheÉèÖÃ
Laravel CacheµÄĬÈϺó¶ËÊÇÎļþÇý¶¯¡£¿ÉÊÇ£¬ÎÒÃÇ¿ÉÒÔÇáËɵØÔÚconfig / cache.phpÎļþÖиü¸Ä»º´æºó¶Ë¡£ÀýÈ磬ҪʹÓÃMemcached»º´æºó¶Ë£¬¿ÉÒÔÔÚconfig / cache.phpÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
‘cache’ => [
'default' => env('CACHE_DRIVER', 'memcached'), 'stores' => [ 'memcached' => [ 'driver' => 'memcached', 'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), 'sasl' => [ env('MEMCACHED_USERNAME'), env('MEMCACHED_PASSWORD'), ], 'options' => [ // Memcached::OPT_CONNECT_TIMEOUT => 2000, ], 'servers' => [ [ 'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => env('MEMCACHED_WEIGHT', 100), ], ], ], // ... ], // ...
µÇ¼ºó¸´ÖÆ
],
Õâ¸öʾÀý´úÂëÖУ¬ÎÒÃÇʹÓÃÁËMemcached»º´æºó¶Ë¡£
Laravel CacheʹÓÃ
ÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓûº´æÊǺÜÊÇÈÝÒ׵ġ£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ËµÃ÷ÎúÔõÑù½«Êý¾Ý´æ´¢ÔÚ»º´æÖУº
// ´æ´¢Êý¾Ý
Cache::put(‘key’, ‘value’, $minutes);
// »ñÈ¡Êý¾Ý
$value = Cache::get(‘key’);
// ÅжÏÊý¾ÝÊÇ·ñ±£´æ
if (Cache::has(‘key’)) {
//
µÇ¼ºó¸´ÖÆ
}
// ɾ³ýÊý¾Ý
Cache::forget(‘key’);
Õâ¸öʾÀý´úÂëÖУ¬Cache::put£¨£©ÒªÁ콫Êý¾Ý´æ´¢ÔÚ»º´æÖУ¬Cache::get£¨£©ÒªÁì´Ó»º´æÖмìË÷Êý¾Ý£¬Cache::has£¨£©ÒªÁì¼ì²éÊý¾ÝÊÇ·ñ±£´æ£¬Cache::forget£¨£©ÒªÁì´Ó»º´æÖÐɾ³ýÊý¾Ý¡£
»º´æ±êÇ©
»º´æ±êÇ©ÊÇÒ»ÖÖ½«¶à¸ö»º´æÏî×éÖ¯µ½Ò»ÆðµÄ»úÖÆ¡£Ê¹Óûº´æ±êÇ©ºÜÈÝÒ×ʶ±ð²¢É¨³ý»º´æ¡£ÏÂÃæÊÇÒ»¸ö˵Ã÷ÔõÑùʹÓûº´æ±êÇ©µÄʾÀý£º
// ½«Êý¾Ý´æ´¢µ½»º´æ±êÇ©ÖÐ
Cache::tags([‘people’, ‘artists’])->put(‘John’, $john, $minutes);
Cache::tags([‘people’, ‘authors’])->put(‘Anne’, $anne, $minutes);
// ÕûÀí»º´æ±êÇ©Êý¾Ý
Cache::tags(‘people’)->flush();
Õâ¸öʾÀý´úÂëÖУ¬ÎÒÃÇʹÓÃÁË»º´æ±êÇ©½«Êý¾Ý×éÖ¯ÔÚÒ»Æð¡£Ê¹ÓÃCache::tags£¨£©ÒªÁìÖ¸¶¨»º´æ±êÇ©£¬È»ºó¿ÉÒÔʹÓÃput£¨£©½«Êý¾Ý´æ´¢ÔÚ±êÇ©ÖС£×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃflush£¨£©ÒªÁìɨ³ýÓë±êÇ©¹ØÁªµÄËùÓлº´æ¡£
»º´æÓÐÓÃÆÚ
Laravel CacheÌṩÁËÐí¶à²î±ðµÄÒªÁìÀ´ÉèÖûº´æµÄÓÐÓÃÆÚ¡£×î³£ÓõÄÒªÁìÊÇʹÓÃput£¨£©ÒªÁìÖеÄ$ minutesÊôÐÔ¡£ÀýÈ磬Ҫ½«»º´æÏî´æ´¢Îª10·ÖÖÓ£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
// ´æ´¢10·ÖÖÓ»º´æ
Cache::put(‘key’, ‘value’, 10);
// ´æ´¢ÓÀÊÀ»º´æ
Cache::forever(‘key’, ‘value’);
// ¼ìË÷Öµ²¢ÖØд洢Ëü
$value = Cache::remember(‘users’, $minutes, function () {
return DB::table('users')->get();
µÇ¼ºó¸´ÖÆ
});
Õâ¸öʾÀý´úÂëÖУ¬Cache::put£¨£©ÒªÁ콫Êý¾Ý´æ´¢ÔÚ»º´æÖУ¬¿ÉÊÇ»º´æ½ö±£´æ10·ÖÖÓ¡£Cache::forever£¨£©ÒªÁ콫Êý¾Ý´æ´¢ÔÚ»º´æÖУ¬µ«²»ÉèÖõ½ÆÚʱ¼ä¡£Cache::remember£¨£©ÒªÁ콫ÔÚ»º´æÖвéÕÒ¡°users¡±Ïî¡£ÈôÊǸÃÏî²»±£´æ£¬ÔòŲÓûص÷º¯Êý²¢½«Æä·ÅÈ뻺´æÖС£×ÝÈ»»º´æÓâÆÚ£¬Ò²½«Ê¹Óûº´æµÄÊý¾Ý¡£
½áÂÛ
Laravel CacheÊÇÒ»¸öÇ¿Ê¢µÄ»º´æ½â¾ö¼Æ»®£¬¿ÉÒÔ×ÊÖúÌá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄÜ¡£±¾ÎÄÏÈÈÝÁËLaravel CacheµÄ¿´·¨ºÍÓ÷¨£¬²¢ÌṩÁËʾÀý´úÂëÀ´×ÊÖú¶ÁÕßÏàʶÔõÑùʹÓÃLaravel CacheʵÏÖ»º´æ¡£Í¨¹ýʹÓÃLaravel Cache£¬¿ª·¢Ö°Ô±¿ÉÒÔ´ó´óÌá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄÜ£¬²¢Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel CacheʵÏÖ»º´æ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡