20 define(
'SQ_ASSET_CACHE_DEBUG', 0);
51 var $_items = Array();
60 var $_items_released = Array();
102 $this->_size_rules[
'max'] = $size;
103 $this->_size_rules[
'all_items'] = $all_items;
119 function add($item, $itemid=-1)
123 if (!isset($item) || !isset($item->id)) {
130 if ($this->
get($itemid))
return false;
132 $this->_items[$itemid] = Array(
'item' => $item,
'reference_count' => 1);
134 if (SQ_ASSET_CACHE_DEBUG) {
152 function get($itemid)
156 if (isset($this->_items[$itemid])) {
157 $item_info = &$this->_items[$itemid];
159 $ret = &$item_info[
'item'];
161 if ($item_info[
'reference_count'] == 0) {
162 unset($this->_items_released[$itemid]);
165 $item_info[
'reference_count']++;
167 if (SQ_ASSET_CACHE_DEBUG) {
189 if (!isset($this->_items[$itemid]))
return false;
191 $item_info = &$this->_items[$itemid];
193 if ($item_info[
'reference_count'] <= 0)
return false;
195 $item_info[
'reference_count']--;
197 if (SQ_ASSET_CACHE_DEBUG) {
201 if ($item_info[
'reference_count'] == 0) {
202 $this->_items_released[$itemid] = $itemid;
221 if ($this->_size_rules[
'max'] == -1)
return;
223 if (count($this->_items_released) && $this->
getSize(
true)) {
224 $current_size = $this->
getSize(!$this->_size_rules[
'all_items']);
226 if ($current_size > $this->_size_rules[
'max']) {
228 reset($this->_items_released);
229 $this->
remove(current($this->_items_released));
248 function remove($itemid, $force=
false)
250 if (!$force && ($this->_items[$itemid][
'reference_count'] != 0)) {
254 unset($this->_items[$itemid]);
255 unset($this->_items_released[$itemid]);
270 $itemsid = array_keys($this->_items_released);
271 foreach ($itemsid as $itemid) {
272 $this->
remove($itemid);
289 return $released ? count($this->_items_released) : count($this->_items);
307 echo
"Asset Cache report:\n";
310 foreach ($this->_items as $itemid => $item_info) {
311 if ($item_info[
'reference_count'] > 0) {
312 echo
'Asset #'.$itemid.
' ('.get_class($item_info[
'item']).
') RC='.$item_info[
'reference_count'].
"\n";
313 if (isset($item_info[
'callers'])) {
314 print_r ($item_info[
'callers']);
319 echo $item_count.
" items \n";
321 $ret = ob_get_clean();
341 $bt = debug_backtrace();
344 $excluded_classes = Array(
'asset_manager',
'asset', strtolower(__CLASS__));
345 for ($caller_idx = 0; $caller_idx < count($bt); $caller_idx++) {
346 $class = (isset($bt[$caller_idx][
'class'])) ? $bt[$caller_idx][
'class'] :
'';
347 if (!in_array(strtolower($class), $excluded_classes)) {
351 if ($caller_idx < count($bt)) {
352 $caller = (isset($bt[$caller_idx][
'class'])) ? $bt[$caller_idx][
'class'] :
'';
353 $caller .=
':'.$bt[$caller_idx][
'file'];
357 if (!isset($this->_items[$itemid][
'callers'])) {
358 $this->_items[$itemid][
'callers'] = Array();
360 if (!isset($this->_items[$itemid][
'callers'][$caller])) {
361 $this->_items[$itemid][
'callers'][$caller] = 0;
363 $this->_items[$itemid][
'callers'][$caller] += $rc_diff;
376 return array_keys($this->_items);