mihasya / mihasya_libs

Some PHP libs

This URL has Read+Write access

mihasya_libs / mihasya_cache.php
430d4b1e » Mikhail P 2008-09-01 initial import 1 <?php
2 abstract class CacheNamespace {
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 3 public static $handlers;
4 public static $conf = array();
430d4b1e » Mikhail P 2008-09-01 initial import 5 }
6
7 function cache($hdlName) {
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 8 if (!CacheNamespace::$handlers[$hdlName]) {
9 if (!CacheNamespace::$conf[$hdlName]) return false; //init hasnt been run
10 //go on and create the connection
11 $conf = CacheNamespace::$conf[$hdlName];
12 $className = $conf['type'].'CacheHdl';
13 $hdl = new $className($conf['initArgs']);
14 CacheNamespace::$handlers[$hdlName] = $hdl;
15 }
16 return CacheNamespace::$handlers[$hdlName];
430d4b1e » Mikhail P 2008-09-01 initial import 17 }
18
19 function cache_init($hdlName, $type='file', $initArgs = array()) {
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 20 $conf = array(
21 'type'=>$type,
22 'initArgs'=>$initArgs
23 );
24 CacheNamespace::$conf[$hdlName] = $conf;
430d4b1e » Mikhail P 2008-09-01 initial import 25 }
26
27 abstract class cacheHdl {
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 28 abstract function __construct($initArgs);
29 abstract function get($key);
30 abstract function set($key, $value, $ttl=null);
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 31 abstract function delete($key, $timeout=null);
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 32 protected function _getRealKey($key) {
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 33 //TODO: make this modifiable so user can define own hash/salt
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 34 return md5($key);
35 }
430d4b1e » Mikhail P 2008-09-01 initial import 36 }
37
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 38 class apcCacheHdl extends cacheHdl{
39 function __construct($initArgs) {
40 if (!function_exists('apc_fetch')) {
41 throw new Exception ("APC does not appear to be enabled");
42 }
43 }
44 function get($key) {
45 return apc_fetch($this->_getRealKey($key));
46 }
47 function set($key, $value, $ttl=null) {
48 return apc_store($this->_getRealKey($key), $value, $ttl);
49 }
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 50 function delete($key, $timeout=null) {
51 if (!$timeout) {
52 return apc_delete($this->_getRealKey($key));
53 } else {
54 return apc_store($this->_getRealKey($key), $value, $timeout);
55 }
56 }
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 57 }
430d4b1e » Mikhail P 2008-09-01 initial import 58 class fileCacheHdl extends cacheHdl{
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 59 private $_path;
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 60
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 61 function __construct($initArgs) {
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 62 $this->_path = $initArgs['path'] ? $initArgs['path'] : '.';
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 63 }
64 function get($key) {
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 65 $realKey = $this->_getRealKey($key);
66 $keyPath = $this->_path.'/'.$realKey;
67 try {
0be3ee6a » mihasya 2008-12-20 more error suppression 68 $fcontent = @fread(@fopen($keyPath, 'r'), filesize($keyPath));
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 69 } catch (Exception $e) {
70 return false;
71 }
72 if (!$fcontent) { return false; }
73 $content = unserialize($fcontent);
9db2f367 » mihasya 2008-12-20 couple of minor logicfixes 74 if ($content->ttl < time() && $content->ttl != 0) {
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 75 unlink($keyPath);
76 return false;
77 }
78 return $content->value;
8a1de7a3 » mihasya 2008-10-18 added apc handler; refactor... 79 }
80 function set($key, $value, $ttl=null) {
81 $realKey = $this->_getRealKey($key);
82 $ttl = $ttl ? time()+$ttl : 0;
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 83 $content = new stdClass();
84 $content->ttl = $ttl;
85 $content->value = $value;
9db2f367 » mihasya 2008-12-20 couple of minor logicfixes 86 $f = fopen($this->_path.'/'.$realKey, 'w+');
0be3ee6a » mihasya 2008-12-20 more error suppression 87 return @fwrite(@fopen($this->_path.'/'.$realKey, 'w+'), serialize($content));
92c13d7a » mihasya 2008-12-20 finished fileCache set/get ... 88 }
89 function delete($key, $timeout=null) {
90 if (!$timeout) {
91 return unlink($this->_path.'/'.$this->_getRealKey($key));
92 } else {
93 if (!$value = $this->get($key)) {
94 return false;
95 }
96 return $this->set($key, $value, $timeout);
97 }
430d4b1e » Mikhail P 2008-09-01 initial import 98 }
99 }
100 ?>