xpdoapccache.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 an APC-powered xPDOCache implementation.
  22. *
  23. * This requires the APC extension for PHP, version 3.1.4 or later. Earlier versions
  24. * did not have all the necessary user cache methods.
  25. *
  26. * @package xpdo
  27. * @subpackage cache
  28. */
  29. class xPDOAPCCache extends xPDOCache {
  30. public function __construct(& $xpdo, $options = array()) {
  31. parent :: __construct($xpdo, $options);
  32. if (function_exists('apc_exists')) {
  33. $this->initialized = true;
  34. } else {
  35. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOAPCCache[{$this->key}]: Error creating APC cache provider; xPDOAPCCache requires the APC extension for PHP, version 2.0.0 or later.");
  36. }
  37. }
  38. public function add($key, $var, $expire= 0, $options= array()) {
  39. $added= apc_add(
  40. $this->getCacheKey($key),
  41. $var,
  42. $expire
  43. );
  44. return $added;
  45. }
  46. public function set($key, $var, $expire= 0, $options= array()) {
  47. $set= apc_store(
  48. $this->getCacheKey($key),
  49. $var,
  50. $expire
  51. );
  52. return $set;
  53. }
  54. public function replace($key, $var, $expire= 0, $options= array()) {
  55. $replaced = false;
  56. if (apc_exists($key)) {
  57. $replaced= apc_store(
  58. $this->getCacheKey($key),
  59. $var,
  60. $expire
  61. );
  62. }
  63. return $replaced;
  64. }
  65. public function delete($key, $options= array()) {
  66. $deleted = false;
  67. if (!isset($options['multiple_object_delete']) || empty($options['multiple_object_delete'])) {
  68. $deleted= apc_delete($this->getCacheKey($key));
  69. } elseif (class_exists('APCIterator', true)) {
  70. $iterator = new APCIterator('user', '/^' . str_replace('/', '\/', $this->getCacheKey($key)) . '/', APC_ITER_KEY);
  71. if ($iterator) {
  72. $deleted = apc_delete($iterator);
  73. }
  74. }
  75. return $deleted;
  76. }
  77. public function get($key, $options= array()) {
  78. $value= apc_fetch($this->getCacheKey($key));
  79. return $value;
  80. }
  81. public function flush($options= array()) {
  82. $flushed = false;
  83. if (class_exists('APCIterator', true) && $this->getOption('flush_by_key', $options, true) && !empty($this->key)) {
  84. $iterator = new APCIterator('user', '/^' . str_replace('/', '\/', $this->key) . '\//', APC_ITER_KEY);
  85. if ($iterator) {
  86. $flushed = apc_delete($iterator);
  87. }
  88. } else {
  89. $flushed = apc_clear_cache('user');
  90. }
  91. return $flushed;
  92. }
  93. }