First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,118 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface {
const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
/**
* The default timeout to use.
*
* @var int
*/
protected $_timeout = self::DEFAULT_TIMEOUT;
/**
* The prefix prepended to cache keys.
*
* If we are using the same memcache instance for multiple CiviCRM
* installs, we must have a unique prefix for each install to prevent
* the keys from clobbering each other.
*
* @var string
*/
protected $_prefix = self::DEFAULT_PREFIX;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_APCcache
*/
public function __construct(&$config) {
if (isset($config['timeout'])) {
$this->_timeout = intval($config['timeout']);
}
if (isset($config['prefix'])) {
$this->_prefix = $config['prefix'];
}
}
/**
* @param $key
* @param $value
*
* @return bool
*/
public function set($key, &$value) {
if (!apc_store($this->_prefix . $key, $value, $this->_timeout)) {
return FALSE;
}
return TRUE;
}
/**
* @param $key
*
* @return mixed
*/
public function get($key) {
return apc_fetch($this->_prefix . $key);
}
/**
* @param $key
*
* @return bool|string[]
*/
public function delete($key) {
return apc_delete($this->_prefix . $key);
}
public function flush() {
$allinfo = apc_cache_info('user');
$keys = $allinfo['cache_list'];
$prefix = $this->_prefix . "CRM_"; // Our keys follows this pattern: ([A-Za-z0-9_]+)?CRM_[A-Za-z0-9_]+
$lp = strlen($prefix); // Get prefix length
foreach ($keys as $key) {
$name = $key['info'];
if ($prefix == substr($name, 0, $lp)) {
// Ours?
apc_delete($this->_prefix . $name);
}
}
}
}

View file

@ -0,0 +1,85 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Class CRM_Utils_Cache_Arraycache
*/
class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface {
/**
* The cache storage container, an in memory array by default
*/
protected $_cache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_Arraycache
*/
public function __construct($config) {
$this->_cache = array();
}
/**
* @param string $key
* @param mixed $value
*/
public function set($key, &$value) {
$this->_cache[$key] = $value;
}
/**
* @param string $key
*
* @return mixed
*/
public function get($key) {
return CRM_Utils_Array::value($key, $this->_cache);
}
/**
* @param string $key
*/
public function delete($key) {
unset($this->_cache[$key]);
}
public function flush() {
unset($this->_cache);
$this->_cache = array();
}
}

View file

@ -0,0 +1,89 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*
* CRM_Utils_Cache_Interface
*
* PHP-FIG has been developing a draft standard for caching,
* PSR-6. The standard has not been ratified yet. When
* making changes to this interface, please take care to
* avoid *conflicst* with PSR-6's CacheItemPoolInterface. At
* time of writing, they do not conflict. Avoiding conflicts
* will enable more transition paths where Civi
* simultaneously supports both interfaces in the same
* implementation.
*
* For example, the current interface defines:
*
* function get($key) => mixed $value
*
* and PSR-6 defines:
*
* function getItem($key) => ItemInterface $item
*
* These are different styles (e.g. "weak item" vs "strong item"),
* but the two methods do not *conflict*. They can coexist,
* and you can trivially write adapters between the two.
*
* @see https://github.com/php-fig/fig-standards/blob/master/proposed/cache.md
*/
interface CRM_Utils_Cache_Interface {
/**
* Set the value in the cache.
*
* @param string $key
* @param mixed $value
*/
public function set($key, &$value);
/**
* Get a value from the cache.
*
* @param string $key
* @return mixed
* NULL if $key has not been previously set
*/
public function get($key);
/**
* Delete a value from the cache.
*
* @param string $key
*/
public function delete($key);
/**
* Delete all values from the cache.
*/
public function flush();
}

View file

@ -0,0 +1,148 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
/**
* The host name of the memcached server.
*
* @var string
*/
protected $_host = self::DEFAULT_HOST;
/**
* The port on which to connect on.
*
* @var int
*/
protected $_port = self::DEFAULT_PORT;
/**
* The default timeout to use.
*
* @var int
*/
protected $_timeout = self::DEFAULT_TIMEOUT;
/**
* The prefix prepended to cache keys.
*
* If we are using the same memcache instance for multiple CiviCRM
* installs, we must have a unique prefix for each install to prevent
* the keys from clobbering each other.
*
* @var string
*/
protected $_prefix = self::DEFAULT_PREFIX;
/**
* The actual memcache object.
*
* @var Memcache
*/
protected $_cache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_Memcache
*/
public function __construct($config) {
if (isset($config['host'])) {
$this->_host = $config['host'];
}
if (isset($config['port'])) {
$this->_port = $config['port'];
}
if (isset($config['timeout'])) {
$this->_timeout = $config['timeout'];
}
if (isset($config['prefix'])) {
$this->_prefix = $config['prefix'];
}
$this->_cache = new Memcache();
if (!$this->_cache->connect($this->_host, $this->_port)) {
// dont use fatal here since we can go in an infinite loop
echo 'Could not connect to Memcached server';
CRM_Utils_System::civiExit();
}
}
/**
* @param $key
* @param $value
*
* @return bool
*/
public function set($key, &$value) {
if (!$this->_cache->set($this->_prefix . $key, $value, FALSE, $this->_timeout)) {
return FALSE;
}
return TRUE;
}
/**
* @param $key
*
* @return mixed
*/
public function &get($key) {
$result = $this->_cache->get($this->_prefix . $key);
return $result;
}
/**
* @param $key
*
* @return mixed
*/
public function delete($key) {
return $this->_cache->delete($this->_prefix . $key);
}
/**
* @return mixed
*/
public function flush() {
return $this->_cache->flush();
}
}

View file

@ -0,0 +1,170 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface {
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
const MAX_KEY_LEN = 62;
/**
* The host name of the memcached server
*
* @var string
*/
protected $_host = self::DEFAULT_HOST;
/**
* The port on which to connect on
*
* @var int
*/
protected $_port = self::DEFAULT_PORT;
/**
* The default timeout to use
*
* @var int
*/
protected $_timeout = self::DEFAULT_TIMEOUT;
/**
* The prefix prepended to cache keys.
*
* If we are using the same memcache instance for multiple CiviCRM
* installs, we must have a unique prefix for each install to prevent
* the keys from clobbering each other.
*
* @var string
*/
protected $_prefix = self::DEFAULT_PREFIX;
/**
* The actual memcache object.
*
* @var Memcached
*/
protected $_cache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_Memcached
*/
public function __construct($config) {
if (isset($config['host'])) {
$this->_host = $config['host'];
}
if (isset($config['port'])) {
$this->_port = $config['port'];
}
if (isset($config['timeout'])) {
$this->_timeout = $config['timeout'];
}
if (isset($config['prefix'])) {
$this->_prefix = $config['prefix'];
}
$this->_cache = new Memcached();
if (!$this->_cache->addServer($this->_host, $this->_port)) {
// dont use fatal here since we can go in an infinite loop
echo 'Could not connect to Memcached server';
CRM_Utils_System::civiExit();
}
}
/**
* @param $key
* @param $value
*
* @return bool
* @throws Exception
*/
public function set($key, &$value) {
$key = $this->cleanKey($key);
if (!$this->_cache->set($key, $value, $this->_timeout)) {
CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage());
CRM_Core_Error::fatal("memcached set failed, wondering why?, $key", $value);
return FALSE;
}
return TRUE;
}
/**
* @param $key
*
* @return mixed
*/
public function &get($key) {
$key = $this->cleanKey($key);
$result = $this->_cache->get($key);
return $result;
}
/**
* @param $key
*
* @return mixed
*/
public function delete($key) {
$key = $this->cleanKey($key);
return $this->_cache->delete($key);
}
/**
* @param $key
*
* @return mixed|string
*/
public function cleanKey($key) {
$key = preg_replace('/\s+|\W+/', '_', $this->_prefix . $key);
if (strlen($key) > self::MAX_KEY_LEN) {
$md5Key = md5($key); // this should be 32 characters in length
$subKeyLen = self::MAX_KEY_LEN - 1 - strlen($md5Key);
$key = substr($key, 0, $subKeyLen) . "_" . $md5Key;
}
return $key;
}
/**
* @return mixed
*/
public function flush() {
return $this->_cache->flush();
}
}

View file

@ -0,0 +1,89 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface {
/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
*
* @var object
*/
static private $_singleton = NULL;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_NoCache
*/
public function __construct($config) {
}
/**
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function set($key, &$value) {
return FALSE;
}
/**
* @param string $key
*
* @return null
*/
public function get($key) {
return NULL;
}
/**
* @param string $key
*
* @return bool
*/
public function delete($key) {
return FALSE;
}
/**
* @return bool
*/
public function flush() {
return FALSE;
}
}

View file

@ -0,0 +1,152 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface {
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 6379;
const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
/**
* The host name of the redisd server
*
* @var string
*/
protected $_host = self::DEFAULT_HOST;
/**
* The port on which to connect on
*
* @var int
*/
protected $_port = self::DEFAULT_PORT;
/**
* The default timeout to use
*
* @var int
*/
protected $_timeout = self::DEFAULT_TIMEOUT;
/**
* The prefix prepended to cache keys.
*
* If we are using the same redis instance for multiple CiviCRM
* installs, we must have a unique prefix for each install to prevent
* the keys from clobbering each other.
*
* @var string
*/
protected $_prefix = self::DEFAULT_PREFIX;
/**
* The actual redis object
*
* @var Redis
*/
protected $_cache;
/**
* Constructor
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_Redis
*/
public function __construct($config) {
if (isset($config['host'])) {
$this->_host = $config['host'];
}
if (isset($config['port'])) {
$this->_port = $config['port'];
}
if (isset($config['timeout'])) {
$this->_timeout = $config['timeout'];
}
if (isset($config['prefix'])) {
$this->_prefix = $config['prefix'];
}
$this->_cache = new Redis();
if (!$this->_cache->connect($this->_host, $this->_port)) {
// dont use fatal here since we can go in an infinite loop
echo 'Could not connect to redisd server';
CRM_Utils_System::civiExit();
}
$this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD);
}
/**
* @param $key
* @param $value
*
* @return bool
* @throws Exception
*/
public function set($key, &$value) {
if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) {
CRM_Core_Error::fatal("Redis set failed, wondering why?, $key", $value);
return FALSE;
}
return TRUE;
}
/**
* @param $key
*
* @return mixed
*/
public function get($key) {
$result = $this->_cache->get($this->_prefix . $key);
return unserialize($result);
}
/**
* @param $key
*
* @return mixed
*/
public function delete($key) {
return $this->_cache->delete($this->_prefix . $key);
}
/**
* @return mixed
*/
public function flush() {
return $this->_cache->flushDB();
}
}

View file

@ -0,0 +1,125 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Class CRM_Utils_Cache_SerializeCache
*/
class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface {
/**
* The cache storage container, an array by default, stored in a file under templates
*/
private $_cache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_SerializeCache
*/
public function __construct($config) {
$this->_cache = array();
}
/**
* @param $key
*
* @return string
*/
public function fileName($key) {
if (strlen($key) > 50) {
return CIVICRM_TEMPLATE_COMPILEDIR . "CRM_" . md5($key) . ".php";
}
return CIVICRM_TEMPLATE_COMPILEDIR . $key . ".php";
}
/**
* @param string $key
*
* @return mixed
*/
public function get($key) {
if (array_key_exists($key, $this->_cache)) {
return $this->_cache[$key];
}
if (!file_exists($this->fileName($key))) {
return;
}
$this->_cache[$key] = unserialize(substr(file_get_contents($this->fileName($key)), 8));
return $this->_cache[$key];
}
/**
* @param string $key
* @param mixed $value
*/
public function set($key, &$value) {
if (file_exists($this->fileName($key))) {
return;
}
$this->_cache[$key] = $value;
file_put_contents($this->fileName($key), "<?php //" . serialize($value));
}
/**
* @param string $key
*/
public function delete($key) {
if (file_exists($this->fileName($key))) {
unlink($this->fileName($key));
}
unset($this->_cache[$key]);
}
/**
* @param null $key
*/
public function flush($key = NULL) {
$prefix = "CRM_";
if (!$handle = opendir(CIVICRM_TEMPLATE_COMPILEDIR)) {
return; // die? Error?
}
while (FALSE !== ($entry = readdir($handle))) {
if (substr($entry, 0, 4) == $prefix) {
unlink(CIVICRM_TEMPLATE_COMPILEDIR . $entry);
}
}
closedir($handle);
unset($this->_cache);
$this->_cache = array();
}
}

View file

@ -0,0 +1,137 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This caching provider stores all cached items as a "group" in the
* "civicrm_cache" table. The entire 'group' may be prefetched when
* instantiating the cache provider.
*/
class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
/**
* The host name of the memcached server.
*
* @var string
*/
protected $group;
/**
* @var int $componentID The optional component ID (so componenets can share the same name space)
*/
protected $componentID;
/**
* @var array in-memory cache to optimize redundant get()s
*/
protected $frontCache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
* - group: string
* - componentID: int
* - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
*
* @throws RuntimeException
* @return \CRM_Utils_Cache_SqlGroup
*/
public function __construct($config) {
if (isset($config['group'])) {
$this->group = $config['group'];
}
else {
throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
}
if (isset($config['componentID'])) {
$this->componentID = $config['componentID'];
}
else {
$this->componentID = NULL;
}
$this->frontCache = array();
if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
$this->prefetch();
}
}
/**
* @param string $key
* @param mixed $value
*/
public function set($key, &$value) {
CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID);
$this->frontCache[$key] = $value;
}
/**
* @param string $key
*
* @return mixed
*/
public function get($key) {
if (!array_key_exists($key, $this->frontCache)) {
$this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID);
}
return $this->frontCache[$key];
}
/**
* @param $key
* @param null $default
*
* @return mixed
*/
public function getFromFrontCache($key, $default = NULL) {
return CRM_Utils_Array::value($key, $this->frontCache, $default);
}
/**
* @param string $key
*/
public function delete($key) {
CRM_Core_BAO_Cache::deleteGroup($this->group, $key);
unset($this->frontCache[$key]);
}
public function flush() {
CRM_Core_BAO_Cache::deleteGroup($this->group);
$this->frontCache = array();
}
public function prefetch() {
$this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID);
}
}