Commit 9f70b0e4 authored by totten's avatar totten

(#174) CRM_Utils_Cache_Interface - Add `has()` (PSR-16)

parent c31de879
......@@ -33,6 +33,7 @@
class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
......
......@@ -37,6 +37,7 @@
class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait;
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
/**
* The cache storage container, an in memory array by default
......
......@@ -108,4 +108,18 @@ interface CRM_Utils_Cache_Interface {
*/
public function clear();
/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
*
* @return bool
*/
public function has($key);
}
......@@ -33,6 +33,7 @@
class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
......
......@@ -33,6 +33,7 @@
class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
......
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| 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-2018
*
* The traditional CRM_Utils_Cache_Interface did not support has().
* To get drop-in compliance with PSR-16, we use a naive adapter.
*
* Ideally, these should be replaced with more performant/native versions.
*/
trait CRM_Utils_Cache_NaiveHasTrait {
public function has($key) {
// This is crazy-talk. If you've got an environment setup where you might
// be investigating this, fix your preferred cache driver by
// replacing `NaiveHasTrait` with a decent function.
$hasDefaultA = ($this->get($key, NULL) === NULL);
$hasDefaultB = ($this->get($key, 123) === 123);
return !($hasDefaultA && $hasDefaultB);
}
}
......@@ -33,6 +33,7 @@
class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
/**
* We only need one instance of this object. So we use the singleton
......
......@@ -35,6 +35,7 @@
class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 6379;
......
......@@ -37,6 +37,7 @@
class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait;
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
/**
* The cache storage container, an array by default, stored in a file under templates
......
......@@ -39,6 +39,7 @@
class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
/**
* The host name of the memcached server.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment