ÔõÑùÔÚThinkPHP6ÖÐʹÓÃRedis¾ÙÐлº´æ²Ù×÷£¿
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÍøÕ¾µÄ»á¼ûÁ¿Ò²Ô½À´Ô½¸ß£¬Õâ¸øÍøÕ¾µÄÐÔÄÜ´øÀ´Á˺ܴóµÄÌôÕ½¡£ÎªÁËÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂÊ£¬Ðí¶àÍøÕ¾¶¼½ÓÄÉÁË»º´æÊÖÒÕ¡£¶øredis×÷ΪһÖÖ¸ßÐÔÄÜ¡¢ÄÚ´æ´æ´¢Êý¾Ý¿â£¬±»ÆÕ±éÓ¦ÓÃÓÚ»º´æ¡¢ÐÂÎÅÐÐÁС¢Î»ÖÃÐÅÏ¢µÈ³¡¾°¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùÔÚThinkPHP6ÖÐʹÓÃRedis¾ÙÐлº´æ²Ù×÷¡£
×°ÖÃRedisÀ©Õ¹
Ê×ÏÈ£¬ÐèÒªÔÚphp.iniÎļþÖÐÆôÓÃRedisÀ©Õ¹¡£¿ÉÒÔͨ¹ýÒÔÏ·½·¨×°ÖÃRedisÀ©Õ¹£º
pecl install redis
µÇ¼ºó¸´ÖÆ
ÈôÊÇÎÞ·¨ÀÖ³É×°Öã¬Ôò¿ÉÒÔÊÖ¶¯ÏÂÔØRedisÀ©Õ¹µÄÔ´Âë¾ÙÐÐ×°Öã¬ÏêϸҪÁìÇë²Î¿¼¹Ù·½Îĵµ¡£
×°ÖÃÍê³Éºó£¬ÔÚphp.iniÎļþÖÐÌí¼ÓÒ»ÐÐÉèÖãº
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
extension=redis.so
µÇ¼ºó¸´ÖÆ
ÖØÆôPHPºó¼´¿ÉʹÓÃRedis¡£
ÉèÖÃRedis
ÔÚThinkPHP6ÖУ¬¹ØÓÚRedisµÄÉèÖö¼ÔÚconfig/cache.phpÎļþÖС£Ê×ÏÈ£¬ÐèҪȷ±£cache.phpÎļþÖеÄRedisÇý¶¯±»ÆôÓãº
'default' => [ 'type' => 'redis', ... ]
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÐèÒªÉèÖÃRedisµÄÅþÁ¬ÐÅÏ¢£¬ÈçÏ£º
'default' => [ 'type' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', ],
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬’host’ÌåÏÖRedisµÄIPµØµã»òÖ÷»úÃû£¬’port’ÌåÏÖRedisµÄ¶Ë¿ÚºÅ£¬’password’ÌåÏÖÅþÁ¬RedisʱµÄÃÜÂ룬’select’ÌåÏÖÑ¡ÔñµÄÊý¾Ý¿â±àºÅ£¬’timeout’ÌåÏÖÅþÁ¬³¬Ê±Ê±¼ä£¬’expire’ÌåÏÖ»º´æÓÐÓÃʱ¼ä£¬’prefix’ÌåÏÖ»º´æǰ׺¡£
ʹÓÃRedis¾ÙÐлº´æ²Ù×÷
ÔÚÉèÖÃÍê³Éºó£¬¼´¿ÉʹÓÃRedis¾ÙÐлº´æ²Ù×÷¡£¹ØÓÚThinkPHP6ÖеĻº´æ²Ù×÷£¬¿ÉÒÔʹÓÃcacheº¯Êý¾ÙÐÐŲÓá£ÀýÈ磺
cache('key_name', 'value', 3600);
µÇ¼ºó¸´ÖÆ
Ôò»á½«’value’»º´æµ½RedisÖУ¬ÓÐÓÃÆÚΪ3600Ãë¡£ÈôÊÇÏëҪȡ³ö»º´æÊý¾Ý£¬¿ÉÒÔʹÓÃÒÔÏ·½·¨£º
$value = cache('key_name');
µÇ¼ºó¸´ÖÆ
¼´¿É´ÓRedisÖÐÈ¡³ö»º´æÊý¾Ý¡£
ͬʱ£¬»¹¿ÉÒÔʹÓÃRedisÌṩµÄÆäËûAPI¾ÙÐлº´æ²Ù×÷£¬ÀýÈçset¡¢getµÈÒªÁì¡£ÏêϸµÄ²Ù×÷Çë²Î¿¼Redis¹ÙÍøÎĵµ¡£
×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÐÅÍи÷ÈËÒѾÕÆÎÕÁËÔÚThinkPHP6ÖÐʹÓÃRedis¾ÙÐлº´æ²Ù×÷µÄÒªÁ졣ʹÓÃRedis¾ÙÐлº´æ¿ÉÒÔ´ó´óÌá¸ßÍøÕ¾µÄÐÔÄÜ£¬¼õÇáЧÀÍÆ÷µÄ¼ç¸º£¬ÊÇÏÖ´úÍøÕ¾±ØÐèµÄÊÖÒÕÖ®Ò»¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖÐʹÓÃRedis¾ÙÐлº´æ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡