Source for file CachingHelper.php

Documentation is available at CachingHelper.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Caching template helper.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Templates
  33. 33:  */
  34. 34: class CachingHelper extends Object
  35. 35: {
  36. 36:     /** @var array */
  37. 37:     private $frame;
  38. 38:  
  39. 39:     /** @var string */
  40. 40:     private $key;
  41. 41:  
  42. 42:  
  43. 43:  
  44. 44:     /**
  45. 45:      * Starts the output cache. Returns CachingHelper object if buffering was started.
  46. 46:      * @param  string 
  47. 47:      * @param  string 
  48. 48:      * @param  array 
  49. 49:      * @return CachingHelper 
  50. 50:      */
  51. 51:     public static function create($key$file$tags)
  52. 52:     {
  53. 53:         $cache self::getCache();
  54. 54:         if (isset($cache[$key])) {
  55. 55:             echo $cache[$key];
  56. 56:             return FALSE;
  57. 57:  
  58. 58:         else {
  59. 59:             $obj new self;
  60. 60:             $obj->key $key;
  61. 61:             $obj->frame array(
  62. 62:                 Cache::FILES => array($file),
  63. 63:                 Cache::TAGS => $tags,
  64. 64:                 Cache::EXPIRE => rand(86400 486400 7),
  65. 65:             );
  66. 66:             ob_start();
  67. 67:             return $obj;
  68. 68:         }
  69. 69:     }
  70. 70:  
  71. 71:  
  72. 72:  
  73. 73:     /**
  74. 74:      * Stops and saves the cache.
  75. 75:      * @return void 
  76. 76:      */
  77. 77:     public function save()
  78. 78:     {
  79. 79:         $this->getCache()->save($this->keyob_get_flush()$this->frame);
  80. 80:         $this->key $this->frame NULL;
  81. 81:     }
  82. 82:  
  83. 83:  
  84. 84:  
  85. 85:     /**
  86. 86:      * Adds the file dependency.
  87. 87:      * @param  string 
  88. 88:      * @return void 
  89. 89:      */
  90. 90:     public function addFile($file)
  91. 91:     {
  92. 92:         $this->frame[Cache::FILES][$file;
  93. 93:     }
  94. 94:  
  95. 95:  
  96. 96:  
  97. 97:     /**
  98. 98:      * Adds the cached item dependency.
  99. 99:      * @param  string 
  100. 100:      * @return void 
  101. 101:      */
  102. 102:     public function addItem($item)
  103. 103:     {
  104. 104:         $this->frame[Cache::ITEMS][$item;
  105. 105:     }
  106. 106:  
  107. 107:  
  108. 108:  
  109. 109:     /********************* backend ****************d*g**/
  110. 110:  
  111. 111:  
  112. 112:  
  113. 113:     /**
  114. 114:      * @return Cache 
  115. 115:      */
  116. 116:     protected static function getCache()
  117. 117:     {
  118. 118:         return Environment::getCache('Nette.Template.Curly');
  119. 119:     }
  120. 120: