php的两个memcached扩展 memcache…

来源:互联网 发布:deepin linux 15.4.1 编辑:程序博客网 时间:2024/06/05 00:36

现在在php中memcached用的很多,以前一直使用的是php的memcache扩展,最近开始改用了php的memcached扩展(注意这里memcache和memcached扩展的名字就相差了一个d)。或许在google或者百度搜索php的memcached扩展的时候,很多结果是memcache.dll或者memcache.so,很少的结果是memcached.so,windows下面甚至没有memcached.dll扩展。
memcache扩展的下载地址为:http://pecl.php.net/package/memcache
memcached扩展的下载地址为:http://pecl.php.net/package/memcached
以上两个都是源码包。
这两个扩展都是用c写的,具体的来看看memcache扩展和memcached扩展在使用上到底有哪些差别。

加载memcache扩展之后,可以在php中直接使用Memcache类,Memcache类有以下一些方法:

 

[php] view plaincopy
  1. Memcache  
  2.     bool add string $key mixed $var [, int $flag [, int $expire ]]  
  3.    
  4.     bool addServer string $host [, int $port 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]]  
  5.    
  6.     bool close void  
  7.    
  8.     bool connect string $host [, int $port [, int $timeout ]]  
  9.    
  10.     int decrement string $key [, int $value  
  11.    
  12.     bool delete string $key [, int $timeout  
  13.    
  14.     bool flush void  
  15.    
  16.     string get string $key [, int &$flags  
  17.    
  18.     array getExtendedStats ([ string $type [, int $slabid [, int $limit 100 ]]]  
  19.    
  20.     int getServerStatus string $host [, int $port 11211  
  21.    
  22.     array getStats ([ string $type [, int $slabid [, int $limit 100 ]]]  
  23.    
  24.     string getVersion void  
  25.    
  26.     int increment string $key [, int $value  
  27.    
  28.     bool pconnect string $host [, int $port [, int $timeout ]]  
  29.    
  30.     bool replace string $key mixed $var [, int $flag [, int $expire ]]  
  31.    
  32.     bool set string $key mixed $var [, int $flag [, int $expire ]]  
  33.    
  34.     bool setCompressThreshold int $threshold [, float $min_savings  
  35.    
  36.     bool setServerParams string $host [, int $port 11211 [, int $timeout [, int $retry_interval false [, bool $status [, callback $failure_callback ]]]]]  
  37.  
[php] view plaincopy
  1. add — 增加一个条目到缓存服务器  
  2. addServer — 向连接池中添加一个memcache服务器  
  3. close — 关闭memcache连接  
  4. connect — 打开一个memcached服务端连接  
  5. decrement — 减小元素的值  
  6. delete — 从服务端删除一个元素  
  7. flush — 清洗(删除)已经存储的所有的元素  
  8. get — 从服务端检回一个元素  
  9. getExtendedStats — 缓存服务器池中所有服务器统计信息  
  10. getServerStatus — 用于获取一个服务器的在线/离线状态  
  11. getStats — 获取服务器统计信息  
  12. getVersion — 返回服务器版本信息  
  13. increment — 增加一个元素的值  
  14. pconnect — 打开一个到服务器的持久化连接  
  15. replace — 替换已经存在的元素的值  
  16. set — Store data at the server  
  17. setCompressThreshold — 开启大值自动压缩  
  18. setServerParams — 运行时修改服务器参数和状态  

加载memcached扩展之后,可以在php中直接使用Memcached类,Memcached类有以下一些方法:

 

 

[php] view plaincopy
  1. Memcached  
  2.     __construct ([ string $persistent_id  
  3.    
  4.     public bool add string $key mixed $value [, int $expiration  
  5.    
  6.     public bool addByKey string $server_key string $key mixed $value [, int $expiration  
  7.    
  8.     public bool addServer string $host int $port [, int $weight  
  9.    
  10.     public bool addServers array $servers  
  11.    
  12.     public bool append string $key string $value  
  13.    
  14.     public bool appendByKey string $server_key string $key string $value  
  15.    
  16.     public bool cas float $cas_token string $key mixed $value [, int $expiration  
  17.    
  18.     public bool casByKey float $cas_token string $server_key string $key mixed $value [, int $expiration  
  19.    
  20.     public int decrement string $key [, int $offset  
  21.    
  22.     public bool delete string $key [, int $time  
  23.    
  24.     public bool deleteByKey string $server_key string $key [, int $time  
  25.    
  26.     public array fetch void  
  27.    
  28.     public array fetchAll void  
  29.    
  30.     public bool flush ([ int $delay  
  31.    
  32.     public mixed get string $key [, callback $cache_cb [, float &$cas_token ]]  
  33.    
  34.     public mixed getByKey string $server_key string $key [, callback $cache_cb [, float &$cas_token ]]  
  35.    
  36.     public bool getDelayed array $keys [, bool $with_cas [, callback $value_cb ]]  
  37.    
  38.     public bool getDelayedByKey string $server_key array $keys [, bool $with_cas [, callback $value_cb ]]  
  39.    
  40.     public mixed getMulti array $keys [, array &$cas_tokens [, int $flags ]]  
  41.    
  42.     public array getMultiByKey string $server_key array $keys [, string &$cas_tokens [, int $flags ]]  
  43.    
  44.     public mixed getOption int $option  
  45.    
  46.     public int getResultCode void  
  47.    
  48.     public string getResultMessage void  
  49.    
  50.     public array getServerByKey string $server_key  
  51.    
  52.     public array getServerList void  
  53.    
  54.     public array getStats void  
  55.    
  56.     public array getVersion void  
  57.    
  58.     public int increment string $key [, int $offset  
  59.    
  60.     public bool prepend string $key string $value  
  61.    
  62.     public bool prependByKey string $server_key string $key string $value  
  63.    
  64.     public bool replace string $key mixed $value [, int $expiration  
  65.    
  66.     public bool replaceByKey string $server_key string $key mixed $value [, int $expiration  
  67.    
  68.     public bool set string $key mixed $value [, int $expiration  
  69.    
  70.     public bool setByKey string $server_key string $key mixed $value [, int $expiration  
  71.    
  72.     public bool setMulti array $items [, int $expiration  
  73.    
  74.     public bool setMultiByKey string $server_key array $items [, int $expiration  
  75.    
  76.     public bool setOption int $option mixed $value  

[php] view plaincopy
  1. add — 向一个新的key下面增加一个元素  
  2. addByKey — 在指定服务器上的一个新的key下增加一个元素  
  3. addServer — 向服务器池中增加一个服务器  
  4. addServers — 向服务器池中增加多台服务器  
  5. append — 向已存在元素后追加数据  
  6. appendByKey — 向指定服务器上已存在元素后追加数据  
  7. cas — 比较并交换值  
  8. casByKey — 在指定服务器上比较并交换值  
  9. __construct — 创建一个Memcached实例  
  10. decrement — 减小数值元素的值  
  11. delete — 删除一个元素  
  12. deleteByKey — 从指定的服务器删除一个元素  
  13. fetch — 抓取下一个结果  
  14. fetchAll — 抓取所有剩余的结果  
  15. flush — 作废缓存中的所有元素  
  16. get — 检索一个元素  
  17. getByKey — 从特定的服务器检索元素  
  18. getDelayed — 请求多个元素  
  19. getDelayedByKey — 从指定的服务器上请求多个元素  
  20. getMulti — 检索多个元素  
  21. getMultiByKey — 从特定服务器检索多个元素  
  22. getOption — 获取Memcached的选项值  
  23. getResultCode — 返回最后一次操作的结果代码  
  24. getResultMessage — 返回最后一次操作的结果描述消息  
  25. getServerByKey — 获取一个key所映射的服务器信息  
  26. getServerList — 获取服务器池中的服务器列表  
  27. getStats — 获取服务器池的统计信息  
  28. getVersion — 获取服务器池中所有服务器的版本信息  
  29. increment — 增加数值元素的值  
  30. prepend — 向一个已存在的元素前面追加数据  
  31. prependByKey — Prepend data to an existing item on specific server  
  32. replace — 替换已存在key下的元素  
  33. replaceByKey — Replace the item under an existing key on specific server  
  34. set — 存储一个元素  
  35. setByKey — Store an item on specific server  
  36. setMulti — 存储多个元素  
  37. setMultiByKey — Store multiple items on specific server  
  38. setOption — 设置一个memcached选项