<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Here cache manager is defined.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
*/
require_once 'Cache/Lite.php';
/**
* Cache manager.
*
* @package AtleapLite
*/
class CacheManager extends Cache_Lite {
/**
* Page group
*
* @access private
*/
var $PAGE_GROUP = 'page';
/**
* Field group
*
* @access private
*/
var $FIELD_GROUP = 'field';
/**
* Menu pages group
*
* @access private
*/
var $MENU_PAGES_GROUP = 'menu_pages';
/**
* Resource group
*
* @access private
*/
var $RESOURCE_GROUP = 'resource';
/**
* Global properties group
*
* @access private
*/
var $GLOBAL_PROPERTIES_GROUP = 'global_properties';
/**
* Application resources group
*
* @access private
*/
var $APPLICATION_RESOURCES_GROUP = 'application_resources';
/**
* Constructor.
*
* @param array $options cache options
* @see Cache_Lite
*/
function CacheManager($options = array()) {
global $cacheDir;
if (!isset($options['cacheDir'])) {
$options['cacheDir'] = $cacheDir;
}
if (!isset($options['lifeTime'])) {
$options['lifeTime'] = 3600;
}
if (!isset($options['automaticSerialization'])) {
$options['automaticSerialization'] = true;
}
$this->Cache_Lite($options);
}
/**
* Puts a page to cache.
*
* @param object $page page to put
*/
function putPage($page) {
$id = $this->_getPageCacheId($page->identifier, $page->language);
$array = daoToArray($page, getPageDescriptor());
$array['id'] = $page->id;
$this->save($array, $id, $this->PAGE_GROUP);
}
/**
* Gets a page from cache.
*
* @param string $identifier page identifier
* @param string $language page language
* @return object|null page or null if not in cache
*/
function getPage($identifier, $language) {
$id = $this->_getPageCacheId($identifier, $language);
$data = $this->get($id, $this->PAGE_GROUP);
$dao = null;
if ($data) {
$dao =& getDao('page');
arrayToDao($data, $dao, getPageDescriptor());
$dao->id = $data['id'];
}
return $dao;
}
/**
* Removes a page from cache.
*
* @param string $identifier page identifier
* @param string $language page language
*/
function removePage($identifier, $language) {
$id = $this->_getPageCacheId($identifier, $language);
$this->remove($id, $this->PAGE_GROUP);
}
/**
* Puts a field to cache.
*
* @param object $field field to put
*/
function putField($field) {
$id = $this->_getFieldCacheId($field->page_id, $field->identifier);
$array = daoToArray($field, getFieldDescriptor());
$array['id'] = $field->id;
$this->save($array, $id, $this->FIELD_GROUP);
}
/**
* Gets a field from cache.
*
* @param int $pageId page ID
* @param string $identifier field identifier
* @return object|null field or null if not in cache
*/
function getField($pageId, $identifier) {
$id = $this->_getFieldCacheId($pageId, $identifier);
$data = $this->get($id, $this->FIELD_GROUP);
$dao = null;
if ($data) {
$dao =& getDao('field');
arrayToDao($data, $dao, getFieldDescriptor());
$dao->id = $data['id'];
}
return $dao;
}
/**
* Removes a field from cache.
*
* @param int $pageId page ID
* @param string $identifier field identifier
*/
function removeField($pageId, $identifier) {
$id = $this->_getFieldCacheId($pageId, $identifier);
$this->remove($id, $this->FIELD_GROUP);
}
/**
* Puts menu pages to cache.
*
* @param string $identifier menu identifier
* @param string $language language
* @param array $pages pages
*/
function putMenuPages($identifier, $language, $pages) {
$id = $this->_getMenuPagesCacheId($identifier, $language);
$arrays = array();
foreach ($pages as $key => $page) {
$array = daoToArray($page, getPageDescriptor());
$array['id'] = $page->id;
$arrays[$key] = $array;
}
$this->save($arrays, $id, $this->MENU_PAGES_GROUP);
}
/**
* Gets menu pages from cache.
*
* @param string $identifier menu identifier
* @param string $language language
* @return array|null pages or null if not in cache
*/
function getMenuPages($identifier, $language) {
$id = $this->_getMenuPagesCacheId($identifier, $language);
$data = $this->get($id, $this->MENU_PAGES_GROUP);
$daos = null;
if ($data) {
$daos = array();
foreach ($data as $key => $array) {
$dao =& getDao('page');
arrayToDao($array, $dao, getPageDescriptor());
$dao->id = $array['id'];
$daos[$key] = $dao;
}
}
return $daos;
}
/**
* Removes menu pages from cache.
*
* @param string $identifier menu identifier
* @param string $language language
*/
function removeMenuPages($identifier, $language) {
$id = $this->_getMenuPagesCacheId($identifier, $language);
$this->remove($id, $this->MENU_PAGES_GROUP);
}
/**
* Cleans all menu pages cache.
*/
function cleanMenuPages() {
$this->clean($this->MENU_PAGES_GROUP);
}
/**
* Puts a resource to cache.
*
* @param object $resource resource to put
*/
function putResource($resource) {
$id = $this->_getResourceCacheId($resource->file_name);
$array = daoToArray($resource, getResourceDescriptor());
$array['id'] = $resource->id;
// putting data explicitly as it's not put by default
$array['data'] = $resource->data;
$array['size'] = $resource->size;
$this->save($array, $id, $this->RESOURCE_GROUP);
}
/**
* Gets a resource from cache.
*
* @param string $fileName resource file name
* @return object|null resource or null if not in cache
*/
function getResource($fileName) {
$id = $this->_getResourceCacheId($fileName);
$data = $this->get($id, $this->RESOURCE_GROUP);
$dao = null;
if ($data) {
$dao =& getDao('resource');
arrayToDao($data, $dao, getResourceDescriptor());
$dao->id = $data['id'];
// putting data and size explicitly as it's not put by default
$dao->data = $data['data'];
$dao->size = $data['size'];
}
return $dao;
}
/**
* Removes a resource from cache.
*
* @param string $fileName resource file name
*/
function removeResource($fileName) {
$id = $this->_getResourceCacheId($fileName);
$this->remove($id, $this->RESOURCE_GROUP);
}
/**
* Puts global properties to cache.
*
* @param mixed $properties properties to put
*/
function putGlobalProperties($properties) {
$id = $this->_getGlobalPropertiesCacheId();
$this->save($properties, $id, $this->GLOBAL_PROPERTIES_GROUP);
}
/**
* Gets global properties from cache.
*
* @return mixed|null global properties or null if not in cache
*/
function getGlobalProperties() {
$id = $this->_getGlobalPropertiesCacheId();
return $this->get($id, $this->GLOBAL_PROPERTIES_GROUP);
}
/**
* Puts application resources to cache.
*
* @param mixed $resources resources to put
*/
function putApplicationResources($resources) {
$id = $this->_getApplicationResourcesCacheId();
$this->save($resources, $id, $this->APPLICATION_RESOURCES_GROUP);
}
/**
* Gets application resources from cache.
*
* @return mixed|null application resources or null if not in cache
*/
function getApplicationResources() {
$id = $this->_getApplicationResourcesCacheId();
return $this->get($id, $this->APPLICATION_RESOURCES_GROUP);
}
/**
* Flushes all cache.
*/
function flushAllCache() {
$this->clean();
}
/**
* Builds page cache ID.
*
* @param string $identifier page identifier
* @param string $language page language
* @return string cache ID
* @access private
*/
function _getPageCacheId($identifier, $language) {
return $this->_getCacheId($this->PAGE_GROUP, array($identifier, $language));
}
/**
* Builds field cache ID.
*
* @param int $pageId page ID
* @param string $identifier field identifier
* @return string cache ID
* @access private
*/
function _getFieldCacheId($pageId, $identifier) {
return $this->_getCacheId($this->FIELD_GROUP, array($pageId, $identifier));
}
/**
* Builds menu pages cache ID.
*
* @param string $identifier menu identifier
* @param string $language language
* @return string cache ID
* @access private
*/
function _getMenuPagesCacheId($identifier, $language) {
return $this->_getCacheId($this->MENU_PAGES_GROUP, array($identifier, $language));
}
/**
* Builds resource cache ID.
*
* @param string $fileName resource file name
* @return string cache ID
* @access private
*/
function _getResourceCacheId($fileName) {
return $this->_getCacheId($this->RESOURCE_GROUP, array($fileName));
}
/**
* Builds global properties cache ID.
*
* @return string cache ID
* @access private
*/
function _getGlobalPropertiesCacheId() {
return $this->_getCacheId($this->GLOBAL_PROPERTIES_GROUP, array());
}
/**
* Builds application resources cache ID.
*
* @return string cache ID
* @access private
*/
function _getApplicationResourcesCacheId() {
return $this->_getCacheId($this->APPLICATION_RESOURCES_GROUP, array());
}
/**
* Builds cache ID.
*
* @param string $category name of category
* @param array $ids several IDs
* @return string cache ID
* @access private
*/
function _getCacheId($category, $ids) {
return $category . "/" . implode("/", $ids);
}
}
?>