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