VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP 数据库缓存Memcache操作类

操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了一个Memcache数据缓存操作类库文件,希望对各位会有帮助了.

PHP 数据库缓存Memcache操作类代码如下:

  1. class memcachedInit { 
  2. private $memcache; 
  3. /** 
  4.  * Memcache缓存-设置缓存 
  5.  * 设置缓存key,value和缓存时间 
  6.  * @param  string $key   KEY值 
  7.  * @param  string $value 值 
  8.  * @param  string $time  缓存时间 
  9.  */ 
  10. public function set_cache($key, $value, $time = 0) {  
  11. return $this->memcache->set($key, $value, false, $time); 
  12. } 
  13. /** 
  14.  * Memcache缓存-获取缓存 
  15.  * 通过KEY获取缓存数据 
  16.  * @param  string $key   KEY值 
  17.  */ 
  18. public function get_cache($key) { 
  19. return $this->memcache->get($key); 
  20. } 
  21. /** 
  22.  * Memcache缓存-清除一个缓存 
  23.  * 从memcache中删除一条缓存 
  24.  * @param  string $key   KEY值 
  25.  */ 
  26. public function clear($key) { 
  27. return $this->memcache->delete($key); 
  28. } 
  29. /** 
  30.  * Memcache缓存-清空所有缓存 
  31.  * 不建议使用该功能 
  32.  * @return 
  33.  */ 
  34. public function clear_all() { 
  35. return $this->memcache->flush(); 
  36. } 
  37. /** 
  38.  * 字段自增-用于记数 
  39.  * @param string $key  KEY值 
  40.  * @param int    $step 新增的step值 
  41.  */ 
  42. public function  increment($key, $step = 1) { 
  43. return $this->memcache->increment($key, (int) $step); 
  44. } 
  45. /** 
  46.  * 字段自减-用于记数 
  47.  * @param string $key  KEY值 
  48.  * @param int    $step 新增的step值 
  49.  */ 
  50. public function decrement($key, $step = 1) { 
  51. return $this->memcache->decrement($key, (int) $step); 
  52. } 
  53. /** 
  54.  * 关闭Memcache链接 
  55.  */ 
  56. public function close() { 
  57. return $this->memcache->close(); 
  58. } 
  59. /** 
  60.  * 替换数据 
  61.  * @param string $key 期望被替换的数据 
  62.  * @param string $value 替换后的值 
  63.  * @param int    $time  时间值 
  64.  * @param bool   $flag  是否进行压缩 
  65.  */ 
  66. public function replace($key, $value, $time = 0, $flag = false) { 
  67. return $this->memcache->replace($key, $value, false, $time); 
  68. } 
  69. /** 
  70.  * 获取Memcache的版本号 
  71.  */ 
  72. public function getVersion() { 
  73. return $this->memcache->getVersion(); 
  74. } 
  75. /** 
  76.  * 获取Memcache的状态数据 
  77.  */ 
  78. public function getStats() { 
  79. return $this->memcache->getStats(); 
  80. } 
  81. /** 
  82.  * Memcache缓存-设置链接服务器 
  83.  * 支持多MEMCACHE服务器 
  84.  * 配置文件中配置Memcache缓存服务器: 
  85.  * $InitPHP_conf['memcache'][0]   = array('127.0.0.1', '11211');   
  86.  * @param  array $servers 服务器数组-array(array('127.0.0.1', '11211')) 
  87.  */ 
  88. public function add_server($servers) { 
  89. $this->memcache = new Memcache; 
  90. if (!is_array($servers) || emptyempty($servers)) exit('memcache server is null!');//开源代码phpfensi.com 
  91. foreach ($servers as $val) { 
  92. $this->memcache->addServer($val[0], $val[1]); 
  93. } 
  94. } 
  95. } 

使用方法:

$newclass = new memcachedInit();

$newclass->getVersion() //获取版本号

$newclass->close() //关闭Memcache链接

$newclass->clear($key) //从memcache中删除一条缓存 

$newclass->get_cache($key) //通过KEY获取缓存数据

上面就简单介绍了它的使用方法了,其实还有很多在这里我就不介绍了呀.

总结:这个只是一个最基于的Memcache缓存操作类了,比起像数据库缓存类操作会更好更复杂了,希望例子能帮助到各位朋友.

 

出处:http://www.phpfensi.com/php/20140910/5192.html


相关教程