Commit b80f7794c837e9d43200890c0f61e5a64ffd6fb6

Authored by conradverm
1 parent 33c74b4d

KTS-2386

"Add some basic plugin caching to improve performance"
Updated. Caching of plugins to file now implemented.

Committed By: Conrad Vermeulen
Reviewed By: Megan Watson

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7272 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 87 additions and 6 deletions
lib/plugins/pluginutil.inc.php
... ... @@ -59,10 +59,74 @@ class KTPluginResourceRegistry {
59 59 }
60 60  
61 61 class KTPluginUtil {
  62 + const CACHE_FILENAME = 'kt_plugins.cache';
  63 +
  64 + /**
  65 + * Store the plugin cache in the cache directory.
  66 + *
  67 + */
  68 + static function savePluginCache()
  69 + {
  70 + $config = KTConfig::getSingleton();
  71 + $cacheDir = $config->get('cache/cacheDirectory');
  72 +
  73 + $written = file_put_contents($cacheDir . '/' . KTPluginUtil::CACHE_FILENAME , serialize($GLOBALS['_KT_PLUGIN']));
  74 +
  75 + if (!$written)
  76 + {
  77 + global $default;
  78 +
  79 + $default->log->warn('savePluginCache - The cache did not write anything.');
  80 +
  81 + // try unlink a zero size file - just in case
  82 + @unlink($cacheFile);
  83 + }
  84 + }
  85 +
  86 + /**
  87 + * Remove the plugin cache.
  88 + *
  89 + */
  90 + static function removePluginCache()
  91 + {
  92 + $config = KTConfig::getSingleton();
  93 + $cacheDir = $config->get('cache/cacheDirectory');
  94 +
  95 + $cacheFile=$cacheDir . '/' . KTPluginUtil::CACHE_FILENAME;
  96 + @unlink($cacheFile);
  97 + }
  98 +
  99 + /**
  100 + * Reads the plugin cache file. This must still be unserialised.
  101 + *
  102 + * @return mixed Returns false on failure, or the serialised cache.
  103 + */
  104 + static function readPluginCache()
  105 + {
  106 + $config = KTConfig::getSingleton();
  107 + $cacheDir = $config->get('cache/cacheDirectory');
  108 +
  109 + $cacheFile=$cacheDir . '/' . KTPluginUtil::CACHE_FILENAME;
  110 + if (!is_file($cacheFile))
  111 + {
  112 + return false;
  113 + }
  114 +
  115 + $cache = file_get_contents($cacheFile);
  116 +
  117 + // we check for an empty cache in case there was a problem. We rather try and reload everything otherwise.
  118 + if (strlen($cache) == 0)
  119 + {
  120 + return false;
  121 + }
  122 + return $cache;
  123 + }
  124 +
62 125 static function loadPlugins () {
63 126  
64   - if (session_is_registered('__KT_PLUGIN_CACHE')) {
65   - require_once(KT_LIB_DIR . "/plugins/plugin.inc.php");
  127 + $cache = KTPluginUtil::readPluginCache();
  128 + if ($cache !== false) {
  129 + require_once(KT_LIB_DIR . '/plugins/plugin.inc.php');
66 130 require_once(KT_LIB_DIR . '/actions/actionregistry.inc.php');
67 131 require_once(KT_LIB_DIR . '/actions/portletregistry.inc.php');
68 132 require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
... ... @@ -76,8 +140,21 @@ class KTPluginUtil {
76 140 require_once(KT_LIB_DIR . "/authentication/interceptorregistry.inc.php");
77 141 require_once(KT_LIB_DIR . "/widgets/widgetfactory.inc.php");
78 142 require_once(KT_LIB_DIR . "/validation/validatorfactory.inc.php");
79   - $GLOBALS['_KT_PLUGIN'] = $_SESSION['__KT_PLUGIN_CACHE'];
80   - $GLOBALS['_KT_PLUGIN']['oKTPluginRegistry']->_aPlugins = array();
  143 +
  144 + // unserialize - step 1 - get _aPluginDetails so we can include all we need
  145 + $GLOBALS['_KT_PLUGIN'] = unserialize($cache);
  146 +
  147 + foreach($GLOBALS['_KT_PLUGIN']['oKTPluginRegistry']->_aPluginDetails as $detail)
  148 + {
  149 + $classname = $detail[0];
  150 + $inc = $detail[2];
  151 + if (!class_exists($classname))
  152 + {
  153 + require_once($inc);
  154 + }
  155 + }
  156 + // unserialize - step 2 - so we don't have incomplete classes
  157 + $GLOBALS['_KT_PLUGIN'] = unserialize($cache);
81 158 return;
82 159 }
83 160 $GLOBALS['_KT_PLUGIN'] = array();
... ... @@ -121,9 +198,13 @@ class KTPluginUtil {
121 198 $oPlugin->load();
122 199 }
123 200 }
124   - $_SESSION['__KT_PLUGIN_CACHE'] = $GLOBALS['_KT_PLUGIN'];
  201 + KTPluginUtil::savePluginCache();
125 202 }
126 203  
  204 + /**
  205 + * This loads the plugins in the plugins folder. It searches for files ending with 'Plugin.php'.
  206 + * This is called by the 'Re-read plugins' action in the web interface.
  207 + */
127 208 function registerPlugins () {
128 209 KTPluginUtil::_deleteSmartyFiles();
129 210 require_once(KT_LIB_DIR . '/cache/cache.inc.php');
... ... @@ -164,7 +245,7 @@ class KTPluginUtil {
164 245 $oCache =& KTCache::getSingleton();
165 246 $oCache->deleteAllCaches();
166 247  
167   - session_unset('__KT_PLUGIN_CACHE');
  248 + KTPluginUtil::removePluginCache();
168 249 }
169 250  
170 251 function _deleteSmartyFiles() {
... ...