- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains functions and classes related to caching contents
- * in files. Contents should be cached this way if it is rarely updated, only
- * wanted in one specific form and often read.
- * @author Andreas Launila
- * @version $Revision: 1.1 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * Checks if there is a cache related to the specified unique identifier.
- * @param string $uniqueId The unique identifier that will be checked for
- * connections to existing cached files.
- * @return boolean True if a cached file exists for the identifier, false
- * otherwise.
- * @access public
- */
- function cacheExists($uniqueId) {
- return file_exists(getCacheFile($uniqueId));
- }
-
- /**
- * Gets the cache associated with the specified unique identifier.
- * @param string $uniqueId The unique identifier that identifies the cache
- * that should be retrieved. This is the unique identifier that was given
- * when writing the cache.
- * @return string The specified cache contents.
- * @throws IllegalArgumentException If there is no cache associated with the
- * specified identifier.
- * @access public
- */
- function getCache($uniqueId) {
- $cacheFile = getCacheFile($uniqueId);
- if(!file_exists($cacheFile)) {
- throw new IllegalArgumentException("No cache exists for the specified ".
- "identifier (".$uniqueId.").");
- }
- return file_get_contents($cacheFile);
- }
-
- /**
- * Writes a specified cache contents and associates it with the specified
- * unique identifier. This function will overwrite any existing cache with the
- * same identifier.
- * @param string $uniqueId The unique identifier that should identify the
- * cache.
- * @param string $contents The contents that should be cached.
- * @access public
- */
- function writeCache($uniqueId, $contents) {
- file_put_contents(getCacheFile($uniqueId), $contents);
- }
-
- /**
- * Deletes the cache with the specified unique identifier.
- * @param string $uniqueId The unique identifier that identifies the cache that
- * should be deleted.
- * @return boolean True if the cache was successfully deleted, false otherwise.
- * @access public
- */
- function deleteCache($uniqueId) {
- return unlink(getCacheFile($uniqueId));
- }
-
- /**
- * Gets the absolute path of the file connected to the specified identifier.
- * @param string $identifier The identifier for which the file name should be
- * retrieved.
- * @access private
- */
- function getCacheFile($identifier) {
- return DOC_ROOT.FOLDER_CACHE.md5($identifier).'.chc';
- }
-
- /**
- * Deletes all contents that is currently cached.
- */
- function deleteAllCachedContents() {
- $fileList = array();
- $folder = DOC_ROOT.FOLDER_CACHE;
- if($dir = @opendir($folder)) {
- while(($file = readdir($dir)) !== false) {
- if(substr($file, -4) == '.chc') {
- unlink($folder.$file);
- }
- }
- closedir($dir);
- }
- }
-
- ?>