Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Translation table cache.
4
 * @author $Author: weizhuo $
5
 * @version $Id: MessageCache.php 2488 2008-08-06 01:34:06Z knut $
6
 * @package System.I18N.core
7
 */
8
 
9
/**
10
 * Load the cache lite library.
11
 */
12
require_once(dirname(__FILE__).'/TCache_Lite.php');
13
 
14
/**
15
 * Cache the translation table into the file system.
16
 * It can cache each cataloug+variant or just the whole section.
17
 * @package System.I18N.core
18
 * @author $Author: weizhuo $
19
 * @version $Id: MessageCache.php 2488 2008-08-06 01:34:06Z knut $
20
 */
21
class MessageCache
22
{
23
 
24
	/**
25
	 * Cache Lite instance.
26
	 * @var TCache_Lite
27
	 */
28
	protected $cache;
29
 
30
	/**
31
	 * Caceh life time, default is 1 year.
32
	 */
33
	protected $lifetime = 3153600;
34
 
35
 
36
	/**
37
	 * Create a new Translation cache.
38
	 * @param string $cacheDir Directory to store the cache files.
39
	 */
40
	public function __construct($cacheDir)
41
	{
42
		$cacheDir = $cacheDir.'/';
43
 
44
		if(!is_dir($cacheDir))
45
			throw new Exception(
46
				'The cache directory '.$cacheDir.' does not exists.'.
47
				'The cache directory must be writable by the server.');
48
		if(!is_writable($cacheDir))
49
			throw new Exception(
50
				'The cache directory '.$cacheDir.' must be writable '.
51
				'by the server.');
52
 
53
		$options = array(
54
			'cacheDir' => $cacheDir,
55
			'lifeTime' => $this->getLifeTime(),
56
			'automaticSerialization' => true
57
		);
58
 
59
		$this->cache = new TCache_Lite($options);
60
	}
61
 
62
	/**
63
	 * Get the cache life time.
64
	 * @return int Cache life time.
65
	 */
66
	public function getLifeTime()
67
	{
68
		return $this->lifetime;
69
	}
70
 
71
	/**
72
	 * Set the cache life time.
73
	 * @param int $time Cache life time.
74
	 */
75
	public function setLifeTime($time)
76
	{
77
		$this->lifetime = (int)$time;
78
	}
79
 
80
	/**
81
	 * Get the cache file ID based section and locale.
82
	 * @param string $catalogue The translation section.
83
	 * @param string $culture The translation locale, e.g. "en_AU".
84
	 */
85
	protected function getID($catalogue, $culture)
86
	{
87
		return $catalogue.':'.$culture;
88
	}
89
 
90
	/**
91
	 * Get the cache file GROUP based section and locale.
92
	 * @param string $catalogue The translation section.
93
	 * @param string $culture The translation locale, e.g. "en_AU".
94
	 */
95
	protected function getGroup($catalogue, $culture)
96
	{
97
		return $catalogue.':'.get_class($this);
98
	}
99
 
100
	/**
101
	 * Get the data from the cache.
102
	 * @param string $catalogue The translation section.
103
	 * @param string $culture The translation locale, e.g. "en_AU".
104
	 * @param string $filename If the source is a file, this file's modified
105
	 * time is newer than the cache's modified time, no cache hit.
106
	 * @return mixed Boolean FALSE if no cache hit. Otherwise, translation
107
	 * table data for the specified section and locale.
108
	 */
109
	public function get($catalogue, $culture, $lastmodified=0)
110
	{
111
		$ID = $this->getID($catalogue, $culture);
112
		$group = $this->getGroup($catalogue, $culture);
113
 
114
		$this->cache->_setFileName($ID, $group);
115
 
116
		$cache = $this->cache->getCacheFile();
117
 
118
		if(is_file($cache) == false)
119
			return false;
120
 
121
 
122
		$lastmodified = (int)$lastmodified;
123
 
124
		if($lastmodified <= 0 || $lastmodified > filemtime($cache))
125
			return false;
126
 
127
		//echo '@@ Cache hit: "'.$ID.'" : "'.$group.'"';
128
		//echo "<br>\n";
129
 
130
		return $this->cache->get($ID, $group);
131
	}
132
 
133
	/**
134
	 * Save the data to cache for the specified section and locale.
135
	 * @param array $data The data to save.
136
	 * @param string $catalogue The translation section.
137
	 * @param string $culture The translation locale, e.g. "en_AU".
138
	 */
139
	public function save($data, $catalogue, $culture)
140
	{
141
		$ID = $this->getID($catalogue, $culture);
142
		$group = $this->getGroup($catalogue, $culture);
143
 
144
		//echo '## Cache save: "'.$ID.'" : "'.$group.'"';
145
		//echo "<br>\n";
146
 
147
		return $this->cache->save($data, $ID, $group);
148
	}
149
 
150
	/**
151
	 * Clean up the cache for the specified section and locale.
152
	 * @param string $catalogue The translation section.
153
	 * @param string $culture The translation locale, e.g. "en_AU".
154
	 */
155
	public function clean($catalogue, $culture)
156
	{
157
		$group = $this->getGroup($catalogue, $culture);
158
		$this->cache->clean($group);
159
	}
160
 
161
	/**
162
	 * Flush the cache. Deletes all the cache files.
163
	 */
164
	public function clear()
165
	{
166
		$this->cache->clean();
167
	}
168
 
169
}
170
 
171
?>