Swoole½ø½×£ºÔõÑùʹÓÃг̾ÙÐи߲¢·¢Redis²Ù×÷
ÔÚÏÖ´úweb¿ª·¢ÖУ¬¸ß²¢·¢ÊÇÒ»Ïî²»¿É×èÖ¹µÄÌôÕ½¡£×÷ΪһÃû¿ª·¢Ö°Ô±£¬ÎªÁË°ü¹ÜÓ¦ÓóÌÐòµÄ¿ÉÓÃÐÔºÍÐÔÄÜ£¬ÎÒÃÇÐèҪʱ¿Ì¹Ø×¢²¢·¢²Ù×÷µÄЧÂʺÍÖÊÁ¿¡£
ÔÚ´ËÅä¾°Ï£¬SwooleгÌÊÖÒÕÓ¦Ô˶øÉú¡£Swoole¿ÉÒÔ×ÊÖúÎÒÃÇ´¦ÀíÒì²½ºÍ²¢·¢ÇëÇó£¬Ìá¸ß³ÌÐòÔËÐÐЧÂÊ¡£ÁíÍ⣬SwooleÖ§³Ö¶ÔµÚÈý·½×é¼þ¾ÙÐÐг̷â×°£¬ÕâΪÎÒÃǽâ¾ö¸ß²¢·¢µÄÌôÕ½ÌṩÁ˸ü¶àµÄÑ¡Ôñ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSwooleг̾ÙÐи߲¢·¢Redis²Ù×÷£¬ÈÃÎÒÃÇ×îÏÈ°É£¡
×°ÖÃSwooleÀ©Õ¹ºÍRedisÀ©Õ¹
ÔÚ¾ÙÐÐʹÓÃSwooleг̾ÙÐи߲¢·¢Redis²Ù×÷֮ǰ£¬ÎÒÃÇÐèÒªÏÈ×°ÖÃSwooleÀ©Õ¹ºÍRedisÀ©Õ¹¡£ÏêϸµÄ×°Öð취¿ÉÒԲο¼Swoole¹Ù·½ÎĵµºÍRedis¹Ù·½Îĵµ¡£
ÅþÁ¬Redis
ÔÚ¾ÙÐÐRedis²Ù×÷֮ǰ£¬ÎÒÃÇÐèÒª½¨ÉèÓëRedisЧÀÍÆ÷µÄÅþÁ¬¡£ÔÚʹÓÃSwooleг̾ÙÐи߲¢·¢²Ù×÷ʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃSwooleÌṩµÄг̿ͻ§¶ËʵÏÖÅþÁ¬²Ù×÷¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룺
use SwooleCoroutineRedis; $redis = new Redis(); $redis->connect('127.0.0.1', 6379);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öгÌRedis¿Í»§¶ËʵÀý£¬È»ºóŲÓÃconnect()ÒªÁìÅþÁ¬RedisЧÀÍÆ÷¡£
¾ÙÐÐRedis²Ù×÷
ÅþÁ¬ÀÖ³ÉÖ®ºó£¬ÎÒÃǾͿÉÒÔʹÓÃSwooleгÌRedis¿Í»§¶ËʵÏÖRedis²Ù×÷ÁË¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺
use SwooleCoroutineRedis; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // ÉèÖüüÖµ SwooleCoroutineun(function () use ($redis) { $result = $redis->set('my_key', 'my_value'); var_dump($result); }); // »ñÈ¡¼üÖµ SwooleCoroutineun(function () use ($redis) { $result = $redis->get('my_key'); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËSwooleCoroutineun()ÒªÁ콨ÉèÁËÁ½¸öг̣¬»®·Ö¾ÙÐÐÉèÖüüÖµºÍ»ñÈ¡¼üÖµµÄ²Ù×÷¡£ÕâÑùÎÒÃǾͿÉÒÔͬʱ¾ÙÐжà¸öRedis²Ù×÷£¬²¢ÇÒ²»»á±»ÛÕ±Õ¡£
·â×°Redisг̿ͻ§¶Ë
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇͨ³£ÐèÒª¶ÔRedisг̿ͻ§¶Ë¾ÙÐзâ×°£¬ÒÔ±ã¸üºÃµØ¾ÙÐÐÏîÄ¿¿ª·¢ºÍά»¤¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룺
namespace AppRedis; use SwooleCoroutineRedis; class RedisClient { private $redis; public function __construct() { $this->redis = new Redis(); $this->redis->connect('127.0.0.1', 6379); } public function get(string $key): string { return $this->redis->get($key); } public function set(string $key, string $value): bool { return $this->redis->set($key, $value); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪRedisClientµÄÀ࣬²¢ÔÚÆäÖзâ×°ÁËget()ºÍset()ÒªÁì¡£ÕâÑù£¬ÔÚÏÖʵÏîÄ¿¿ª·¢ÖУ¬ÎÒÃǾͿÉÒÔÖ±½ÓŲÓÃRedisClientÀàÖеÄÒªÁìʵÏÖRedis²Ù×÷ÁË¡£
ʹÓÃг̾ÙÐи߲¢·¢Redis²Ù×÷
ÏÖÔÚ£¬ÎÒÃÇÒѾ¾ß±¸ÁËʹÓÃSwooleг̾ÙÐÐRedis²Ù×÷µÄÄÜÁ¦£¬½ÓÏÂÀ´ÎÒÃÇÐèÒª½â¾ö¸ß²¢·¢µÄÌôÕ½¡£
ÔڹŰåµÄRedis²Ù×÷ÖУ¬ÎÒÃÇͨ³£Ê¹ÓöàÏ̻߳ò¶àÀú³ÌÀ´ÊµÏָ߲¢·¢¡£¿ÉÊÇ£¬ÔÚʹÓÃSwooleг̾ÙÐи߲¢·¢Redis²Ù×÷ʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃг̳ØÀ´ÊµÏָ߲¢·¢¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺
use SwooleCoroutineChannel; $pool_size = 10; $chan = new Channel($pool_size); for ($i = 0; $i < $pool_size; $i++) { go(function () use ($chan) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $chan->push($redis); }); } go(function () use ($chan) { $redis = $chan->pop(); $result = $redis->get('my_key'); var_dump($result); $chan->push($redis); }); go(function () use ($chan) { $redis = $chan->pop(); $result = $redis->set('my_key', 'my_value'); var_dump($result); $chan->push($redis); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öÈÝÁ¿Îª10µÄг̳ء£È»ºó£¬ÎÒÃÇʹÓÃgo()ÒªÁ쿪ÆôÁ½¸öг̻®·Ö¾ÙÐлñÈ¡¼üÖµºÍÉèÖüüÖµµÄ²Ù×÷¡£ÔÚгÌÖУ¬ÎÒÃÇÏÈ´Óг̳ØÖлñÈ¡Ò»¸öг̿ͻ§¶ËʵÀý£¬È»ºó¾ÙÐÐRedis²Ù×÷£¬×îºó½«Ð³Ì¿Í»§¶ËʵÀýÖØÐÂÍÆÈëг̳ØÖС£
ͨ¹ýʹÓÃг̳أ¬ÎÒÃÇ¿ÉÒÔͬʱ´¦Àí¶à¸öRedisÇëÇ󣬲¢ÇÒ²»»áÓÉÓÚÁè¼ÝRedis×î´óÅþÁ¬Êý¶ø±»ÛÕ±Õ¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃSwooleг̾ÙÐи߲¢·¢Redis²Ù×÷¡£ÎÒÃÇÊ×ÏÈÏÈÈÝÁËÅþÁ¬RedisµÄ·½·¨£¬È»ºóÑÝʾÁËÔõÑùʹÓÃSwooleгÌRedis¿Í»§¶Ë¾ÙÐÐRedis²Ù×÷¡£½ÓÏÂÀ´£¬ÎÒÃÇ·â×°ÁËRedisг̿ͻ§¶Ë£¬²¢ÇÒÏÈÈÝÁËÔõÑùʹÓÃг̳ؾÙÐи߲¢·¢Redis²Ù×÷¡£
ͨ¹ýʵÏÖÕâЩÊÖÒÕ£¬ÎÒÃÇ¿ÉÒÔ³ä·ÖʹÓÃSwooleг̺ÍRedisµÄÓÅÊÆ£¬Ìá¸ßÓ¦ÓóÌÐòµÄ²¢·¢ÐÔºÍÐÔÄÜ¡£
ÒÔÉϾÍÊÇSwoole½ø½×£ºÔõÑùʹÓÃг̾ÙÐи߲¢·¢Redis²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡