• PHP中利用Redis管道加快执行


    $redis->muti($mode)->get($key)->set($key)->exec();
     既然是这样的, 也就是说当我要使用管道执行一万次操作的时候需要写一万次操作在muti()的后面,,,还是我找到更好的写法?
    设计者没有想到这个问题么?今天测试成功了

    [php] view plaincopy
     
    1. <?  
    2. php   $redis = new Redis();   
    3. $redis->connect('10.1.132.86', 6379);    
    4. $pipe = $redis->multi(Redis::PIPELINE);   
    5. for ($i = 0; $i <  10000; $i++) {   
    6.     $pipe->set("key::$i", str_pad($i, 4, '0', 0));   
    7.     $pipe->get("key::$i");   
    8. }   
    9.       
    10.     $replies = $pipe->exec(); echo " "; print_r($replies);   

    Description: Enter and exit transactional mode.

    Parameters

    (optional) Redis::MULTI or Redis::PIPELINE. Defaults toRedis::MULTI. A Redis::MULTI block of commands runs as a single transaction; aRedis::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity.discard cancels a transaction.

    Return value

    multi() returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object untilexec() is called.

    Example
    $ret = $redis->multi()
        ->set('key1', 'val1')
        ->get('key1')
        ->set('key2', 'val2')
        ->get('key2')
        ->exec();
    
    /*
    $ret == array(
        0 => TRUE,
        1 => 'val1',
        2 => TRUE,
        3 => 'val2');
    */
    

    ##############

    简单数据mget也可以实现

    mGet, getMultiple


    Description: Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSEat the position of the key.

    Parameters

    Array: Array containing the list of the keys

    Return value

    Array: Array containing the values related to keys in argument

    Examples
    $redis->set('key1', 'value1');
    $redis->set('key2', 'value2');
    $redis->set('key3', 'value3');
    $redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
    $redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value2', `FALSE`);
  • 相关阅读:
    renderer:function参数介绍
    JSF request参数传递
    jsf初学解决faces 中文输入乱码问题
    JSF中获得HTTP SESSION和Request
    jquery、js调用iframe父窗口与子窗口元素的方法整理
    android 手势识别学习
    JAVA事件监听机制学习
    我的android学习脚步----------- 的第一个应用
    图像处理之哈哈镜的实现
    图像处理知识随笔
  • 原文地址:https://www.cnblogs.com/php5/p/4263920.html
Copyright © 2020-2023  润新知