PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Utils/Cache/APCcache.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 151 lines | 61 code | 15 blank | 75 comment | 10 complexity | 8d66dbf6e5458e9e4ab6d67cb034a20e MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface {
  33. // TODO Consider native implementation.
  34. use CRM_Utils_Cache_NaiveMultipleTrait;
  35. // TODO Native implementation
  36. use CRM_Utils_Cache_NaiveHasTrait;
  37. const DEFAULT_TIMEOUT = 3600;
  38. const DEFAULT_PREFIX = '';
  39. /**
  40. * The default timeout to use.
  41. *
  42. * @var int
  43. */
  44. protected $_timeout = self::DEFAULT_TIMEOUT;
  45. /**
  46. * The prefix prepended to cache keys.
  47. *
  48. * If we are using the same memcache instance for multiple CiviCRM
  49. * installs, we must have a unique prefix for each install to prevent
  50. * the keys from clobbering each other.
  51. *
  52. * @var string
  53. */
  54. protected $_prefix = self::DEFAULT_PREFIX;
  55. /**
  56. * Constructor.
  57. *
  58. * @param array $config
  59. * An array of configuration params.
  60. *
  61. * @return \CRM_Utils_Cache_APCcache
  62. */
  63. public function __construct(&$config) {
  64. if (isset($config['timeout'])) {
  65. $this->_timeout = intval($config['timeout']);
  66. }
  67. if (isset($config['prefix'])) {
  68. $this->_prefix = $config['prefix'];
  69. }
  70. }
  71. /**
  72. * @param $key
  73. * @param $value
  74. * @param null|int|\DateInterval $ttl
  75. *
  76. * @return bool
  77. */
  78. public function set($key, $value, $ttl = NULL) {
  79. CRM_Utils_Cache::assertValidKey($key);
  80. if (is_int($ttl) && $ttl <= 0) {
  81. return $this->delete($key);
  82. }
  83. $ttl = CRM_Utils_Date::convertCacheTtl($ttl, $this->_timeout);
  84. $expires = time() + $ttl;
  85. if (!apc_store($this->_prefix . $key, ['e' => $expires, 'v' => $value], $ttl)) {
  86. return FALSE;
  87. }
  88. return TRUE;
  89. }
  90. /**
  91. * @param $key
  92. * @param mixed $default
  93. *
  94. * @return mixed
  95. */
  96. public function get($key, $default = NULL) {
  97. CRM_Utils_Cache::assertValidKey($key);
  98. $result = apc_fetch($this->_prefix . $key, $success);
  99. if ($success && isset($result['e']) && $result['e'] > time()) {
  100. return $this->reobjectify($result['v']);
  101. }
  102. return $default;
  103. }
  104. /**
  105. * @param $key
  106. *
  107. * @return bool|string[]
  108. */
  109. public function delete($key) {
  110. CRM_Utils_Cache::assertValidKey($key);
  111. apc_delete($this->_prefix . $key);
  112. return TRUE;
  113. }
  114. public function flush() {
  115. $allinfo = apc_cache_info('user');
  116. $keys = $allinfo['cache_list'];
  117. // Our keys follows this pattern: ([A-Za-z0-9_]+)?CRM_[A-Za-z0-9_]+
  118. $prefix = $this->_prefix;
  119. // Get prefix length
  120. $lp = strlen($prefix);
  121. foreach ($keys as $key) {
  122. $name = $key['info'];
  123. if ($prefix == substr($name, 0, $lp)) {
  124. // Ours?
  125. apc_delete($name);
  126. }
  127. }
  128. return TRUE;
  129. }
  130. public function clear() {
  131. return $this->flush();
  132. }
  133. private function reobjectify($value) {
  134. return is_object($value) ? unserialize(serialize($value)) : $value;
  135. }
  136. }