尊龙凯时人生就是博

laravelµÄredisÓ÷¨

laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ª·¢¿ò¼Ü£¬ËüÈÙâß¡¶the 2020 laravel developers survey¡·×îÊܽӴýµÄ php ¿ò¼ÜÍ·ÏΡ£ÆäÖУ¬laravel µÄ redis ¹¦Ð§±»ÆÕ±éʹÓ㬿ÉÒÔ×ÊÖú¿ª·¢ÕßʵÏÖ¸ßЧµÄ»º´æºÍÐÐÁд¦Àí¡£

Redis ÊÇÒ»¸ö¿ªÔ´µÄÄÚ´æÊý¾Ý´æ´¢ÏµÍ³£¬ËüÊÇÒ»¸öÀàËÆÓÚ¼üÖµ¶ÔµÄ NoSQL Êý¾Ý¿â¡£Redis µÄÌصãÔÚÓÚËüÄܹ»Ö§³Ö¶àÖÖÊý¾Ý½á¹¹£¬Èç×Ö·û´®¡¢ÁÐ±í¡¢¹þÏ£¡¢ÜöÝÍ¡¢ÓÐÐòÜöÝ͵È£¬Í¬Ê±Ëü»¹¾ßÓиßÐÔÄÜ¡¢¸ß¿ÉÓÃÐÔ¡¢¿ÉÀ©Õ¹ÐÔºÍÒ×ÓÚʹÓõÈÓŵ㣬Òò´ËÔÚ¿ª·¢ÖлñµÃÁËÆÕ±éÓ¦Ó᣽ÓÏÂÀ´£¬ÎÒÃǽ«ÏêϸÏÈÈÝ Laravel µÄ Redis Ó÷¨¡£

Redis µÄÉèÖÃ

ÔÚ Laravel ÖÐʹÓà Redis ±ØÐèÒªÏȾÙÐÐÉèÖ᣷­¿ª .env Îļþ£¬ÕÒµ½ÒÔÏÂÉèÖÃÏ

CACHE_DRIVER=file
SESSION_DRIVER=file
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬CACHE_DRIVER ÌåÏÖ»º´æÇý¶¯£¬Ä¬ÈÏÖµÊÇ file£¬ÕâÀïÎÒÃÇÐèÒªÐÞ¸ÄΪ Redis£¬¼´£º

CACHE_DRIVER=redis

µÇ¼ºó¸´ÖÆ

SESSION_DRIVER ͬÀí¡£REDIS_HOST ÌåÏÖ Redis ЧÀ͵ĵص㣬ĬÒÔΪ±¾»úµÄ 127.0.0.1£¬REDIS_PASSWORD ÌåÏÖ Redis µÄÃÜÂ룬ÈôÊÇûÓÐÐèÒªÖÃΪ null¡£REDIS_PORT ÌåÏÖ Redis ЧÀͼàÌýµÄ¶Ë¿Ú£¬Ä¬ÒÔΪ 6379¡£

ΪÁ˲âÊÔÓ÷¨£¬ÔÚÕâÀïÎÒÃÇʹÓÃµÄ Redis ÊÇÔÚ±¾»ú×°ÖõÄ£¬Ã»ÓÐÉèÖÃÃÜÂë¡£ÈôÊÇÄúʹÓõÄÊÇÔ¶³Ì Redis£¬ÐèÒªÐÞ¸Ä REDIS_HOST ºÍ REDIS_PORT£¬Í¬Ê±Ò²ÒªÉèÖà REDIS_PASSWORD¡£

ʹÓà Redis ¾ÙÐлº´æ

ÔÚ Laravel ÖÐʹÓà Redis ¾ÙÐлº´æºÜÊǼòÆÓ¡£ÎÒÃÇÖ»ÐèҪʹÓà Cache ÀàÀ´»á¼û»º´æ£¬È»ºó½«ÆäÇý¶¯ÉèÖÃΪ Redis£¬ÕâÑù¼´¿ÉʹÓà Redis ¾ÙÐлº´æ¡£

»º´æÒ»¸öÖµ

ʹÓà Redis ¾ÙÐлº´æµÄ·½·¨Ðí¶à£¬ÕâÀïÎÒÃÇÊ×ÏÈÏÈÈÝ»º´æÒ»¸öÖµ¡£

use IlluminateSupportFacadesCache;

Cache::store('redis')->put('key', 'value', $seconds);

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëͨ¹ý Cache::store(‘redis’) »á¼ûÁË Redis »º´æ£¬½Ó×ÅʹÓà put ÒªÁ컺´æÁËÒ»¸ö key-value ¶Ô¡£ÆäÖУ¬$seconds ÌåÏÖ»º´æµÄÓÐÓÃÆÚ£¬µ¥Î»ÎªÃë¡£µ± $seconds µÄֵΪ null ʱ£¬»º´æ½«²»¹ýÆÚ¡£

»ñÈ¡Ò»¸öÖµ

»º´æÒ»¸öÖµµÄ×÷ÓÃÊÇÔÚºóÐøÄܹ»Ö±½Ó»ñÈ¡µ½¸ÃÖµ¡£ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄ Cache ÀàÀ´»ñÈ¡¸ÃÖµ£º

use IlluminateSupportFacadesCache;

$value = Cache::store('redis')->get('key');

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëͨ¹ý Cache::store(‘redis’) »á¼ûÁË Redis »º´æ£¬get ÒªÁì»ñÈ¡ÁË key ¶ÔÓ¦µÄ value Öµ¡£

ɾ³ýÒ»¸öÖµ

µ±ÎÒÃÇÐèҪɾ³ýij¸ö»º´æʱ£¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺

use IlluminateSupportFacadesCache;

Cache::store('redis')->forget('key');

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëͨ¹ý Cache::store(‘redis’) »á¼ûÁË Redis »º´æ£¬forget ÒªÁìɾ³ýÁ˶ÔÓ¦µÄ»º´æ¡£

ÅжÏÒ»¸ö»º´æÊÇ·ñ±£´æ

Åжϻº´æÊÇ·ñ±£´æ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏ´úÂ룺

use IlluminateSupportFacadesCache;

if (Cache::store('redis')->has('key')) {
    //
}

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëͨ¹ý Cache::store(‘redis’) »á¼ûÁË Redis »º´æ£¬has ÒªÁìÅжÏÁË key ¶ÔÓ¦µÄ value ÖµÊÇ·ñ±£´æ¡£

ʹÓà Redis ¾ÙÐÐÐÐÁд¦Àí

ÔÚ Laravel ÖÐʹÓà Redis ¾ÙÐÐÐÐÁд¦Àí£¬ÐèÒªÏÈÒýÈëÐÐÁÐЧÀÍÌṩÕß¡£·­¿ª config/app.php Îļþ£¬ÕÒµ½ providers Êý×飬Ìí¼ÓÈçÏ´úÂ룺

'providers' => [
    // ...
    IlluminateQueueQueueServiceProvider::class,
    IlluminateRedisRedisServiceProvider::class,
    // ...
],

µÇ¼ºó¸´ÖÆ

½Ó×Å£¬ÎÒÃÇÐèÒªÉèÖÃÐÐÁÐÅþÁ¬¡£·­¿ª config/queue.php Îļþ£¬ÐÞ¸ÄÈçÏÂÉèÖÃÏ

'connections' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => 'default',
        'retry_after' => 90,
        'block_for' => null,
    ],
],

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬driver ÌåÏÖʹÓà Redis Çý¶¯£»connection ÌåÏÖʹÓõĻº´æÅþÁ¬£»queue ÌåÏÖʹÓõÄÐÐÁÐÃû³Æ£»retry_after ÌåÏÖ±¬·¢Ê§°ÜʱµÄÖØÊÔʱ¼ä£¬µ¥Î»ÎªÃ룻block_for ÌåÏÖ»ñȡʹÃüʱµÄÛÕ±Õʱ¼ä£¬µ¥Î»ÎªÃë¡£

ÍÆËÍʹÃüµ½ÐÐÁÐ

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏ´úÂ뽫ʹÃüÍƵ½ÐÐÁÐÖУº

use IlluminateSupportFacadesQueue;

Queue::push(new SendEmailJob($user));

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëʹÓà Queue::push ÒªÁìÍÆËÍÁËÒ»¸ö SendEmailJob µ½Ä¬ÈÏÐÐÁÐÖС£ÆäÖУ¬SendEmailJob ÊÇÒ»¸ö×Ô½ç˵µÄʹÃüÀ࣬¿ÉÒÔʵÏÖ¸üÖØ´óµÄÂß¼­¡£

Ö´ÐÐÐÐÁÐʹÃü

ÍÆËÍʹÃüµ½ÐÐÁкó£¬ÎÒÃÇÐèҪʹÓÃÒÔÏÂÏÂÁîÖ´ÐÐʹÃü£º

php artisan queue:work

µÇ¼ºó¸´ÖÆ

ÉÏÊöÏÂÁÆô¶¯Ò»¸öÀú³ÌÀ´Ö´ÐÐÐÐÁÐʹÃü¡£ÈôÊÇÄúµÄЧÀÍÆ÷Óжà¸ö CPU ½¹µã£¬¿ÉÒÔͨ¹ýÔöÌí –daemon Ñ¡ÏîÀ´Æô¶¯¶à¸öÀú³ÌÀ´Ìá¸ßʹÃü´¦ÀíËÙÂÊ¡£

×ܽá

Laravel µÄ Redis ¹¦Ð§ÊǺÜÊÇÇ¿Ê¢µÄ£¬Í¨¹ýÓë Redis µÄÅäºÏ¿ÉÒÔʵÏÖÓÅÒìµÄ»º´æºÍÐÐÁд¦Àí¡£ÔÚ´¦Àí¸ß²¢·¢ºÍÖØ´óӪҵʱ£¬Ê¹Óà Redis »º´æºÍÐÐÁпÉÒÔÏÔÖøÌá¸ßϵͳµÄÐÔÄܺÍÎȹÌÐÔ¡£±¾ÎļòÆÓÏÈÈÝÁË Laravel µÄ Redis Ó÷¨£¬Ï£Íû¶ÔÄúµÄ¿ª·¢ÊÂÇéÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇlaravelµÄredisÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ