From 7d32e6b68c7b84b509fa3703ef623b6e781fa0b6 Mon Sep 17 00:00:00 2001 From: amontero Date: Fri, 7 Apr 2017 11:11:54 +0200 Subject: [PATCH] mongodb_cache: Use MongoDate field type instead of NumberLong for dates. --- mongodb_cache/mongodb_cache_plugin.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/mongodb_cache/mongodb_cache_plugin.php b/mongodb_cache/mongodb_cache_plugin.php index 2808c4a..1b029f6 100644 --- a/mongodb_cache/mongodb_cache_plugin.php +++ b/mongodb_cache/mongodb_cache_plugin.php @@ -240,7 +240,7 @@ class Cache implements \DrupalCacheInterface { // Remove non-permanently cached items from the collection. $criteria = [ 'expire' => [ - '$lte' => $flush_timestamp, + '$lte' => new \MongoDate($flush_timestamp), '$ne' => CACHE_PERMANENT, ], ]; @@ -259,8 +259,8 @@ class Cache implements \DrupalCacheInterface { /** * Prepare a cached item. * - * Checks that items are either permanent not yet expired, and unserializes - * data as appropriate. + * Checks that items are either permanent not yet expired, unserializes + * data as appropriate and converts MongoDB native dates to timestamps. * * @param array|null $cache * An item loaded from cache_get() or cache_get_multiple(). @@ -277,6 +277,14 @@ class Cache implements \DrupalCacheInterface { unset($cache['_id']); $cache = (object) $cache; + // Provide backwards compatibility for NumberLong field types. + if ($cache->created instanceof \MongoDate) { + $cache->created = $cache->created->toDateTime()->getTimestamp(); + } + if ($cache->expire instanceof \MongoDate) { + $cache->expire = $cache->expire->toDateTime()->getTimestamp(); + } + // If enforcing a minimum cache lifetime, validate that the data is // currently valid for this user before we return it by making sure the // cache entry was created before the timestamp in the current session's @@ -307,8 +315,8 @@ class Cache implements \DrupalCacheInterface { $entry = array( '_id' => (string) $cid, 'cid' => (string) $cid, - 'created' => REQUEST_TIME, - 'expire' => $expire, + 'created' => new \MongoDate(REQUEST_TIME), + 'expire' => new \MongoDate($expire), 'serialized' => !$scalar, 'data' => $scalar ? $data : serialize($data), ); @@ -368,7 +376,7 @@ class Cache implements \DrupalCacheInterface { $criteria = [ 'expire' => [ '$ne' => CACHE_PERMANENT, - '$lte' => REQUEST_TIME, + '$lte' => new \MongoDate(REQUEST_TIME), ], ]; $this->attemptRemove($criteria); @@ -380,7 +388,7 @@ class Cache implements \DrupalCacheInterface { $criteria = [ 'expire' => [ '$ne' => CACHE_PERMANENT, - '$lte' => REQUEST_TIME, + '$lte' => new \MongoDate(REQUEST_TIME), ], ]; $this->attemptRemove($criteria); -- 1.9.2