? per-bin-prefix.patch Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/memcache/README.txt,v retrieving revision 1.11.2.4 diff -u -p -r1.11.2.4 README.txt --- README.txt 29 Sep 2010 15:03:26 -0000 1.11.2.4 +++ README.txt 4 Feb 2011 08:11:30 -0000 @@ -13,11 +13,11 @@ These are the broad steps you need to take in order to use this software. Order is important. -1. Install the memcached binaries on your server. See +1. Install the memcached binaries on your server. See http://www.lullabot.com/articles/how_install_memcache_debian_etch -2. Install the PECL memcache extension for PHP. This must be version 2.2.1 or +2. Install the PECL memcache extension for PHP. This must be version 2.2.1 or higher or you will experience errors. 3. Put your site into offline mode. 4. Download and install the memcache module. @@ -71,8 +71,8 @@ arrays to $conf; memcache_servers and me pattern: 'memcache_servers' => array( - host1:port => cluster, - host2:port => cluster, + host1:port => cluster, + host2:port => cluster, hostN:port => cluster ) @@ -138,6 +138,31 @@ $conf = array( 'memcache_key_prefix' => 'something_unique', ); +## PER BIN PREFIXING ## +Sometimes it is desired to share data in specific bins between multiple sites (while hiding the rest). +For example you would want to share user sessions shared between sites while the rest of the site caches remain unique to that site. +That can be accomplished using more granular control over key prefixes per bin using the following method +# site 1 +$conf = array( + ... + 'memcache_key_prefix' => array( + 'default' => 'something_unique_for_site1', # Default prefix for all bins on site 1 + 'cache_page' => 'page_site1', + 'cache_filter' => 'filter_site1', + 'cache_session' => 'shared_bin_for_all_sites', # shared bin + ), +); +# site 2 +$conf = array( + ... + 'default_key_prefix' => array( + 'default' => 'something_unique_for_site2', # Default prefix for all bins on site 2 + 'cache_filter' => 'filter_site2', + 'cache_session' => 'shared_bin_for_all_sites', # shared bin + ), +); + + ## SESSIONS ## Here is a sample config that uses memcache for sessions. Note you MUST have @@ -178,8 +203,8 @@ Failed to set key: Failed to set key: ca SOLUTION: Upgrade your PECL library to PECL package (2.2.1) (or higher). -WARNING: -Zlib compression at the php.ini level and Memcache conflict. +WARNING: +Zlib compression at the php.ini level and Memcache conflict. See http://drupal.org/node/273824 ## MEMCACHE ADMIN ## @@ -191,8 +216,8 @@ way to clear the cache, and an interface ## Memcached PECL Extension Support We also now support the Memcached PECL extension. If you install this extension, -it will be used by default. This new extension backends to libmemcached and -allows you to use some of the newer advanced features in memcached 1.4. +it will be used by default. This new extension backends to libmemcached and +allows you to use some of the newer advanced features in memcached 1.4. NOTE: It is important to realize that the memcache php.ini options do not impact the memcached extension, this new extension doesn't read in options that way. Index: dmemcache.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/memcache/dmemcache.inc,v retrieving revision 1.1.4.14 diff -u -p -r1.1.4.14 dmemcache.inc --- dmemcache.inc 29 Sep 2010 15:03:26 -0000 1.1.4.14 +++ dmemcache.inc 4 Feb 2011 08:11:30 -0000 @@ -321,22 +321,44 @@ function dmemcache_object($bin = NULL, $ return empty($memcacheCache[$bin]) ? FALSE : $memcacheCache[$bin]; } +/** + * Returns a memcache object key with optional unique prefix. + * + * @param $key The key which is to be used. + * @param $bin The bin which is to be used. + * + * @return a memcache key. + */ function dmemcache_key($key, $bin = 'cache') { - static $prefix; + static $prefixes; // memcache_key_prefix can be set in settings.php to support site namespaces // in a multisite environment. - if (empty($prefix)) { - if ($prefix = variable_get('memcache_key_prefix', '')) { - $prefix .= '-'; + if (empty($prefixes)) { + $prefixes = variable_get('memcache_key_prefix', ''); + } + + $prefix = ''; + // Per-bin key prefixes + if (is_array($prefixes)) { + if (!empty($prefixes[$bin])) { + $prefix = $prefixes[$bin] . '-'; + } + elseif (!empty($prefixes['default'])) { + $prefix = $prefixes['default'] . '-'; } } + elseif (!empty($prefixes)) { + // Support single string prefix (Old behavior) + $prefix = $prefixes . '-'; + } + $full_key = urlencode($prefix . $bin . '-' . $key); // Memcache only supports key lengths up to 250 bytes. If we have generated // a longer key, hash it with sha1 which will shrink the key down to 40 bytes // while still keeping it unique. if (strlen($full_key) > 250) { - $full_key = $prefix . $bin . '-' . sha1($key); + $full_key = urlencode($prefix . $bin . '-' . sha1($key)); } return $full_key;