php专区

 首页 > php专区 > PHP应用 > php类库 > PHP 数据库缓存Memcache操作类 - php类库

PHP 数据库缓存Memcache操作类 - php类库

分享到:
【字体:大 中 小】
导读:
          操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了一个M...

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缓存操作类了,比起像数据库缓存类操作会更好更复杂了,希望例子能帮助到各位朋友.

分享到:
php完整邮件发送类 - php类库
php完整邮件发送类 class smtp  {  /* Public Variables */  var $smtp_port;  var $time_out;  var $host_name;  var $log_file;  var $relay_host;  var $debug;  var $auth;  var $user;  var $pass;    /* Private Variables...
php类实现MySQL数据库备份、还原 - php类...
php类实现MySQL数据库备份、还原 这是一个非常简单的利用php来备份mysql数据库的类文件,我们只要简单的在dbmange中配置好连接地址用户名与数据库即可,下面我们一起来看这个例子,代码如下:
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……