]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Make code-dependent cache entries more volatile
authorEvan Prodromou <evan@status.net>
Mon, 6 Dec 2010 22:05:32 +0000 (17:05 -0500)
committerEvan Prodromou <evan@status.net>
Mon, 6 Dec 2010 22:20:26 +0000 (17:20 -0500)
If a cache entry is dependent on the code that's running, upgrading
(or enabling/disabling plugins) can generate hard-to-track
inconsistencies.

This change adds a close-to-unique fingerprint of the running code to
some cache keys, so that if the fingerprint changes, the old values
are ignored and new values are used.

If the automated uniqueness fails, an administrator can add an extra
config value, $config['site']['build'], that's thrown into the key also.

lib/cache.php
lib/default.php

index ea0ff769d1992c4b9e16690777bd104311d531ce..dc667654ab6ef327e8a022ae1b13d7612ecfa1b2 100644 (file)
@@ -86,6 +86,55 @@ class Cache
         return 'statusnet:' . $base_key . ':' . $extra;
     }
 
+    /**
+     * Create a cache key for data dependent on code
+     *
+     * For cache elements that are dependent on changes in code, this creates
+     * a more-or-less fingerprint of the current running code and adds it to
+     * the cache key. In the case of an upgrade of core, or addition or
+     * removal of plugins, a new unique fingerprint is generated and used.
+     * 
+     * There can still be problems with a) differences in versions of the 
+     * plugins and b) people running code between official versions. This is
+     * usually a problem only for experienced users like developers, who know
+     * how to clear their cache.
+     *
+     * For sites that run code between versions (like the status.net cloud),
+     * there's an additional build number configuration setting.
+     * 
+     * @param string $extra the real part of the key
+     *
+     * @return string full key
+     */
+    
+    static function codeKey($extra)
+    {
+        static $prefix = null;
+       
+        if (empty($prefix)) {
+           
+            $plugins     = StatusNet::getActivePlugins();
+            $names       = array();
+           
+            foreach ($plugins as $plugin) {
+                $names[] = $plugin[0];
+            }
+           
+            $names = array_unique($names);
+            asort($names);
+           
+            // Unique enough.
+       
+            $uniq = crc32(implode(',', $names));
+
+            $build = common_config('site', 'build');
+
+            $prefix = STATUSNET_VERSION.':'.$build.':'.$uniq;
+        }
+       
+        return Cache::key($prefix.':'.$extra);
+    }
+    
     /**
      * Make a string suitable for use as a key
      *
index a19453fce49ddb3ec2edcd61f78e42f9220b395f..6c8b0392747f70e063fb2186e23f7576bb50a4b8 100644 (file)
@@ -59,7 +59,8 @@ $default =
               'textlimit' => 140,
               'indent' => true,
               'use_x_sendfile' => false,
-              'notice' => null // site wide notice text
+              'notice' => null, // site wide notice text
+              'build' => 1, // build number, for code-dependent cache
               ),
         'db' =>
         array('database' => 'YOU HAVE TO SET THIS IN config.php',