memcached.init.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-memcached 内存缓存
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. class memcachedInit {
  13. private $memcache;
  14. /**
  15. * Memcache缓存-设置缓存
  16. * 设置缓存key,value和缓存时间
  17. * @param string $key KEY值
  18. * @param string $value 值
  19. * @param string $time 缓存时间
  20. */
  21. public function set_cache($key, $value, $time = 0) {
  22. return $this->memcache->set($key, $value, false, $time);
  23. }
  24. /**
  25. * Memcache缓存-获取缓存
  26. * 通过KEY获取缓存数据
  27. * @param string $key KEY值
  28. */
  29. public function get_cache($key) {
  30. return $this->memcache->get($key);
  31. }
  32. /**
  33. * Memcache缓存-清除一个缓存
  34. * 从memcache中删除一条缓存
  35. * @param string $key KEY值
  36. */
  37. public function clear($key) {
  38. return $this->memcache->delete($key);
  39. }
  40. /**
  41. * Memcache缓存-清空所有缓存
  42. * 不建议使用该功能
  43. * @return
  44. */
  45. public function clear_all() {
  46. return $this->memcache->flush();
  47. }
  48. /**
  49. * 字段自增-用于记数
  50. * @param string $key KEY值
  51. * @param int $step 新增的step值
  52. */
  53. public function increment($key, $step = 1) {
  54. return $this->memcache->increment($key, (int) $step);
  55. }
  56. /**
  57. * 字段自减-用于记数
  58. * @param string $key KEY值
  59. * @param int $step 新增的step值
  60. */
  61. public function decrement($key, $step = 1) {
  62. return $this->memcache->decrement($key, (int) $step);
  63. }
  64. /**
  65. * 关闭Memcache链接
  66. */
  67. public function close() {
  68. return $this->memcache->close();
  69. }
  70. /**
  71. * 替换数据
  72. * @param string $key 期望被替换的数据
  73. * @param string $value 替换后的值
  74. * @param int $time 时间值
  75. * @param bool $flag 是否进行压缩
  76. */
  77. public function replace($key, $value, $time = 0, $flag = false) {
  78. return $this->memcache->replace($key, $value, false, $time);
  79. }
  80. /**
  81. * 获取Memcache的版本号
  82. */
  83. public function getVersion() {
  84. return $this->memcache->getVersion();
  85. }
  86. /**
  87. * 获取Memcache的状态数据
  88. */
  89. public function getStats() {
  90. return $this->memcache->getStats();
  91. }
  92. /**
  93. * Memcache缓存-设置链接服务器
  94. * 支持多MEMCACHE服务器
  95. * 配置文件中配置Memcache缓存服务器:
  96. * $InitPHP_conf['memcache'][0] = array('127.0.0.1', '11211');
  97. * @param array $servers 服务器数组-array(array('127.0.0.1', '11211'))
  98. */
  99. public function add_server($servers) {
  100. $this->memcache = new Memcache;
  101. if (!is_array($servers) || empty($servers)) exit('memcache server is null!');
  102. foreach ($servers as $val) {
  103. $this->memcache->addServer($val[0], $val[1]);
  104. }
  105. }
  106. }