CacheHelper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * CacheHelper.
  11. *
  12. * @package symfony
  13. * @subpackage helper
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: CacheHelper.php 8443 2008-04-14 14:02:47Z fabien $
  16. */
  17. /* Usage
  18. <?php if (!cache('name')): ?>
  19. ... HTML ...
  20. <?php cache_save() ?>
  21. <?php endif; ?>
  22. */
  23. function cache($name, $lifeTime = 86400)
  24. {
  25. if (!sfConfig::get('sf_cache'))
  26. {
  27. return null;
  28. }
  29. $cache = sfContext::getInstance()->getViewCacheManager();
  30. if (sfConfig::get('symfony.cache.started'))
  31. {
  32. throw new sfCacheException('Cache already started.');
  33. }
  34. $data = $cache->start($name, $lifeTime);
  35. if (is_null($data))
  36. {
  37. sfConfig::set('symfony.cache.started', true);
  38. sfConfig::set('symfony.cache.current_name', $name);
  39. return false;
  40. }
  41. else
  42. {
  43. echo $data;
  44. return true;
  45. }
  46. }
  47. function cache_save()
  48. {
  49. if (!sfConfig::get('sf_cache'))
  50. {
  51. return null;
  52. }
  53. if (!sfConfig::get('symfony.cache.started'))
  54. {
  55. throw new sfCacheException('Cache not started.');
  56. }
  57. $data = sfContext::getInstance()->getViewCacheManager()->stop(sfConfig::get('symfony.cache.current_name', ''));
  58. sfConfig::set('symfony.cache.started', false);
  59. sfConfig::set('symfony.cache.current_name', null);
  60. echo $data;
  61. }