zoukankan      html  css  js  c++  java
  • memcache 在php中的用法

    memcached 的安装方法详见我博客的另一个页面:http://www.cnblogs.com/chrdai/p/6656443.html

    用法:

    一、memcache 连接命令:

    1、memcache :: addServer($host , $port , $weight=0);  // 向服务器池中增加一个服务器

    2、memcache :: connect($host , $port , $timeout);// 打开一个memcached服务端连接,$timeout为连接持续(超时)时间,一般不要做更改

    <?php
    /**
     * ***** memcache 打开一个memcached服务端链接
     */
    $mem = new memcache();
    $mem->connect('127.0.0.1',11211);
    echo $mem->getversion();    // 1.4.4-14-g9c660c0

    3、memcache :: pconnect($host , $port , $timeout);    //打开一个到服务器的持久化连接,$timeout为连接持续(超时)时间,一般不要做更改

    <?php
    /**
     * ***** memcache 打开一个到服务器的持久链接
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    echo $mem -> getversion();   // 1.4.4-14-g9c660c0

    4、memcache :: close();  // 这个函数不会关闭持久化连接, 持久化连接仅仅会在web服务器关机/重启时关闭

    <?php
    /**
     * ***** memcache 关闭memcached服务端链接
     */
    $mem = new memcache();
    $mem->connect('127.0.0.1',11211);
    $mem->close();

    二、memcache 存储命令:

    1、memcache :: set($key , $value , $expiration);   // 添加一个值,如果已经存在,则覆写, $expiration 为存储时间,默认为 0

    <?php
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> set('string','a simple string', false,3600);  // 为什么添加 false,请查看我博客另一个页面 http://www.cnblogs.com/chrdai/p/6774856.html

    2、memcache :: add($key , $value , $expiration);  // 添加一个值,如果已经存在,则返回false

    <?php
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    if( $mem->add("mystr","this is a memcache test!",false,3600))
    {
        echo  '原始数据缓存成功!';
    }
    else {
        echo '数据已存在:'.$mem->get("mystr");
    }

    3、memcache :: replace($key , $value , $expiration);  // 对一个已有的key进行覆写操作

    <?php
    /**
     * ***** memcache存储操作
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> replace('string','replace this string', false,3600);

    4、memcache :: append($key , $value );  // 向已存在元素后追加数据

    <?php
    /**
     * ***** memcache存储命令
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> set('foo','abc',false,3600);
    $mem -> append('foo','def');
    echo $mem -> get('foo');       // abcdef

    5、memcache :: prepend($key , $value );  // 向已存在元素前面追加数据

    <?php
    /**
     * ***** memcache存储命令
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> set('foo','abc',false,3600);
    $mem -> prepend('foo','def',false,3600);
    echo $mem -> get('foo');        // defabc

    三、memcache 查找命令:

    1、memcache :: get($key , [ $cache_cb , $cas_token] );  // 获取某个元素

    参数:

    key          // 要检索的元素的key。

    cache_cb      // 通读缓存回掉函数或NULL.

    cas_token     // 检索的元素的CAS标记值。

    <?php
    /**
     * ***** memcache查找命令
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> set('foo','abc',false,3600);
    echo $mem -> get('foo');           // abc

    四、memcache 删除命令:

    1、memcache::delete ($key );  // 删除某个元素

    <?php
    /**
     * ***** memcache删除命令
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    $mem -> set('foo','abc',false,3600);
    $mem -> delete('foo');
    echo $mem -> get('foo');        // 没有这个值了   

    2、memcache::flush();  // 使所有已经存在的元素失效。方法Memcache::flush() 并不会真正的释放任何资源,而是仅仅标记所有元素都失效了,因此已经被使用的内存会被新的元素复写

    <?php
    /**
     * ***** memcache 删除memcache中所有的元素
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    $mem -> set('foo','string',false,3600);
    $mem -> flush();
    echo $mem -> get('foo');   // 什么也没有了

    五、memcache 自增减命令:

    1memcache::increment($key , [$value=1]);  // 将$key 自增 $value ,$value默认为1

    <?php
    /**
     * ***** memcache 自增减命令
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    $mem -> set('counter',2,false,3600);
    $mem -> increment('counter',5);
    echo $mem -> get('counter');        // 7

    2memcache::decrement($key , [$value=1]);  // 将$key 自减 $value ,$value默认为1

    <?php
    /**
     * ***** memcache 自增减命令
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    $mem -> set('counter',10,false,3600);
    $mem -> decrement('counter',5);
    echo $mem -> get('counter');        // 5

    、memcache 查看服务器信息命令:

    1memcache::getVersion();  // 查看当前版本

    <?php
    /**
     * ***** memcache查看当前版本命令
     */
    $mem = new memcache();
    $mem->addServer('127.0.0.1',11211);
    echo $mem->getVersion();        //1.4.4-14-g9c660c0

    2memcache::getServerStatus($host , [$port]);  //  获取服务器的在线/离线状态,$port默认为11211

    <?php
    /**
     * ***** memcache 查看服务器是否在线命令
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    var_dump($mem -> getServerStatus('127.0.0.1', 11211));   // 1

    3memcache::getStats() //  获取服务器统计信息

    <?php
    /**
     * ***** memcache 查看服务器统计信息命令
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    var_dump($mem -> getStats());
    /*
    array (size=35)
      'pid' => string '10972' (length=5)
      'uptime' => string '3054797089' (length=10)
      'time' => string '252939981' (length=9)
      'version' => string '1.4.4-14-g9c660c0' (length=17)
      'pointer_size' => string '64' (length=2)
      'curr_connections' => string '12' (length=2)
      'total_connections' => string '35' (length=2)
      'connection_structures' => string '14' (length=2)
      'cmd_get' => string '173' (length=3)
      'cmd_set' => string '183' (length=3)
      'cmd_flush' => string '2' (length=1)
      'get_hits' => string '121' (length=3)
      'get_misses' => string '52' (length=2)
      'delete_misses' => string '0' (length=1)
      'delete_hits' => string '9' (length=1)
      'incr_misses' => string '4' (length=1)
      'incr_hits' => string '14' (length=2)
      'decr_misses' => string '0' (length=1)
      'decr_hits' => string '8' (length=1)
      'cas_misses' => string '1' (length=1)
      'cas_hits' => string '1' (length=1)
      'cas_badval' => string '0' (length=1)
      'auth_cmds' => string '0' (length=1)
      'auth_errors' => string '0' (length=1)
      'bytes_read' => string '9204' (length=4)
      'bytes_written' => string '14008' (length=5)
      'limit_maxbytes' => string '67108864' (length=8)
      'accepting_conns' => string '1' (length=1)
      'listen_disabled_num' => string '0' (length=1)
      'threads' => string '4' (length=1)
      'conn_yields' => string '0' (length=1)
      'bytes' => string '270' (length=3)
      'curr_items' => string '3' (length=1)
      'total_items' => string '154' (length=3)
      'evictions' => string '0' (length=1)
    */

    4memcache::getExtendedStats() //  获取缓存服务器池中所有服务器统计信息

    <?php
    /**
     * ***** memcache 获取缓存服务器池中所有服务器统计信息命令
     */
    $mem = new memcache();
    $mem->pconnect('127.0.0.1',11211);
    var_dump($mem -> getExtendedstats());
    /*
    array (size=1)
      '127.0.0.1:11211' =>
        array (size=35)
          'pid' => string '10972' (length=5)
          'uptime' => string '3054797301' (length=10)
          'time' => string '252940193' (length=9)
          'version' => string '1.4.4-14-g9c660c0' (length=17)
          'pointer_size' => string '64' (length=2)
          'curr_connections' => string '12' (length=2)
          'total_connections' => string '35' (length=2)
          'connection_structures' => string '14' (length=2)
          'cmd_get' => string '173' (length=3)
          'cmd_set' => string '183' (length=3)
          'cmd_flush' => string '2' (length=1)
          'get_hits' => string '121' (length=3)
          'get_misses' => string '52' (length=2)
          'delete_misses' => string '0' (length=1)
          'delete_hits' => string '9' (length=1)
          'incr_misses' => string '4' (length=1)
          'incr_hits' => string '14' (length=2)
          'decr_misses' => string '0' (length=1)
          'decr_hits' => string '8' (length=1)
          'cas_misses' => string '1' (length=1)
          'cas_hits' => string '1' (length=1)
          'cas_badval' => string '0' (length=1)
          'auth_cmds' => string '0' (length=1)
          'auth_errors' => string '0' (length=1)
          'bytes_read' => string '9211' (length=4)
          'bytes_written' => string '14758' (length=5)
          'limit_maxbytes' => string '67108864' (length=8)
          'accepting_conns' => string '1' (length=1)
          'listen_disabled_num' => string '0' (length=1)
          'threads' => string '4' (length=1)
          'conn_yields' => string '0' (length=1)
          'bytes' => string '270' (length=3)
          'curr_items' => string '3' (length=1)
          'total_items' => string '154' (length=3)
          'evictions' => string '0' (length=1)
    */
  • 相关阅读:
    sql被注入,用友不能建账
    项目总帐金额翻倍
    1)123104科目的余额出现翻倍情况,经调数据库,期初余额已调平,但余额表中的数仍是未调平前的错误数。2)一月结账时提示有一科目119101的总账与个人明细账不平....
    尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用友U8尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用sql替换T6工作流中的操作员
    解决win7科迈登录报错RASRDP MODULE已停止工作
    sql2005 64 位 连接 sql2000 32位
    jquery选择器
    深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
  • 原文地址:https://www.cnblogs.com/chrdai/p/6775379.html
Copyright © 2011-2022 走看看