Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 316 lines (287 sloc) 10.832 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
<?php /*

ocPortal
Copyright (c) ocProducts, 2004-2012

See text/EN/licence.txt for full licencing information.


NOTE TO PROGRAMMERS:
Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
**** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
* @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
* @copyright ocProducts Ltd
* @package core
*/

/**
* Standard code module initialisation function.
*/
function init__caches()
{
global $CACHE_ON;
$CACHE_ON=NULL;

global $MEM_CACHE,$SITE_INFO;
$MEM_CACHE=NULL;
$use_memcache=((array_key_exists('use_mem_cache',$SITE_INFO)) && ($SITE_INFO['use_mem_cache']=='1'));// Default to off because badly configured caches can result in lots of very slow misses and lots of lost sessions || ((!array_key_exists('use_mem_cache',$SITE_INFO)) && ((function_exists('xcache_get')) || (function_exists('wincache_ucache_get')) || (function_exists('apc_fetch')) || (function_exists('eaccelerator_get')) || (function_exists('mmcache_get'))));
if ((($use_memcache)/* || ($GLOBALS['DEBUG_MODE'])*/) && ($GLOBALS['IN_MINIKERNEL_VERSION']!=1)) // There's some randomness so people in dev mode SOMETIMES use memcache. This'll stop it being allowed to rot.
{
if (class_exists('Memcache'))
{
$MEM_CACHE=new Memcache();
$MEM_CACHE->connect('localhost',11211) OR $MEM_CACHE=NULL;
}
elseif (function_exists('apc_fetch'))
{
require_code('caches_apc');

$MEM_CACHE=new apccache();
global $ECACHE_OBJECTS;
$ECACHE_OBJECTS=apc_fetch(get_file_base().'ECACHE_OBJECTS');
if ($ECACHE_OBJECTS===false) $ECACHE_OBJECTS=array();
}
elseif ((function_exists('eaccelerator_put')) || (function_exists('mmcache_put')))
{
require_code('caches_eaccelerator');

$MEM_CACHE=new eacceleratorcache();
global $ECACHE_OBJECTS;
if (function_exists('eaccelerator_get'))
$ECACHE_OBJECTS=eaccelerator_get(get_file_base().'ECACHE_OBJECTS');
if (function_exists('mmcache_get'))
$ECACHE_OBJECTS=mmcache_get(get_file_base().'ECACHE_OBJECTS');
if ($ECACHE_OBJECTS===NULL) $ECACHE_OBJECTS=array();
}
elseif (function_exists('xcache_get'))
{
require_code('caches_xcache');

$MEM_CACHE=new xcache();
global $ECACHE_OBJECTS;
$ECACHE_OBJECTS=xcache_get(get_file_base().'ECACHE_OBJECTS');
if ($ECACHE_OBJECTS===false) $ECACHE_OBJECTS=array();
}
elseif (function_exists('wincache_ucache_get'))
{
require_code('caches_wincache');

$MEM_CACHE=new wincache();
global $ECACHE_OBJECTS;
$ECACHE_OBJECTS=wincache_ucache_get(get_file_base().'ECACHE_OBJECTS');
if ($ECACHE_OBJECTS===false) $ECACHE_OBJECTS=array();
}
elseif (file_exists(get_custom_file_base().'/persistant_cache/'))
{
require_code('caches_filesystem');
require_code('files');
$MEM_CACHE=new filecache();
}
}
}

/**
* Get data from the persistant cache.
*
* @param mixed Key
* @param ?TIME Minimum timestamp that entries from the cache may hold (NULL: don't care)
* @return ?mixed The data (NULL: not found / NULL entry)
*/
function persistant_cache_get($key,$min_cache_date=NULL)
{
global $MEM_CACHE;
if (($GLOBALS['DEBUG_MODE']) && (mt_rand(0,3)==1)) return NULL;
if ($MEM_CACHE===NULL) return NULL;
$test=$MEM_CACHE->get(get_file_base().serialize($key),$min_cache_date); // First we'll try specifically for site
if ($test!==NULL) return $test;
return $MEM_CACHE->get(('ocp'.float_to_raw_string(ocp_version_number())).serialize($key),$min_cache_date); // And last we'll try server-wide
}

/**
* Put data into the persistant cache.
*
* @param mixed Key
* @param mixed The data
* @param boolean Whether it is server-wide data
* @param ?integer The expiration time in seconds. (NULL: Default expiry in 60 minutes, or never if it is server-wide).
*/
function persistant_cache_set($key,$data,$server_wide=false,$expire_secs=NULL)
{
global $MEM_CACHE;
if ($MEM_CACHE===NULL) return NULL;
if ($expire_secs===NULL) $expire_secs=$server_wide?0:(60*60);
$MEM_CACHE->set(($server_wide?('ocp'.float_to_raw_string(ocp_version_number())):get_file_base()).serialize($key),$data,0,$expire_secs);
}

/**
* Delete data from the persistant cache.
*
* @param mixed Key name
*/
function persistant_cache_delete($key)
{
global $MEM_CACHE;
if ($MEM_CACHE===NULL) return NULL;
$MEM_CACHE->delete(get_file_base().serialize($key));
$MEM_CACHE->delete('ocp'.float_to_raw_string(ocp_version_number()).serialize($key));
}

/**
* Remove all data from the persistant cache.
*/
function persistant_cache_empty()
{
global $MEM_CACHE;
if ($MEM_CACHE===NULL) return NULL;
$MEM_CACHE->flush();
}

/**
* Remove an item from the general cache (most commonly used for blocks).
*
* @param ID_TEXT The type of what we are cacheing (e.g. block name)
* @param ?array A map of identifiying characteristics (NULL: no identifying characteristics, decache all)
*/
function decache($cached_for,$identifier=NULL)
{
if (running_script('stress_test_loader')) return;
if (get_page_name()=='admin_import') return;

// NB: If we use persistant cache we still need to decache from DB, in case we're switching between for whatever reason. Or maybe some users use persistant cache and others don't. Or maybe some nodes do and others don't.

if ($GLOBALS['MEM_CACHE']!==NULL)
{
persistant_cache_delete(array('CACHE',$cached_for));
}

$where=array('cached_for'=>$cached_for);
if ($identifier!==NULL) $where['identifier']=md5(serialize($identifier));
$GLOBALS['SITE_DB']->query_delete('cache',$where);

if ($identifier!==NULL)
{
global $TEMPCODE_SETGET;
$identifier[]=array_key_exists('in_panel',$TEMPCODE_SETGET)?$TEMPCODE_SETGET['in_panel']:'0';
$where['identifier']=md5(serialize($identifier));
$GLOBALS['SITE_DB']->query_delete('cache',$where);
}
}

/**
* Find the cache-on parameters for 'codename's cacheing style (prevents us needing to load up extra code to find it).
*
* @param ID_TEXT The codename of what will be checked for cacheing
* @return ?array The cached result (NULL: no cached result)
*/
function find_cache_on($codename)
{
if (defined('HIPHOP_PHP')) return NULL;

// See if we have it cached
global $CACHE_ON;
if ($CACHE_ON===NULL)
{
$CACHE_ON=persistant_cache_get('CACHE_ON');
if ($CACHE_ON===NULL)
{
$CACHE_ON=$GLOBALS['SITE_DB']->query_select('cache_on',array('*'));
persistant_cache_set('CACHE_ON',$CACHE_ON);
}
}
foreach ($CACHE_ON as $row)
{
if ($row['cached_for']==$codename)
{
return $row;
}
}
return NULL;
}

/**
* Find the cached result of what is named by codename and the further constraints.
*
* @param ID_TEXT The codename to check for cacheing
* @param LONG_TEXT The further restraints (a serialized map)
* @param integer The TTL for the cache entry
* @param boolean Whether we are cacheing Tempcode (needs special care)
* @param boolean Whether to defer caching to CRON. Note that this option only works if the block's defined cache signature depends only on $map (timezone, bot-type, in-panel and interlock are automatically considered)
* @param ?array Parameters to call up block with if we have to defer caching (NULL: none)
* @return ?mixed The cached result (NULL: no cached result)
*/
function get_cache_entry($codename,$cache_identifier,$ttl=10000,$tempcode=false,$caching_via_cron=false,$map=NULL) // Default to a very big ttl
{
if ($GLOBALS['MEM_CACHE']!==NULL)
{
$pcache=persistant_cache_get(array('CACHE',$codename));
if ($pcache===NULL) return NULL;
$theme=$GLOBALS['FORUM_DRIVER']->get_theme();
$lang=user_lang();
$pcache=isset($pcache[$cache_identifier][$lang][$theme])?$pcache[$cache_identifier][$lang][$theme]:NULL;
if ($pcache===NULL)
{
if ($caching_via_cron)
{
request_via_cron($codename,$map,$tempcode);
return paragraph(do_lang_tempcode('CACHE_NOT_READY_YET'),'','nothing_here');
}
return NULL;
}
$cache_rows=array($pcache);
} else
{
$cache_rows=$GLOBALS['SITE_DB']->query_select('cache',array('*'),array('lang'=>user_lang(),'cached_for'=>$codename,'the_theme'=>$GLOBALS['FORUM_DRIVER']->get_theme(),'identifier'=>md5($cache_identifier)),'',1);
if (!isset($cache_rows[0])) // No
{
if ($caching_via_cron)
{
request_via_cron($codename,$map,$tempcode);
return paragraph(do_lang_tempcode('CACHE_NOT_READY_YET'),'','nothing_here');
}
return NULL;
}

if ($tempcode)
{
$ob=new ocp_tempcode();
if (!$ob->from_assembly($cache_rows[0]['the_value'],true)) return NULL;
$cache_rows[0]['the_value']=$ob;
} else
{
$cache_rows[0]['the_value']=unserialize($cache_rows[0]['the_value']);
}
}

$stale=(($ttl!=-1) && (time()>($cache_rows[0]['date_and_time']+$ttl*60)));

if ((!$caching_via_cron) && ($stale)) // Out of date
{
return NULL;
} else // We can use directly
{
if ($stale)
request_via_cron($codename,$map,$tempcode);

$cache=$cache_rows[0]['the_value'];
if ($cache_rows[0]['langs_required']!='')
{
$bits=explode('!',$cache_rows[0]['langs_required']);
$langs_required=explode(':',$bits[0]); // Sometimes lang has got intertwinded with non cacheable stuff (and thus was itself not cached), so we need the lang files
foreach ($langs_required as $lang)
if ($lang!='') require_lang($lang,NULL,NULL,true);
if (isset($bits[1]))
{
$javascripts_required=explode(':',$bits[1]);
foreach ($javascripts_required as $javascript)
if ($javascript!='') require_javascript($javascript);
}
if (isset($bits[2]))
{
$csss_required=explode(':',$bits[2]);
foreach ($csss_required as $css)
if ($css!='') require_css($css);
}
}
return $cache;
}
}

/**
* Request that CRON loads up a block's caching in the background.
*
* @param ID_TEXT The codename of the block
* @param ?array Parameters to call up block with if we have to defer caching (NULL: none)
* @param boolean Whether we are cacheing Tempcode (needs special care)
*/
function request_via_cron($codename,$map,$tempcode)
{
global $TEMPCODE_SETGET;
$map=array(
'c_theme'=>$GLOBALS['FORUM_DRIVER']->get_theme(),
'c_lang'=>user_lang(),
'c_codename'=>$codename,
'c_map'=>serialize($map),
'c_timezone'=>get_users_timezone(get_member()),
'c_is_bot'=>is_null(get_bot_type())?0:1,
'c_in_panel'=>((array_key_exists('in_panel',$TEMPCODE_SETGET)?$TEMPCODE_SETGET['in_panel']:'_false')=='_true')?1:0,
'c_interlock'=>((array_key_exists('interlock',$TEMPCODE_SETGET)?$TEMPCODE_SETGET['interlock']:'_false')=='_true')?1:0,
'c_store_as_tempcode'=>$tempcode?1:0,
);
if (is_null($GLOBALS['SITE_DB']->query_value_null_ok('cron_caching_requests','id',$map)))
$GLOBALS['SITE_DB']->query_insert('cron_caching_requests',$map);
}
Something went wrong with that request. Please try again.