resourceloader: Simplify getHashMtime() to merely a timestamp
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 31 Mar 2015 18:17:11 +0000 (19:17 +0100)
committerOri.livneh <ori@wikimedia.org>
Tue, 31 Mar 2015 20:57:07 +0000 (20:57 +0000)
Similar to what getDefinitionMtime() does already. No need to
repeat the hash. No need for an array that needs serialising and
unserialising internally.

Change the hash key to avoid using old cache values.

Also moved the comment about hashes being included in the key to
this method. getDefinitionMtime() is a later method that performs
the same logic but doesn't need the entire story again.

Follows-up 044713c4d3bdda32.

Change-Id: Idd83de5ac27138a2dbf2ec49d81ea9188bd6ad57

includes/resourceloader/ResourceLoaderModule.php

index 572428e..ed16521 100644 (file)
@@ -458,21 +458,26 @@ abstract class ResourceLoaderModule {
                        return 1;
                }
 
+               // Embed the hash itself in the cache key. This allows for a few nifty things:
+               // - During deployment, servers with old and new versions of the code communicating
+               //   with the same memcached will not override the same key repeatedly increasing
+               //   the timestamp.
+               // - In case of the definition changing and then changing back in a short period of time
+               //   (e.g. in case of a revert or a corrupt server) the old timestamp and client-side cache
+               //   url will be re-used.
+               // - If different context-combinations (e.g. same skin, same language or some combination
+               //   thereof) result in the same definition, they will use the same hash and timestamp.
                $cache = wfGetCache( CACHE_ANYTHING );
-               $key = wfMemcKey( 'resourceloader', 'modulemodifiedhash', $this->getName(), $hash );
+               $key = wfMemcKey( 'resourceloader', 'hashmtime', $this->getName(), $hash );
 
                $data = $cache->get( $key );
-               if ( is_array( $data ) && $data['hash'] === $hash ) {
-                       // Hash is still the same, re-use the timestamp of when we first saw this hash.
-                       return $data['timestamp'];
+               if ( is_int( $data ) && $data > 0 ) {
+                       // We've seen this hash before, re-use the timestamp of when we first saw it.
+                       return $data;
                }
 
                $timestamp = time();
-               $cache->set( $key, array(
-                       'hash' => $hash,
-                       'timestamp' => $timestamp,
-               ) );
-
+               $cache->set( $key, $timestamp );
                return $timestamp;
        }
 
@@ -504,18 +509,7 @@ abstract class ResourceLoaderModule {
                }
 
                $hash = md5( json_encode( $summary ) );
-
                $cache = wfGetCache( CACHE_ANYTHING );
-
-               // Embed the hash itself in the cache key. This allows for a few nifty things:
-               // - During deployment, servers with old and new versions of the code communicating
-               //   with the same memcached will not override the same key repeatedly increasing
-               //   the timestamp.
-               // - In case of the definition changing and then changing back in a short period of time
-               //   (e.g. in case of a revert or a corrupt server) the old timestamp and client-side cache
-               //   url will be re-used.
-               // - If different context-combinations (e.g. same skin, same language or some combination
-               //   thereof) result in the same definition, they will use the same hash and timestamp.
                $key = wfMemcKey( 'resourceloader', 'moduledefinition', $this->getName(), $hash );
 
                $data = $cache->get( $key );
@@ -529,7 +523,6 @@ abstract class ResourceLoaderModule {
 
                $timestamp = time();
                $cache->set( $key, $timestamp );
-
                return $timestamp;
        }