baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / cache / CacheManager.inc.php
blobf925dde035f6eff5ead7f3a47076f17ba012e206
1 <?php
3 /**
4 * @file classes/cache/CacheManager.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @ingroup cache
10 * @see GenericCache
12 * @brief Provides cache management functions.
16 // $Id: CacheManager.inc.php,v 1.7 2009/09/22 18:36:21 asmecher Exp $
19 class CacheManager {
20 /**
21 * Get the static instance of the cache manager.
22 * @return object CacheManager
24 function &getManager() {
25 $manager =& Registry::get('cacheManager', true, null);
26 if ($manager === null) {
27 $manager = new CacheManager();
29 return $manager;
32 /**
33 * Get a file cache.
34 * @param $context string
35 * @param $cacheId string
36 * @param $fallback callback
37 * @return object FileCache
39 function &getFileCache($context, $cacheId, $fallback) {
40 import('cache.FileCache');
41 $returner = new FileCache(
42 $context, $cacheId, $fallback,
43 $this->getFileCachePath()
45 return $returner;
48 /**
49 * Get a cache.
50 * @param $context string
51 * @param $cacheId string
52 * @param $fallback callback
53 * @return object Cache
55 function &getCache($context, $cacheId, $fallback) {
56 $cacheType = Config::getVar('cache','cache');
57 switch ($cacheType) {
58 case 'memcache':
59 import('cache.MemcacheCache');
60 $cache = new MemcacheCache(
61 $context, $cacheId, $fallback,
62 Config::getVar('cache','memcache_hostname'),
63 Config::getVar('cache','memcache_port')
65 break;
66 case '': // Provide a default if not specified
67 case 'file':
68 $cache =& $this->getFileCache($context, $cacheId, $fallback);
69 break;
70 case 'none':
71 import('cache.GenericCache');
72 $cache = new GenericCache(
73 $context, $cacheId, $fallback
75 break;
76 default:
77 die ("Unknown cache type \"$cacheType\"!\n");
78 break;
80 return $cache;
83 /**
84 * Get the path in which file caches will be stored.
85 * @return string The full path to the file cache directory
87 function getFileCachePath() {
88 return Core::getBaseDir() . DIRECTORY_SEPARATOR . 'cache';
91 /**
92 * Flush an entire context, if specified, or
93 * the whole cache.
94 * @param $context string The context to flush, if only one is to be flushed
96 function flush($context = null) {
97 $cacheType = Config::getVar('cache','cache');
98 switch ($cacheType) {
99 case 'memcache':
100 // There is no(t yet) selective flushing in memcache;
101 // invalidate the whole thing.
102 $junkCache =& $this->getCache(null, null, null);
103 $junkCache->flush();
104 break;
105 case 'file':
106 $filePath = $this->getFileCachePath();
107 $files = glob($filePath . DIRECTORY_SEPARATOR . 'fc-' . (isset($context)?$context . '-':'') . '*.php');
108 foreach ($files as $file) {
109 unlink ($file);
111 break;
112 case 'none':
113 // Nothing necessary.
114 break;
115 default:
116 die ("Unknown cache type \"$cacheType\"!\n");