/
ClassCache.class.php
177 lines (160 loc) · 4.16 KB
/
ClassCache.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
*
* Copyright Feb 28, 2012 Chris Keeley <info@chriskeeley.co.uk> All rights reserved.
*
* Description of ClassCache
*
* @author chris
*/
require_once 'SystemFile.class.php';
/**
* A file backed class cache.
*/
class ClassCache {
/**
* Persistent storage for the cache.
* @var type
* @access private
*/
private $cacheFile;
/**
* Runtime storage for the persistent cache.
* @var type
* @access private
*/
private $classMap;
/**
* Static instance holds an instantiation
* of the ClassCache to avoid multiple invocation overhead
* @static
* @access public
*/
public static $instance;
/**
*
* @var type
* @access private
*/
private $entries;
/**
* Flag denoting whether or not the cache has been opened.
* @var boolean true if the cache is open, false if not
* @access private
*/
private $open;
/**
* Logger instance
* @var Logger
*/
private $logger;
/**
* True if the cache has at least one entry
* @var type
*/
private $hasEntries;
/**
* Class constructor.
* @access public
*/
public function __construct() {
$this->cacheFile = new SystemFile(AUTOLOAD_CACHE_FILE);
$this->classMap = array();
$this->open = false;
$this->hasEntries = false;
$this->logger = Logger::getLogger(__CLASS__);
$this->openCache();
}
/**
* Get an instance of the ClassCache
* @return ClassCache
* @access public
* @static
*/
public static function instance() {
if (!isset(self::$instance)) {
self::$instance = new ClassCache();
}
return self::$instance;
}
/**
* Write the contents of the classMap to persistent storage.
* @access public
* @return void
*/
public function write() {
$cacheSZ = serialize($this->classMap);
$this->cacheFile->write($cacheSZ);
}
/**
* Lookup an entry in the cache
* @access public
* @return void
*/
public function lookup($className) {
if(!$this->open) {
$this->openCache();
}
/**
* Check first to see if its worth looking
* in the cache
*/
if($this->hasEntries) {
$hash = md5(strtolower($className));
if(isset($this->classMap[$hash])) {
$aClass = $this->classMap[$hash];
$aClass->incrementHitCount();
$this->classMap[$hash] = $aClass;
return $this->classMap[$hash];
}
}
return false;
}
/**
* Load the cache with the contents from persistent storage
* @access private
*/
private function openCache() {
if(!$this->open) {
$this->classMap = $this->getCacheFileAsSet();
if(count($this->classMap)) {
$this->hasEntries = true;
}
$this->open = true;
}
}
/**
* the cache data file is a serialized array.
* we read in the serialized data and hydrate back into array format.
* @return array contents of the persistent storage
* @access private
*/
private function getCacheFileAsSet() {
$contents = file_get_contents($this->cacheFile->pathName);
$cacheDataSet = array();
if($contents) {
$cacheDataSet = unserialize($contents);
}
return $cacheDataSet;
}
/**
* Add a new ClassMap entry into the classMap
* @param ClassMapEntry $entry
* @return void
* @access public
*/
public function newEntry(ClassMapEntry $entry) {
$hash = md5(strtolower($entry->getClassName()));
$this->classMap[$hash] = $entry;
$this->logger->debug("Cache entry: {$entry->getClassName()}.{$entry->getClassSuffix()}");
}
/**
* Make sure we sync the contents of the classMap with persistent storage.
* @access public
* @return void
*/
public function __destruct() {
$this->write();
}
}
?>