Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created March 11, 2010 04:02
Show Gist options
  • Select an option

  • Save sminnee/328803 to your computer and use it in GitHub Desktop.

Select an option

Save sminnee/328803 to your computer and use it in GitHub Desktop.
Index: subsites/code/Subsite.php
===================================================================
--- subsites/code/Subsite.php (revision 100863)
+++ subsites/code/Subsite.php (working copy)
@@ -74,6 +74,11 @@
*/
protected static $allowed_themes = array();
+ /**
+ * Memory cache of accessible sites
+ */
+ private static $_cache_accessible_sites = array();
+
static function set_allowed_domains($domain){
user_error('Subsite::set_allowed_domains() is deprecated; it is no longer necessary '
. 'because users can now enter any domain name', E_USER_NOTICE);
@@ -429,16 +434,23 @@
* @param $mainSiteTitle The label to give to the main site
*/
function accessible_sites($permCode, $includeMainSite = false, $mainSiteTitle = "Main site", $member = null) {
- // For 2.3 and 2.4 compatibility
- $q = defined('Database::USE_ANSI_SQL') ? "\"" : "`";
-
// Rationalise member arguments
if(!$member) $member = Member::currentUser();
if(!$member) return new DataObjectSet();
if(!is_object($member)) $member = DataObject::get_by_id('Member', $member);
+ // Rationalise permCode argument
if(is_array($permCode)) $SQL_codes = "'" . implode("', '", Convert::raw2sql($permCode)) . "'";
else $SQL_codes = "'" . Convert::raw2sql($permCode) . "'";
+
+ // For 2.3 and 2.4 compatibility
+ $q = defined('Database::USE_ANSI_SQL') ? "\"" : "`";
+
+ // Cache handling
+ $cacheKey = $SQL_codes . '-' . $member->ID;
+ if(isset(self::$_cache_accessible_sites[$cacheKey])) {
+ return self::$_cache_accessible_sites[$cacheKey];
+ }
$templateClassList = "'" . implode("', '", ClassInfo::subclassesFor("Subsite_Template")) . "'";
@@ -497,6 +509,8 @@
}
}
+ self::$_cache_accessible_sites[$cacheKey] = $subsites;
+
return $subsites;
}
@@ -568,6 +582,13 @@
static function disable_subsite_filter($disabled = true) {
self::$disable_subsite_filter = $disabled;
}
+
+ /**
+ * Flush caches on database reset
+ */
+ static function on_db_reset() {
+ self::$_cache_accessible_sites = array();
+ }
}
/**
Index: sapphire/dev/SapphireTest.php
===================================================================
--- sapphire/dev/SapphireTest.php (revision 100863)
+++ sapphire/dev/SapphireTest.php (working copy)
@@ -415,7 +415,7 @@
// Some DataObjectsDecorators keep a static cache of information that needs to
// be reset whenever the database is cleaned out
- foreach(ClassInfo::subclassesFor('DataObjectDecorator') as $class) {
+ foreach(array_merge(ClassInfo::subclassesFor('DataObjectDecorator'), ClassInfo::subclassesFor('DataObject')) as $class) {
$toCall = array($class, 'on_db_reset');
if(is_callable($toCall)) call_user_func($toCall);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment