laravelÔõôʹÓûº´æ
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¹¤¾ßÀ´×ÊÖúÎÒÃǸüÇáËɵع¹½¨¸ßЧµÄwebÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄ¹¦Ð§ÊÇ»º´æ£¬Í¨¹ý½«Öظ´Ê¹ÓõÄÊý¾Ý»º´æÆðÀ´£¬¿ÉÒÔïÔÌÊý¾Ý¿âÅÌÎʺÍÌá¸ßÓ¦ÓóÌÐòµÄÏìÓ¦ËÙÂÊ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐʹÓûº´æ¡£
Laravel»º´æÇý¶¯
LaravelÌṩ¶àÖÖ»º´æÇý¶¯£¬°üÀ¨Îļþ¡¢Êý¾Ý¿â¡¢Redis¡¢MemcachedµÈ¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelʹÓÃÁËfileÇý¶¯£¬¼´½«»º´æÊý¾Ý´æ´¢ÔÚÎļþϵͳÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸ÄÉèÖÃÎļþÀ´Ñ¡ÔñÆäËû»º´æÇý¶¯¡£
ÉèÖÃÎļþλÖãºconfig/cache.php
ÀýÈ磬ÈôÊÇÎÒÃÇÏëʹÓÃRedis×÷Ϊ»º´æÇý¶¯£¬¿ÉÒÔƾ֤ÒÔÏ·½·¨ÐÞ¸ÄÉèÖÃÎļþ£º
‘default’ => ‘redis’,
…
‘redis’ => [
'driver' => 'redis', 'connection' => 'default',
µÇ¼ºó¸´ÖÆ
],
ʹÓûº´æ
µ±Ê¹ÓÃLaravel¾ÙÐÐÊý¾ÝÅÌÎÊʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý»º´æÀ´ïÔÌÊý¾Ý¿âÅÌÎÊ´ÎÊý¡£ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룺
$users = Cache::remember(‘users’, $minutes, function () {
return DB::table('users')->get();
µÇ¼ºó¸´ÖÆ
});
ÉÏÊö´úÂëʹÓÃCache::rememberÒªÁìÀ´»º´æÅÌÎÊЧ¹û¡£µÚÒ»¸ö²ÎÊýÊÇ»º´æµÄ¼üÃû£¬µÚ¶þ¸ö²ÎÊýÊÇ»º´æµÄʱ¼ä£¨ÒÔ·ÖÖÓΪµ¥Î»£©£¬µÚÈý¸ö²ÎÊýÊÇÒ»¸ö±Õ°üº¯Êý£¬ÓÃÓÚÅÌÎÊÊý¾Ý¿â²¢·µ»ØЧ¹û¡£ÈôÊÇ»º´æÖб£´æ¸Ã¼ü£¬Ôò·µ»Ø»º´æÖеÄÊý¾Ý£»²»È»£¬½«Ö´Ðбհüº¯Êý²¢½«Ð§¹û´æ´¢µ½»º´æÖС£
ÎÒÃÇ»¹¿ÉÒÔʹÓÃCache::putÒªÁìÖ±½Ó½«Êý¾Ý´æ´¢µ½»º´æÖУ¬È»ºóʹÓÃCache::getÒªÁì»ñÈ¡»º´æÖеÄÊý¾Ý¡£ÀýÈ磺
Cache::put(‘key’, ‘value’, $minutes);
$value = Cache::get(‘key’);
×Ô½ç˵»º´æÇý¶¯
ÈôÊÇLaravelÌṩµÄ»º´æÇý¶¯²»¿ÉÖª×ãÐèÇó£¬ÎÒÃÇ¿ÉÒÔͨ¹ý×Ô½ç˵»º´æÇý¶¯À´ÊµÏÖ×Ô¼ºµÄ»º´æ¼Æ»®¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
Ê×ÏÈ£¬ÔÚconfig/cache.phpÎļþÖнç˵еĻº´æÇý¶¯£º
‘custom’ => [
'driver' => 'custom', 'connection' => null,
µÇ¼ºó¸´ÖÆ
],
È»ºó£¬ÔÚapp/Providers/AppServiceProvider.phpÖÐ×¢²á×Ô½ç˵Çý¶¯£º
use IlluminateSupportFacadesCache;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register() { Cache::extend('custom', function ($app) { return new CustomCacheDriver(); }); }
µÇ¼ºó¸´ÖÆ
}
×îºó£¬ÔÚCustomCacheDriverÀàÖÐʵÏÖÏêϸµÄ»º´æÂß¼£º
use IlluminateContractsCacheStore;
class CustomCacheDriver implements Store
{
public function get($key) { // »ñÈ¡»º´æÊý¾Ý } public function put($key, $value, $seconds) { // ´æ´¢Êý¾Ýµ½»º´æ } // ÆäËûÒªÁì...
µÇ¼ºó¸´ÖÆ
}
×ܽá
±¾ÎÄÏÈÈÝÁËLaravelÖлº´æµÄ»ù±¾Ó÷¨ºÍ×Ô½ç˵»º´æÇý¶¯µÄÒªÁ졣ͨ¹ýºÏÀíµØʹÓûº´æ£¬¿ÉÒÔ´ó´óÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇÐèҪƾ֤ÏêϸÐèÇóÑ¡ÔñºÏÊʵĻº´æ¼Æ»®ºÍÓÅ»¯Õ½ÂÔ£¬´Ó¶ø×î´óÏ޶ȵØÊ©Õ¹»º´æµÄ×÷Óá£
ÒÔÉϾÍÊÇlaravelÔõôʹÓûº´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡