Memcache.class.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @desc
  4. *
  5. * memcache class
  6. */
  7. class Mcache {
  8. public $is_compress = false;
  9. public $expire = false;
  10. public $cache;
  11. public function Mcache($g_memcahe_conf) {
  12. $this->is_compress = COMPRESSED ? MEMCACHE_COMPRESSED : false;
  13. $this->expire = MEM_EXPIRES ? MEM_EXPIRES : false;
  14. $this->cache = new Memcache();
  15. $this->connect($g_memcahe_conf);
  16. }
  17. /**
  18. * @desc
  19. * 添加Memcache地址, 打开一个到Memcache的连接
  20. */
  21. public function connect($g_memcahe_conf) {
  22. // multi-server config
  23. if(!empty($g_memcahe_conf[0])){
  24. foreach ($g_memcahe_conf as $conf){
  25. $this->cache->addServer($conf['server'], $conf['port'], $conf['per'], $conf['weight'], $conf['timeout'], $conf['retry']);
  26. }
  27. }
  28. // single server config
  29. else{
  30. $this->cache->addServer(
  31. $g_memcahe_conf['server'],
  32. $g_memcahe_conf['port'],
  33. $g_memcahe_conf['per'],
  34. $g_memcahe_conf['weight'],
  35. $g_memcahe_conf['timeout'],
  36. $g_memcahe_conf['retry']);
  37. }
  38. }
  39. /**
  40. * @desc
  41. * 向Memcache添加一个值,如果已经存在,则覆写
  42. */
  43. public function set($key, $val) {
  44. return $this->cache->set($key, $val, $this->is_compress,$this->expire);
  45. }
  46. /**
  47. * @desc
  48. * 添加一个值,如果已经存在,则返回false
  49. */
  50. public function add($key, $val) {
  51. return $this->cache->add($key, $val, $this->is_compress,$this->expire);
  52. }
  53. /**
  54. * @desc
  55. * 替换一个已经存在Memcache服务器上的项目(功能类似Memcache::set)
  56. */
  57. public function replace($key, $val) {
  58. return $this->cache->replace($key, $val, $this->is_compress,$this->expire);
  59. }
  60. /**
  61. * @desc
  62. * 删除一个Memcache上的key值
  63. */
  64. public function del($key) {
  65. return $this->cache->delete($key);
  66. }
  67. /**
  68. * @desc
  69. * 刷新所有Memcache上保存的项目
  70. */
  71. public function flush() {
  72. return $this->cache->flush();
  73. }
  74. /**
  75. * @desc
  76. * 从Memcache上获取一个key值
  77. *
  78. */
  79. public function get($key) {
  80. return $this->cache->get($key);
  81. }
  82. /**
  83. * @desc
  84. * 获取进程池中所有进程的运行系统统计
  85. */
  86. public function getStats($par="") {
  87. return $this->cache->getExtendedStats($par);
  88. }
  89. }
  90. ?>