From e0ba77f32f2575c7ad3c5221ad95b378e5a5a136 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 23 Jan 2019 12:49:21 -0800 Subject: [PATCH] CRM_Utils_Cache::nack() - Add helper for combining get()+has() calls in PSR-16 --- CRM/Utils/Cache.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/CRM/Utils/Cache.php b/CRM/Utils/Cache.php index 3306f821a7..a1cffded14 100644 --- a/CRM/Utils/Cache.php +++ b/CRM/Utils/Cache.php @@ -258,4 +258,32 @@ class CRM_Utils_Cache { return $className; } + /** + * Generate a unique negative-acknowledgement token (NACK). + * + * When using PSR-16 to read a value, the `$cahce->get()` will a return a default + * value on cache-miss, so it's hard to know if you've gotten a geniune value + * from the cache or just a default. If you're in an edge-case where it matters + * (and you want to do has()+get() in a single roundtrip), use the nack() as + * the default: + * + * $nack = CRM_Utils_Cache::nack(); + * $value = $cache->get('foo', $nack); + * echo ($value === $nack) ? "Cache has a value, and we got it" : "Cache has no value". + * + * The value should be unique to avoid accidental matches. As a performance + * tweak, we may reuse the NACK a few times within the current page-view. + * + * @return string + * Unique nonce value indicating a "negative acknowledgement" (failed read). + * If we need to accurately perform has($key)+get($key), we can + * use `get($key,$nack)`. + */ + public static function nack() { + if (!isset(Civi::$statics[__CLASS__]['nack'])) { + Civi::$statics[__CLASS__]['nack'] = 'NACK:' . md5(CRM_Utils_Request::id() . CIVICRM_SITE_KEY . CIVICRM_DSN . mt_rand(0, 10000)); + } + return Civi::$statics[__CLASS__]; + } + } -- 2.25.1