| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|