Development

/branches/1.0/lib/helper/CacheHelper.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/helper/CacheHelper.php

Revision 5085, 1.8 kB (checked in by fabien, 7 years ago)

fixed cache() typo

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * CacheHelper.
13  *
14  * @package    symfony
15  * @subpackage helper
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19
20 /* Usage
21
22 <?php if (!cache('name')): ?>
23
24 ... HTML ...
25
26   <?php cache_save() ?>
27 <?php endif; ?>
28
29 */
30 function cache($name, $lifeTime = 86400)
31 {
32   $context = sfContext::getInstance();
33
34   if (!sfConfig::get('sf_cache'))
35   {
36     return null;
37   }
38
39   $request = $context->getRequest();
40   $cache   = $context->getViewCacheManager();
41
42   if (!is_null($request->getAttribute('started', null, 'symfony/action/sfAction/cache')))
43   {
44     throw new sfCacheException('Cache already started');
45   }
46
47   $data = $cache->start($name, $lifeTime);
48
49   if ($data === null)
50   {
51     $request->setAttribute('started', 1, 'symfony/action/sfAction/cache');
52     $request->setAttribute('current_name', $name, 'symfony/action/sfAction/cache');
53
54     return 0;
55   }
56   else
57   {
58     echo $data;
59
60     return 1;
61   }
62 }
63
64 function cache_save()
65 {
66   $context = sfContext::getInstance();
67
68   if (!sfConfig::get('sf_cache'))
69   {
70     return null;
71   }
72
73   $request = $context->getRequest();
74
75   if (is_null($request->getAttribute('started', null, 'symfony/action/sfAction/cache')))
76   {
77     throw new sfCacheException('Cache not started');
78   }
79
80   $name = $request->getAttribute('current_name', '', 'symfony/action/sfAction/cache');
81
82   $data = $context->getViewCacheManager()->stop($name);
83
84   $request->setAttribute('started', null, 'symfony/action/sfAction/cache');
85   $request->setAttribute('current_name', null, 'symfony/action/sfAction/cache');
86
87   echo $data;
88 }
89
Note: See TracBrowser for help on using the browser.