25 require_once SQ_CORE_PACKAGE_PATH.
'/system/cache_storage/cache_storage/cache_storage.inc';
51 parent::__construct($assetid);
73 function store($cache_key, $perm_key, $url, $assetid, $data, $expiry)
76 $group = $this->
_getAssetHash($cache_key).$this->_getAssetHash($perm_key);
77 $file_name = md5($cache_key.
':'.$perm_key.
':'.$url);
78 $file_path = $group.
'/'.$file_name;
82 $cached = $this->
_getCache($cache_key, $perm_key, $url);
83 if (!is_null($cached)) {
84 $this->
_updateCache($cache_key, $perm_key, $url, $expiry, $file_path, $data);
89 $old_umask = umask(0);
90 if (!is_dir(SQ_CACHE_PATH.
'/'.$group)) {
91 $status = mkdir(SQ_CACHE_PATH.
'/'.$group, 0755);
94 trigger_localised_error(
'CORE0245', E_USER_WARNING, SQ_CACHE_PATH.
'/'.$group);
98 $status = string_to_file($data, SQ_CACHE_PATH.
'/'.$file_path);
100 trigger_localised_error(
'CORE0243', E_USER_WARNING, SQ_CACHE_PATH.
'/'.$file_path);
105 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
106 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
110 $cached = $this->
_getCache($cache_key, $perm_key, $url);
111 if (is_null($cached)) {
114 'cache_key' => $cache_key,
115 'perm_key' => $perm_key,
117 'assetid' => $assetid,
118 'expires' => ts_iso8601($expiry),
119 'path' => $file_path,
123 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
125 $cached = $this->
_getCache($cache_key, $perm_key, $url);
126 if (is_null($cached)) {
127 throw new Exception(
'Unable to cache asset ID # '.$assetid.
' due to database error: '.$e->getMessage());
131 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
134 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
136 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
156 function read($cache_key, $perm_key, $url, $assetid)
159 $cached = $this->
_getCache($cache_key, $perm_key, $url);
160 if (!is_null($cached)) {
162 if (time() > strtotime($cached[
'expires'])) {
166 if (file_exists(SQ_CACHE_PATH.
'/'.$cached[
'path'])) {
167 $contents = file_to_string(SQ_CACHE_PATH.
'/'.$cached[
'path']);
168 if ($contents === FALSE) {
171 trigger_localised_error(
'CORE0244', E_USER_WARNING, SQ_CACHE_PATH.
'/'.$cached[
'path']);
195 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
196 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
201 throw new Exception (
'Unable to purge cache due to database error: '.$e->getMessage());
204 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
205 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
233 if (!empty($assetids)) {
235 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
236 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
242 foreach (array_chunk($assetids, 999) as $chunk) {
243 $bind_vars = Array(
'assetids' => &$chunk);
247 throw new Exception(
'Unable to delete cache entries due to database error: '.$e->getMessage());
250 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
251 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
274 $bind_vars = Array(
'now' => ts_iso8601(time()));
278 }
catch (Exception $e) {
279 trigger_error(
'Unable to select expired cache entry paths due to database error: '.$e->getMessage(), E_USER_WARNING);
283 foreach ($paths as $k => $path) {
284 $cache_file = SQ_CACHE_PATH.
'/'.$path;
285 if (is_file($cache_file) === TRUE) {
286 $result = unlink($cache_file);
287 if ($result !== TRUE) {
288 trigger_error(
'Unable to remove expired cache file "'.$cache_file.
'"', E_USER_WARNING);
296 }
catch (Exception $e) {
297 trigger_error(
'Unable to delete expired cache entries due to database error: '.$e->getMessage(), E_USER_WARNING);
316 if (!empty($assetids)) {
317 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
322 foreach (array_chunk($assetids, 999) as $chunk) {
323 $bind_vars = Array(
'assetids' => &$chunk);
324 $result = array_merge($result,
MatrixDAL::executeAll(
'cache_manager',
'getCacheFilePathsByAsset', $bind_vars));
325 $result = array_unique($result);
328 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
345 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
348 if ($option ==
'col') {
354 throw new Exception (
'Unable to get all cache file paths due to database error: '.$e->getMessage());
357 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
378 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
382 'cache_key' => $cache_key,
383 'perm_key' => $perm_key,
388 throw new Exception(
'Unable to get cache entries due to database error: '.$e->getMessage());
391 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
393 return array_shift($result);
410 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
411 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
415 'cache_key' => $cache_key,
416 'perm_key' => $perm_key,
421 throw new Exception(
'Unable to delete cache entries due to database error: '.$e->getMessage());
424 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
425 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
443 function _updateCache($cache_key, $perm_key, $url, $expiry, $file_path=
'', $updated_content=
'')
446 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
447 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
451 'expires' => ts_iso8601($expiry),
452 'cache_key' => $cache_key,
453 'perm_key' => $perm_key,
458 throw new Exception(
'Unable to update cache expiry time due to database error: '.$e->getMessage());
461 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
462 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
466 $old_umask = umask(0);
467 $status = string_to_file($updated_content, SQ_CACHE_PATH.
'/'.$file_path);
469 trigger_localised_error(
'CORE0243', E_USER_WARNING, SQ_CACHE_PATH.
'/'.$file_path);
488 $cache_manager = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cache_manager');
490 $assetid = trim($assetid);
492 $bucketsize = $cache_manager->attr(
'num_cache_dirs');
493 $len = strlen($assetid);
494 for ($i = 0; $i < $len; $i++) {
495 if (!is_numeric($assetid{$i})) {
496 $hash += ord($assetid{$i});
498 $hash += (int) $assetid{$i};
501 $hash = $hash % $bucketsize;
503 while (strlen($hash) != strlen($bucketsize)) {
531 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
537 'url4' => $url.
'/?%',
541 if (!empty($cached_assets)) {
543 foreach ($cached_assets as $cached_asset) {
544 if (isset($cached_asset[
'path'])) {
545 $asset_cache_path = SQ_CACHE_PATH.
'/'.$cached_asset[
'path'];
546 if (is_file($asset_cache_path)) {
547 if (!unlink($asset_cache_path)) {
548 log_dump(
'Failed to delete cache file: '.$asset_cache_path);
555 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
558 $delete_bind_vars = Array(
562 'url4' => $url.
'/?%',
566 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
567 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
568 throw new Exception(
'Unable to delete cache entries due to database error: '.$e->getMessage());
571 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
573 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();