xpdomemcached.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 memcached-powered xPDOCache implementation.
  22. *
  23. * This requires the memcached extension for PHP.
  24. *
  25. * @package xpdo
  26. * @subpackage cache
  27. */
  28. class xPDOMemCached extends xPDOCache {
  29. protected $memcached = null;
  30. public function __construct(& $xpdo, $options = array()) {
  31. parent :: __construct($xpdo, $options);
  32. if (class_exists('Memcached', true)) {
  33. $this->memcached = new Memcached();
  34. if ($this->memcached) {
  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->memcached->addServer($server[0], (integer) $server[1]);
  39. }
  40. $this->memcached->setOption(Memcached::OPT_COMPRESSION, (boolean) $this->getOption($this->key . '_memcached_compression', $options, $this->getOption('memcached_compression', $options, $this->getOption(Memcached::OPT_COMPRESSION, $options, true))));
  41. $this->initialized = true;
  42. } else {
  43. $this->memcached = null;
  44. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOMemCached[{$this->key}]: Error creating memcached provider for server(s): " . $this->getOption($this->key . '_memcached_server', $options, $this->getOption('memcached_server', $options, 'localhost:11211')));
  45. }
  46. } else {
  47. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOMemCached[{$this->key}]: Error creating memcached provider; xPDOMemCached requires the PHP memcached extension.");
  48. }
  49. }
  50. public function add($key, $var, $expire= 0, $options= array()) {
  51. $added= $this->memcached->add(
  52. $this->getCacheKey($key),
  53. $var,
  54. $expire
  55. );
  56. return $added;
  57. }
  58. public function set($key, $var, $expire= 0, $options= array()) {
  59. $set= $this->memcached->set(
  60. $this->getCacheKey($key),
  61. $var,
  62. $expire
  63. );
  64. return $set;
  65. }
  66. public function replace($key, $var, $expire= 0, $options= array()) {
  67. $replaced= $this->memcached->replace(
  68. $this->getCacheKey($key),
  69. $var,
  70. $expire
  71. );
  72. return $replaced;
  73. }
  74. public function delete($key, $options= array()) {
  75. if (!isset($options['multiple_object_delete']) || empty($options['multiple_object_delete'])) {
  76. $deleted= $this->memcached->delete($this->getCacheKey($key));
  77. } else {
  78. $deleted= $this->flush($options);
  79. }
  80. return $deleted;
  81. }
  82. public function get($key, $options= array()) {
  83. return $this->memcached->get($this->getCacheKey($key));
  84. }
  85. public function flush($options= array()) {
  86. return $this->memcached->flush();
  87. }
  88. }