• windows下安装memcached


    memcached官网
    http://www.danga.com/memcached/
    memcached windows官网
    http://jehiah.cz/projects/memcached-win32/

    memcached 1.2.0 for Win32为最新版,需libevent 1.2

    Unzip the binaries in your desired directory (eg. c:\memcached)
    Install the service using the command: 'c:\memcached\memcached.exe -d install' from either the command line
    Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
    Use the server, by default listening to port 11211
    Use 'memcached.exe -h' for extra help and command line server

    以后memcached将作为windows的一个服务每次开机时自动启动。
    在php.ini 去掉 'extension=php_memcache.dll'前的注释
    下载pecl的memcache模块包到ext目录
    NOTE: php和pecl的版本要一致。

    1. <?php
    2. $memcache_obj = new Memcache;

    3. /* connect to memcached server */
    4. $memcache_obj->connect('localhost', 11211);

    5. /*
    6. set value of item with key 'var_key', using on-the-fly compression
    7. expire time is 50 seconds
    8. */
    9. $memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);

    10. echo $memcache_obj->get('var_key');
    11. ?>

    显示"some really big variable"就是成功了

    memcached for Win32

    This is a port of memcached to the win32 architecture by Kronuz

    The win32 version of memcached can be run both as a NT Service or from the command line.
    To install memcached as a service, follow the next steps:

    1. Unzip the binaries in your desired directory (eg. c:\memcached)
    2. Install the service using the command: 'c:\memcached\memcached.exe -d install' from either the command line
    3. Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
    4. Use the server, by default listening to port 11211

    Use 'memcached.exe -h' for extra help and command line server
    You can check the ChangeLog to see what's new.

    PHP memcached 应用示例

    首先 下载 memcached-client.php,在下载了 memcached-client.php 之后,就可以通过这个文件中的类“memcached”对 memcached 服务进行操作了。其实代码调用非常简单,主要会用到的方法有 add()、get()、replace() 和 delete(),方法说明如下:

    add ($key, $val, $exp = 0)
    往 memcached 中写入对象,$key 是对象的唯一标识符,$val 是写入的对象数据,$exp 为过期时间,单位为秒,默认为不限时间;

    get ($key)
    从 memcached 中获取对象数据,通过对象的唯一标识符 $key 获取;

    replace ($key, $value, $exp=0)
    使用 $value 替换 memcached 中标识符为 $key 的对象内容,参数与 add() 方法一样,只有 $key 对象存在的情况下才会起作用;

    delete ($key, $time = 0)
    删除 memcached 中标识符为 $key 的对象,$time 为可选参数,表示删除之前需要等待多长时间。

    下面是一段简单的测试代码,代码中对标识符为 ‘mykey’ 的对象数据进行存取操作:

    以下是引用片段:
    <?php
    // 包含 memcached 类文件
    require_once('memcached-client.php');
    // 选项设置
    $options = array(
        'servers' => array('192.168.1.1:11211′), //memcached 服务的地址、端口,可用多个数组元素表示多个 memcached 服务
        'debug' => true, //是否打开 debug
        'compress_threshold' => 10240, //超过多少字节的数据时进行压缩
        'persistant' => false //是否使用持久连接
        );
    // 创建 memcached 对象实例
    $mc = new memcached($options);
    // 设置此脚本使用的唯一标识符
    $key = 'mykey';
    // 往 memcached 中写入对象
    $mc->add($key, 'some random strings');
    $val = $mc->get($key);
    echo "n".str_pad('$mc->add() ', 60, '_')."n";
    var_dump($val);
    // 替换已写入的对象数据值
    $mc->replace($key, array('some'=>'haha', 'array'=>'xxx'));
    $val = $mc->get($key);
    echo "n".str_pad('$mc->replace() ', 60, '_')."n";
    var_dump($val);
    // 删除 memcached 中的对象
    $mc->delete($key);
    $val = $mc->get($key);
    echo "n".str_pad('$mc->delete() ', 60, '_')."n";
    var_dump($val);
    ?>


    是不是很简单,在实际应用中,通常会把数据库查询的结果集保存到 memcached 中,下次访问时直接从 memcached 中获取,而不再做数据库查询操作,这样可以在很大程度上减轻数据库的负担。通常会将 SQL 语句 md5() 之后的值作为唯一标识符 key。下边是一个利用 memcached 来缓存数据库查询结果集的示例(此代码片段紧接上边的示例代码):

    以下是引用片段:
    <?php
    $sql = 'SELECT * FROM users';
    $key = md5($sql);   //memcached 对象标识符
    if ( !($datas = $mc->get($key)) ) {
        // 在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集。
        echo "n".str_pad('Read datas from MySQL.', 60, '_')."n";
        $conn = mysql_connect('localhost', 'test', 'test');
        mysql_select_db('test');
        $result = mysql_query($sql);
        while ($row = mysql_fetch_object($result))
            $datas[] = $row;
        // 将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。
        $mc->add($key, $datas);
    } else {
        echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
    }
    var_dump($datas);
    ?>

  • 相关阅读:
    matplotlib-形状
    matplotlib-区域填充
    C++文件操作
    画数学公式
    文字
    画注释
    Doubango简介-sip
    boost的asio接收单路大数据量udp包的方法
    Boost.Asio基本原理(CSDN也有Markdown了,好开森)
    boot asio 非阻塞同步编程---非阻塞的accept和receive。
  • 原文地址:https://www.cnblogs.com/rooney/p/2495476.html
Copyright © 2020-2023  润新知