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)
*/

 

posted @ 2017-04-27 17:43  Chrdai  阅读(469)  评论(0编辑  收藏  举报