Skip to content
Snippets Groups Projects
Commit 1d6391d2 authored by totten's avatar totten
Browse files

(cloud-native#3) SerializeCache - Remove unused, incomplete cache-driver

The class `CRM_Utils_Cache_SerializeCache` provides a cache-driver for
storing cache records in the filesystem (under `CIVICRM_TEMPLATE_COMPILEDIR`,
using PHP `serialize()` format). Why remove it?

1.  As we work through cleanup in file management (e.g.  #12843,
   cloud-native#3), having an unnecessary reference to
   CIVICRM_TEMPLATE_COMPILEDIR makes it harder to reason about the system.

2. The class is not used. I'm pretty sure it was added speculatively
   (i.e.  with an aim to try using it some day), but that never came to
   pass.  Grepping `universe`, I cannot find any usages or references to
   `SerializeCache`.

3. The implementation is incomplete -- parameters like `get(..., $default)`
   and `set(..., $ttl)` will generate exceptions if used.
parent b28c6739
Branches
Tags
No related merge requests found
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| 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-2019
*/
/**
* Class CRM_Utils_Cache_SerializeCache
*/
class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait;
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;
/**
* The cache storage container, an array by default, stored in a file under templates
* @var array
*/
private $_cache;
/**
* Constructor.
*
* @param array $config
* An array of configuration params.
*
* @return \CRM_Utils_Cache_SerializeCache
*/
public function __construct($config) {
$this->_cache = [];
}
/**
* @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
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = NULL) {
if ($default !== NULL) {
throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default");
}
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
* @param null|int|\DateInterval $ttl
* @return bool
*/
public function set($key, $value, $ttl = NULL) {
if ($ttl !== NULL) {
throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL");
}
if (file_exists($this->fileName($key))) {
// WTF, write-once cache?!
return FALSE;
}
$this->_cache[$key] = $value;
$bytes = file_put_contents($this->fileName($key), "<?php //" . serialize($value));
return ($bytes !== FALSE);
}
/**
* @param string $key
* @return bool
*/
public function delete($key) {
if (file_exists($this->fileName($key))) {
unlink($this->fileName($key));
}
unset($this->_cache[$key]);
return TRUE;
}
/**
* @param null $key
* @return bool
*/
public function flush($key = NULL) {
$prefix = "CRM_";
if (!$handle = opendir(CIVICRM_TEMPLATE_COMPILEDIR)) {
// die? Error?
return FALSE;
}
while (FALSE !== ($entry = readdir($handle))) {
if (substr($entry, 0, 4) == $prefix) {
unlink(CIVICRM_TEMPLATE_COMPILEDIR . $entry);
}
}
closedir($handle);
unset($this->_cache);
$this->_cache = [];
return TRUE;
}
public function clear() {
return $this->flush();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment