/nacridan/translation/MessageCache.php

https://gitlab.com/nacridan/Nacridan · PHP · 189 lines · 66 code · 26 blank · 97 comment · 6 complexity · 10fd2ded8024b0d0e82c1db637dcbc1c MD5 · raw file

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