ÏîÄ¿ÖÐÓ¦ÓÃRedis+PhpµÄ³¡¾°

5年以前  |  阅读数:322 次  |  编程语言:PHP 

Ç°ÑÔ

Ò»Ð(C)°¸ÀýÖÐÓеÄͬѧ˵Ϊʲô²»¿ÉÒÔÓÃstringÀaÐÍ£¬stringÀaÐÍÍeÈ«¿ÉÒÔʵÏÖѽ

ÎÒ½¨ÒeÄa¿´ÏÂÎÒµÄרÀ¸ÎÄÕ¡¶Redis¸ß¼¶Óè¡,ÀiÃae½eÉÜÁËÓÃhashÀaÐ͵ĺô¦

*ÉÌÆά¶È¼ÆÊý**

¶ÔÉÌÆϲ»¶Êý£¬ÆÀÂÛÊý£¬¼ø¶¨Êý£¬a¯ÀÀÊý½øÐмÆÊý
˵ÆðµçÉÌ£¬¿Ï¶¨Àe²»¿ªÉÌÆ
£¬¶ø¸½´øÉÌÆ*Óи÷ÖÖ¼ÆÊý£¨Ï²»¶Êý£¬ÆÀÂÛÊý£¬¼ø¶¨Êý£¬a¯ÀÀÊý,etc£(C)
RedisµÄÃuÁi¶¼ÊÇÔ­×ÓÐԵģ¬Äa¿ÉÒÔÇaËɵØÀuÓÃINCR£¬DECRµÈÃuÁiÀ´¼ÆÊý¡£

²ÉÓÃRedis µÄÀaÐÍ: Hash. Èç¹uÄa¶ÔredisÊý¾ÝÀaÐͲ»Ì«ÊiϤ£¬¿ÉÒԲο¼
http://redis.io/topics/data-types-intro

Ϊproduct¶¨Òa¸okey product:£¬ÎªÃ¿ÖÖÊýÖµ¶¨Òahashkey, Æ(C)Èçϲ»¶Êýlike_num


    $redis->hSet('product:123', 'like_num ', 5); // Ìi¼Ó idΪ123µÄÉÌÆ* like_num Ϊ5

    $redis->hIncrBy('product:123', 'like_num ', 1); // Ìi¼Ó idΪ123µÄÉÌÆ*like_num +1

    $redis->hGetAll('product:123'); // »ñÈ¡idΪ123µÄÉÌÆ*Ïa¹ØÐÅÏ¢
                      array('like_num '=> 1)

Óû§Î¬¶È¼ÆÊý

¶ÔÓû§¶¯Ì¬Êý¡¢¹Ø×¢Êý¡¢ÛË¿Êý¡¢Ï²»¶ÉÌÆÊý¡¢¢ÌuÊýµÈ¼ÆÊý
Óû§Î¬¶È¼ÆÊýͬÉÌÆ
ά¶È¼ÆÊý¶¼²ÉÓà Hash. ΪUser¶¨Òa¸okey Ϊ user:
ΪÿÖÖÊýÖµ¶¨Òahashkey, Æ(C)Èç¹Ø×¢Êýfollow


    $redis->hSet('user:100000', 'follow ', 5); // Ìi¼ÓuidΪ10000µÄÓû§follow Ϊ5

    $redis->hIncrBy('user:100000', 'follow ', 1); // ¸uÐÂuidΪ10000µÄÓû§follow +1

    $redis->hGetAll('user:100000'); // »ñÈ¡uidΪ10000µÄÓû§
                      array('like_num '=> 1)

´ae´¢Éç½»¹Øϵ

Æ(C)È罫ÓÃ'oµÄºÃÓÑ/*ÛË¿/¹Ø×¢£¬¿ÉÒÔ´aeÔÚÒ»¸osorted setÖУ¬score¿ÉÒÔÊÇtimestamp
ĬÈϼ¯ºÏ°´ÕÕscoreµÝÔoÅÅÐo
ÕaÑuÇoÁ½¸oÈ˵Ĺ²Í¬ºÃÓѵIJÙ×÷£¬¿ÉÄܾÍÖ»ÐeÒªÓÃÇo½»¼¯ÃuÁi¼´¿É


    $redis->zAdd('user:1000:follow', 1463557212, '1001'); 

                    #uidΪ1000Óû§¹Ø×¢uidΪ1001 , scoreÖµÉe¶¨Ê±¼a´Á1463557212

    $redis->zAdd('user:1000:follow', 1463557333, '1002'); 

    $redis->zAdd('user:2000:follow', 1463577568, '1001'); 

    $redis->zAdd('user:2000:follow', 1463896964, '1003');

                    #uidΪ2000Óû§¹Ø×¢1001ºÍ1003Óû§ , scoreÖµÉe¶¨Ê±¼a´Á

    $redis->zInter('com_fllow:1000:2000', array('user:1000:follow', 'user:2000:follow')); 

        #¶Ô¼¯ºÏ'user:1000:follow'ºÍ'user:2000:follow'È¡½»¼¯'com_fllow:1000:2000'
        #»ñµÃ¹²Í¬¹Ø×¢µÄuid 

    $redis->zRange('com_fllow:1000:2000',0,-1); // »ñÈ¡È«²¿¼¯ºÏÔªËØ
        #array('10001','10002')

ÓÃ×÷»º´ae´uÌaememcached

Ó¦ÓÃÓÚÉÌÆ*Áбi£¬ÆÀÂÛÁбi£¬@ÌaʾÁбi

Ïa¶Ômemcached ¼oµ¥µÄkey-value´ae´¢À´Ëµ£¬redisÖÚ¶aµÄÊý¾Ý½a¹¹£¨list,set,sorted set,hash,
etc£(C)

¿ÉÒÔ¸u*½±acache¸÷ÖÖÒµÎñÊý¾Ý£¬ÐÔÄÜÒ²²»ÑÇÓÚmemcached¡£
NOTE: RPUSH pagewviews.user: EXPIRE pagewviews.user: 60 //×¢ÒaÒªupdate timeout

*´spamϵͳ

Ó¦ÓÃϵͳÆÀÂÛ¡¢¢²¼ÉÌÆ¡¢ÂÛ̳*¢ÌuµÄspam¿ØÖÆ

×÷Ϊһ¸oµçÉÌÍøÕ¾±»¸÷ÖÖspam¹¥»÷ÊÇÉÙ²»Ãa£¨À¬»øÆÀÂÛ¡¢¢²¼À¬»øÉÌÆ¡¢¹a¸ae¡¢Ë¢×Ô¼ÒÉÌÆ*ÅÅÃuµÈ£(C)

Õe¶ÔÕaÐ(C)spamÖƶ¨Ò»ÏµÁÐanti-spam¹aeÔo£¬ÆaÖÐÓÐÐ(C)¹aeÔo¿ÉÒÔÀuÓÃredis×oʵʱ*ÖÎo

Æ(C)È磺1ÖÖÓÆÀÂÛ²»µÃ³¬¹ý2´Î¡¢5ÖÖÓÆÀÂÛÉÙÓÚ5´ÎµÈ£¨¸u¶a»uÖÆ/¹aeÔoÐeÒª½aºÏdrools £(C)
³£¹aesorted set½«×i½uÒ»ÌiÓû§²Ù×÷¼Ç¼ÆðÀ´
£¨ÎªÊ²Ã´²»È«²¿¼Ç¼£¿½ÚÊ¡memory£¬È«²¿²Ù×÷»a¼Ç¼µ½log£¬ºoÐøÀuÓÃhadoop½øÐиuÈ«Ãae*ÖÎoͳ¼Æ£(C)


    #»ñÈ¡5ÃeÄÚ²Ù×÷¼Ç¼
    $res = $redis->zRangeByScore('user:1000:comment', time() - 5, time());
    #ÅжÏ5ÃeÄÚ²»ÄÜÆÀÂÛ
    if (!$res) {
      $redis->zAdd('user:1000:comment', time(), 'ÆÀÂÛÄÚÈÝ');
    } else {
      echo '5ÃeÖ®ÄÚ²»ÄÜÆÀÂÛ';
    }

    #5ÃeÄÚÆÀÂÛ²»µÃ³¬¹ý2´Î
    if($redis->zRangeByScore('user:1000:comment',time()-5 ,time())==1)
    echo '5ÃeÖ®ÄÚ²»ÄÜÆÀÂÛ2´Î';

    #5ÃeÄÚÆÀÂÛ²»µÃÉÙÓÚ2´Î

    if(count($redis->zRangeByScore('user:1000:comment',time()-5 ,time()))<2)
    echo '5ÃeÖ®ÄÚ²»ÄÜÆÀÂÛ2´Î';




Óû§Timeline/Feeds

Ó¦ÓÃÓÚ¹Ø×¢µÄÈË¡¢Ö÷Ìa¡¢Æ*ÅƼ°×¨À¸

redisÔÚÕa±ßÖ÷Òªµ±×÷cacheʹÓÃ


    $redis->zAdd('user:2000:feed:topic', time(), '13');
    //score Ϊtimestamp uidΪ2000µÄÓû§¹Ø×¢tidΪ13µÄtopic

    $redis->expire('user:2000:feed:topic',24*60*60);
    #¹Ø×¢ÓÐЧÆÚΪ24Сʱ
    # ttl 30ÌiÖ®ÄÚ°´ÃeÊý¼ÆËa 30ÌiÖ®ÍaÒÔtimestampΪ׼

×iÐÂÁбi&ÅÅÐаñ

ÓÃÓڼǼÓû§¸Õ¸Õϲ»¶µÄÉÌÆ*×iÐÂÁбiorÅÅÐаñ µÈÒµÎñ³¡¾°

ÉÌÆ*×iÐÂÁбi-sorted set½a¹¹³ÊÏÖ


        $redis->zAdd('user:1000:product:like', time(), '3002');
        $redis->zAdd('user:1000:product:like', time(), '3001');
        $redis->zAdd('user:1000:product:like', time(), '3004');
        $redis->zAdd('user:1000:product:like', time(), '3003');

        $redis->zRange('user:1000:product:like', 0, -1,true); 
        #ĬÈÏϲ»¶Ê±¼aÉýÐoÐoÅÅÁÐ
        #
          Array(
            [3002] => 1463565179
            [3001] => 1463565189
            [3004] => 1463565199
            [3003] => 1463565209
          )

        $redis->zRevRange('user:1000:product:like', 0, -1,true); 
        #ÒÔϲ»¶Ê±¼a½µÐoÅÅÁÐ
        #
          Array
          (
            [3003] => 1463565424
            [3004] => 1463565414
            [3001] => 1463565404
            [3002] => 1463565394
          )


ÅÅÐаñ-listÊý¾Ý½a¹¹³ÊÏÖ


        $redis->lPush('user:1000:product:like', '3002');
        $redis->lPush('user:1000:product:like', '3001');
        $redis->lPush('user:1000:product:like', '3004');
        $redis->lPush('user:1000:product:like', '3003');

        $redis->lRange('user:1000:product:like', 0, -1);

        Array
        (
          [0] => 3003
          [1] => 3004
          [2] => 3001
          [3] => 3002
        )

ÏuϢ֪ͨ

²ÉÓÃHash½a¹¹¶ÔÏuϢ֪ͨҵÎñ³¡¾°¼ÆÊý


    $redis->hSet('user:1000:message:notice', 'system', 1);
    #ÉeÖÃ1Ìoδ¶ÁϵͳÏuÏ¢

    $redis->hIncrBy('user:1000:message:notice', 'system', 1);
    #δ¶ÁϵͳÏuÏ¢+1

    $redis->hSet('user:1000:message:notice', 'comment', 1);
    #ÉeÖÃ1Ìoδ¶ÁÆÀÂÛ

    $redis->hIncrBy('user:1000:message:notice', 'comment', 1);
    #δ¶ÁÆÀÂÛ+1

    $redis->hGetAll('user:1000:message:notice');
    #²e¿´ËuÓÐÏuϢ֪ͨÊýÁ¿

    Array
    (
      [system] => 2
      [comment] => 2
    )



½«RedisÓÃ×÷ÏuÏ¢¶ÓÁÐ

²ÉÓÃRedisµÄListÊý¾Ý½a¹¹ÊµÏÖ*Ö²¼Ê½µÄÏuÏ¢¶ÓÁÐ

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
将二进制数据转为16进制以便显示
获取IMSI
获取IMEI
Java生成UUID
PHP自定义函数获取搜索引擎来源关键字的方法
让你成为最历害的git提交人
在Zeus Web Server中安装PHP语言支持
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php+ajax+json 详解及实例代码
Yii2汉字转拼音类的实例代码
php封装的page分页类完整实例
php数组合并array_merge()函数使用注意事项
PHP设计模式之工厂模式与单例模式
PHP实现简单爬虫的方法
php实现数组中索引关联数据转换成json对象的方法
wget使用技巧