com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_cache.php

Documentation is available at lib_cache.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains functions and classes related to caching contents
  9. * in files. Contents should be cached this way if it is rarely updated, only
  10. * wanted in one specific form and often read.
  11. * @author Andreas Launila
  12. * @version $Revision: 1.1 $
  13. * @package com.lokorin.lokorin.lib
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. */
  16.  
  17. /**
  18. * Checks if there is a cache related to the specified unique identifier.
  19. * @param string $uniqueId The unique identifier that will be checked for
  20. * connections to existing cached files.
  21. * @return boolean True if a cached file exists for the identifier, false
  22. * otherwise.
  23. * @access public
  24. */
  25. function cacheExists($uniqueId) {
  26. return file_exists(getCacheFile($uniqueId));
  27. }
  28.  
  29. /**
  30. * Gets the cache associated with the specified unique identifier.
  31. * @param string $uniqueId The unique identifier that identifies the cache
  32. * that should be retrieved. This is the unique identifier that was given
  33. * when writing the cache.
  34. * @return string The specified cache contents.
  35. * @throws IllegalArgumentException If there is no cache associated with the
  36. * specified identifier.
  37. * @access public
  38. */
  39. function getCache($uniqueId) {
  40. $cacheFile = getCacheFile($uniqueId);
  41. if(!file_exists($cacheFile)) {
  42. throw new IllegalArgumentException("No cache exists for the specified ".
  43. "identifier (".$uniqueId.").");
  44. }
  45. return file_get_contents($cacheFile);
  46. }
  47.  
  48. /**
  49. * Writes a specified cache contents and associates it with the specified
  50. * unique identifier. This function will overwrite any existing cache with the
  51. * same identifier.
  52. * @param string $uniqueId The unique identifier that should identify the
  53. * cache.
  54. * @param string $contents The contents that should be cached.
  55. * @access public
  56. */
  57. function writeCache($uniqueId, $contents) {
  58. file_put_contents(getCacheFile($uniqueId), $contents);
  59. }
  60.  
  61. /**
  62. * Deletes the cache with the specified unique identifier.
  63. * @param string $uniqueId The unique identifier that identifies the cache that
  64. * should be deleted.
  65. * @return boolean True if the cache was successfully deleted, false otherwise.
  66. * @access public
  67. */
  68. function deleteCache($uniqueId) {
  69. return unlink(getCacheFile($uniqueId));
  70. }
  71.  
  72. /**
  73. * Gets the absolute path of the file connected to the specified identifier.
  74. * @param string $identifier The identifier for which the file name should be
  75. * retrieved.
  76. * @access private
  77. */
  78. function getCacheFile($identifier) {
  79. return DOC_ROOT.FOLDER_CACHE.md5($identifier).'.chc';
  80. }
  81.  
  82. /**
  83. * Deletes all contents that is currently cached.
  84. */
  85. function deleteAllCachedContents() {
  86. $fileList = array();
  87. $folder = DOC_ROOT.FOLDER_CACHE;
  88. if($dir = @opendir($folder)) {
  89. while(($file = readdir($dir)) !== false) {
  90. if(substr($file, -4) == '.chc') {
  91. unlink($folder.$file);
  92. }
  93. }
  94. closedir($dir);
  95. }
  96. }
  97.  
  98. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:10 +0200 by phpDocumentor 1.3.0RC4