尊龙凯时人生就是博

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²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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