27 private $_cacheDir = null;
28 private $_cache = null;
29 private $_fileUtil = null;
44 $this->_cacheDir = WCMF_BASE.$cacheDir;
50 public function exists($section, $key) {
51 $this->initializeCache($section);
52 return isset($this->_cache[$section][$key]);
58 public function get($section, $key) {
59 $this->initializeCache($section);
60 if (isset($this->_cache[$section][$key])) {
61 return $this->_cache[$section][$key];
69 public function put($section, $key, $value) {
70 $this->initializeCache($section);
71 $this->_cache[$section][$key] = $value;
72 $this->saveCache($section);
78 public function clear($section) {
79 if (preg_match(
'/\*$/', $section)) {
81 $cachBaseDir = $this->getCacheDir();
82 $directory = $cachBaseDir.dirname($section);
83 if (is_dir($directory)) {
84 $pattern =
'/^'.preg_replace(
'/\*$/',
'', basename($section)).
'/';
85 $files = $this->_fileUtil->getFiles($directory, $pattern,
true,
true);
86 foreach ($files as $file) {
87 $this->
clear(str_replace($cachBaseDir,
'', $file));
89 $directories = $this->_fileUtil->getDirectories($directory, $pattern,
true,
true);
90 foreach ($directories as $directory) {
91 $this->
clear(str_replace($cachBaseDir,
'', $directory).
'/*');
97 $file = $this->getCacheFile($section);
99 unset($this->_cache[$section]);
107 $cacheDir = $this->getCacheDir();
108 if (is_dir($cacheDir)) {
109 $this->_fileUtil->emptyDir($cacheDir);
111 $this->_cache = null;
118 private function initializeCache($section) {
119 if (!isset($this->_cache[$section])) {
120 $file = $this->getCacheFile($section);
121 if (file_exists($file)) {
122 $this->_cache[$section] = unserialize(file_get_contents($file));
125 $this->_cache[$section] = array();
134 private function saveCache($section) {
135 $content = serialize($this->_cache[$section]);
136 $file = $this->getCacheFile($section);
137 $this->_fileUtil->mkdirRec(dirname($file));
138 $fh = fopen($file,
"w");
140 fwrite($fh, $content);
144 throw new ConfigurationException(
"The cache path is not writable: ".$file);
152 private function getCacheDir() {
153 if ($this->_cacheDir == null) {
154 $this->_cacheDir = session_save_path().DIRECTORY_SEPARATOR;
156 return $this->_cacheDir;
164 private function getCacheFile($section) {
165 return $this->getCacheDir().$section;
setCacheDir($cacheDir)
Set the cache directory (defaults to session_save_path if not given).
FileCache provides simple caching functionality.
__construct()
Constructor.
Cache defines the interface for cache implementations.
FileUtil provides basic support for file functionality like HTTP file upload.
put($section, $key, $value)