You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

  1. <?php
  2. /**
  3. * Класс кеша.
  4. * Отвечает за кеширование результатов выполнения действий.
  5. *
  6. * @copyright
  7. * @link
  8. * @package Majestic
  9. * @subpackage Cache
  10. * @since
  11. * @version SVN: $Id$
  12. * @filesource $URL$
  13. */
  14. final class Cache
  15. {
  16. private $cache_file;
  17. private $cache_time;
  18. function __construct($cache_name, $cache_time)
  19. {
  20. $this->cache_time = (int) $cache_time;
  21. $this->cache_file = CACHE_PATH.'/'.$cache_name;
  22. }
  23. /**
  24. * Сохраняет кэш в файл
  25. *
  26. * @return boolean - сохранил или нет
  27. */
  28. function save($data)
  29. {
  30. if ($this->cache_time != 0) {
  31. return (bool) file_put_contents($this->cache_file, $data);
  32. }
  33. return false;
  34. }
  35. /**
  36. * Достает кэш из файла
  37. *
  38. * @return string - содержимое кеша
  39. */
  40. function load()
  41. {
  42. return file_get_contents($this->cache_file);
  43. }
  44. /**
  45. * Проверяет, закешированы ли данные с таким именем
  46. *
  47. * @return boolean - закеширован или нет
  48. */
  49. function isCached()
  50. {
  51. if (! file_exists($this->cache_file)) {
  52. return false;
  53. }
  54. if ($this->cache_time > 0 && $this->cache_time + filemtime($this->cache_file) < TIME_NOW) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. }
  60. ?>