Development

/branches/1.0/lib/i18n/sfMessageCache.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/i18n/sfMessageCache.class.php

Revision 6806, 3.3 kB (checked in by fabien, 7 years ago)

fixed phpdoc package/subpackage entries

  • 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  * Translation table cache.
4  * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
5  * @version    $Id$
6  * @package    symfony
7  * @subpackage i18n
8  */
9
10 /**
11  * Cache the translation table into the file system.
12  * It can cache each cataloug+variant or just the whole section.
13  *
14  * @package    symfony
15  * @subpackage i18n
16  * @author $Author: weizhuo $
17  * @version $Id$
18  */
19 class sfMessageCache
20 {
21   /**
22    * Cache Lite instance.
23    */
24   protected $cache;
25
26   /**
27    * Cache life time, default is 1 year.
28    */
29   protected $lifetime = 3153600;
30
31   /**
32    * Creates a new Translation cache.
33    *
34    * @param string $cacheDir Directory to store the cache files.
35    */
36   public function initialize($options = array())
37   {
38     $this->cache = new sfFileCache();
39     $this->cache->initialize($options);
40   }
41
42   /**
43    * Gets the cache life time.
44    *
45    * @return int Cache life time.
46    */
47   public function getLifeTime()
48   {
49     return $this->lifetime;
50   }
51
52   /**
53    * Sets the cache life time.
54    *
55    * @param int $time Cache life time.
56    */
57   public function setLifeTime($time)
58   {
59     $this->lifetime = intval($time);
60   }
61
62   /**
63    * Gets the cache file ID based section and locale.
64    *
65    * @param string $catalogue The translation section.
66    * @param string $culture The translation locale, e.g. "en_AU".
67    */
68   protected function getID($catalogue, $culture)
69   {
70     return $culture;
71   }
72
73   /**
74    * Gets the cache file GROUP based section and locale.
75    *
76    * @param string $catalogue The translation section.
77    * @param string $culture The translation locale, e.g. "en_AU".
78    */
79   protected function getGroup($catalogue, $culture)
80   {
81     return $catalogue;
82   }
83
84   /**
85    * Gets the data from the cache.
86    *
87    * @param string $catalogue The translation section.
88    * @param string $culture The translation locale, e.g. "en_AU".
89    * @param string $filename If the source is a file, this file's modified time is newer than the cache's modified time, no cache hit.
90    * @return mixed Boolean FALSE if no cache hit. Otherwise, translation
91    * table data for the specified section and locale.
92    */
93   public function get($catalogue, $culture, $lastmodified = 0)
94   {
95     $ID = $this->getID($catalogue, $culture);
96     $group = $this->getGroup($catalogue, $culture);
97
98     if ($lastmodified <= 0 || $lastmodified > $this->cache->lastModified($ID, $group))
99     {
100       return false;
101     }
102
103     return unserialize($this->cache->get($ID, $group));
104   }
105
106   /**
107    * Saves the data to cache for the specified section and locale.
108    *
109    * @param array $data The data to save.
110    * @param string $catalogue The translation section.
111    * @param string $culture The translation locale, e.g. "en_AU".
112    */
113   public function save($data, $catalogue, $culture)
114   {
115     $ID = $this->getID($catalogue, $culture);
116     $group = $this->getGroup($catalogue, $culture);
117
118     return $this->cache->set($ID, $group, serialize($data));
119   }
120
121   /**
122    * Cleans up the cache for the specified section and locale.
123    *
124    * @param string $catalogue The translation section.
125    * @param string $culture The translation locale, e.g. "en_AU".
126    */
127   public function clean($catalogue, $culture)
128   {
129     $group = $this->getGroup($catalogue, $culture);
130     $this->cache->clean($group);
131   }
132
133   /**
134    * Flushes the cache. Deletes all the cache files.
135    */
136   public function clear()
137   {
138     $this->cache->clean();
139   }
140 }
141
Note: See TracBrowser for help on using the browser.