+ /**
+ * Translate a TTL to a concrete expiration time.
+ *
+ * @param NULL|int|DateInterval $ttl
+ * @param int $default
+ * The value to use if $ttl is not specified (NULL).
+ * @return int
+ * Timestamp (seconds since epoch).
+ * @throws \CRM_Utils_Cache_InvalidArgumentException
+ */
+ public static function convertCacheTtlToExpires($ttl, $default) {
+ if ($ttl === NULL) {
+ $ttl = $default;
+ }
+
+ if (is_int($ttl)) {
+ return time() + $ttl;
+ }
+ elseif ($ttl instanceof DateInterval) {
+ return date_add(new DateTime(), $ttl)->getTimestamp();
+ }
+ else {
+ throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
+ }
+ }
+
+ /**
+ * Normalize a TTL.
+ *
+ * @param NULL|int|DateInterval $ttl
+ * @param int $default
+ * The value to use if $ttl is not specified (NULL).
+ * @return int
+ * Seconds until expiration.
+ * @throws \CRM_Utils_Cache_InvalidArgumentException
+ */
+ public static function convertCacheTtl($ttl, $default) {
+ if ($ttl === NULL) {
+ return $default;
+ }
+ elseif (is_int($ttl)) {
+ return $ttl;
+ }
+ elseif ($ttl instanceof DateInterval) {
+ return date_add(new DateTime(), $ttl)->getTimestamp() - time();
+ }
+ else {
+ throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
+ }
+ }
+
+