xpdomemcache.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * Copyright 2010-2015 by MODX, LLC.
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * Provides a memcache-powered xPDOCache implementation.
  22. *
  23. * This requires the memcache extension for PHP.
  24. *
  25. * @package xpdo
  26. * @subpackage cache
  27. */
  28. class xPDOMemCache extends xPDOCache {
  29. protected $memcache = null;
  30. public function __construct(& $xpdo, $options = array()) {
  31. parent :: __construct($xpdo, $options);
  32. if (class_exists('Memcache', true)) {
  33. $this->memcache= new Memcache();
  34. if ($this->memcache) {
  35. $servers = explode(',', $this->getOption($this->key . '_memcached_server', $options, $this->getOption('memcached_server', $options, 'localhost:11211')));
  36. foreach ($servers as $server) {
  37. $server = explode(':', $server);
  38. $this->memcache->addServer($server[0], (integer) $server[1]);
  39. }
  40. $compressThreshold = $this->getOption($this->key . '_memcached_compress_threshold', $options, $this->getOption('memcached_compress_threshold', array(), '20000:0.2'));
  41. if (!empty($compressThreshold)) {
  42. $threshold = explode(':', $compressThreshold);
  43. if (count($threshold) == 2) {
  44. $minValue = (integer) $threshold[0];
  45. $minSaving = (float) $threshold[1];
  46. if ($minSaving >= 0 && $minSaving <= 1) {
  47. $this->memcache->setCompressThreshold($minValue, $minSaving);
  48. }
  49. }
  50. }
  51. $this->initialized = true;
  52. } else {
  53. $this->memcache = null;
  54. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOMemCache[{$this->key}]: Error creating memcache provider for server(s): " . $this->getOption($this->key . '_memcached_server', $options, $this->getOption('memcached_server', $options, 'localhost:11211')));
  55. }
  56. } else {
  57. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOMemCache[{$this->key}]: Error creating memcache provider; xPDOMemCache requires the PHP memcache extension.");
  58. }
  59. }
  60. public function add($key, $var, $expire= 0, $options= array()) {
  61. $added= $this->memcache->add(
  62. $this->getCacheKey($key),
  63. $var,
  64. $this->getOption($this->key . xPDO::OPT_CACHE_COMPRESS, $options, $this->getOption(xPDO::OPT_CACHE_COMPRESS, $options, false)),
  65. $expire
  66. );
  67. return $added;
  68. }
  69. public function set($key, $var, $expire= 0, $options= array()) {
  70. $set= $this->memcache->set(
  71. $this->getCacheKey($key),
  72. $var,
  73. $this->getOption($this->key . xPDO::OPT_CACHE_COMPRESS, $options, $this->getOption(xPDO::OPT_CACHE_COMPRESS, $options, false)),
  74. $expire
  75. );
  76. return $set;
  77. }
  78. public function replace($key, $var, $expire= 0, $options= array()) {
  79. $replaced= $this->memcache->replace(
  80. $this->getCacheKey($key),
  81. $var,
  82. $this->getOption($this->key . xPDO::OPT_CACHE_COMPRESS, $options, $this->getOption(xPDO::OPT_CACHE_COMPRESS, $options, false)),
  83. $expire
  84. );
  85. return $replaced;
  86. }
  87. public function delete($key, $options= array()) {
  88. if (!isset($options['multiple_object_delete']) || empty($options['multiple_object_delete'])) {
  89. $deleted= $this->memcache->delete($this->getCacheKey($key));
  90. } else {
  91. $deleted= $this->flush($options);
  92. }
  93. return $deleted;
  94. }
  95. public function get($key, $options= array()) {
  96. $value= $this->memcache->get($this->getCacheKey($key));
  97. return $value;
  98. }
  99. public function flush($options= array()) {
  100. return $this->memcache->flush();
  101. }
  102. }