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