Merge pull request #389 from colemanw/permissions
[civicrm-core.git] / CRM / Utils / Cache / SerializeCache.php
CommitLineData
6a488035
TO
1<?php
2
3class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface {
4
5 /**
6 * The cache storage container, an array by default, stored in a file under templates
7 */
8 private $_cache;
9
10 /**
11 * Constructor
12 *
13 * @param array $config an array of configuration params
14 *
15 * @return void
16 */
17 function __construct($config) {
18 $this->_cache = array();
19 }
20
21 function fileName ($key) {
22 if (strlen($key) > 50)
23 return CIVICRM_TEMPLATE_COMPILEDIR ."CRM_".md5($key).".php";
24 return CIVICRM_TEMPLATE_COMPILEDIR .$key.".php";
25 }
26
27 function get ($key) {
28 if (array_key_exists($key,$this->_cache))
29 return $this->_cache[$key];
30
31 if (!file_exists($this->fileName ($key))) {
32 return;
33 }
34 $this->_cache[$key] = unserialize (substr (file_get_contents ($this->fileName ($key)),8));
35 return $this->_cache[$key];
36 }
37
38 function set($key, &$value) {
39 if (file_exists($this->fileName ($key))) {
40 return;
41 }
42 $this->_cache[$key] = $value;
43 file_put_contents ($this->fileName ($key),"<?php //".serialize ($value));
44 }
45
46 function delete($key) {
47 if (file_exists($this->fileName ($key))) {
48 unlink ($this->fileName ($key));
49 }
50 unset($this->_cache[$key]);
51 }
52
53 function flush($key =null) {
54 $prefix = "CRM_";
55 if (!$handle = opendir(CIVICRM_TEMPLATE_COMPILEDIR)) {
56 return; // die? Error?
57 }
58 while (false !== ($entry = readdir($handle))) {
59 if (substr ($entry,0,4) == $prefix) {
60 unlink (CIVICRM_TEMPLATE_COMPILEDIR.$entry);
61 }
62 }
63 closedir($handle);
64 unset($this->_cache);
65 $this->_cache = array();
66 }
67}
68