phpist

Untitled

Oct 26th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <?php ## Базовый класс для кэшируемых страниц
  2. require_once "PageA.php";
  3.  
  4. abstract class Cached extends Page
  5. {
  6. // Время действия кэша
  7. protected $expires;
  8. // Хранилище
  9. protected $store;
  10.  
  11. // Конструктор класса
  12. public function __construct($title = '', $content = '', $expires = 0)
  13. {
  14. // Вызываем конструктор базового класса Page
  15. parent::__construct($title, $content);
  16. // Устанавливаем время жизни кэша
  17. $this->expires = $expires;
  18. // Подготовка хранилища
  19. // $this->store = new Memcached();
  20. // $this->store->addServer('localhost', 11211);
  21. // Размещение данных в хранилище
  22. $this->set($this->id('title'), $title);
  23. $this->set($this->id('content'), $content);
  24. }
  25.  
  26. // Проверить есть ли позиция $key в кэше
  27. protected function isCached($key)
  28. {
  29. // return (bool) $this->store->get($key);
  30. }
  31. // Поместить в кэш по ключу $key значение $value
  32. // В случае если ключ уже существует:
  33. // 1. Не делать ничего, если $force принимает значение false
  34. // 2. Переписать, если $force принимает значение true
  35. protected function set($key, $value, $force = false)
  36. {
  37. // if ($force) {
  38. // $this->store->set($key, $value, $this->expires);
  39. // } else {
  40. // if($this->isCached($key)) {
  41. // $this->store->set($key, $value, $this->expires);
  42. // }
  43. // }
  44. }
  45. // Извлечение значения $key из кэша
  46. protected function get($key)
  47. {
  48. // return $this->store->get($key);
  49. }
  50.  
  51. // Формируем уникальный ключ для хранилища
  52. abstract public function id($name);
  53.  
  54. // Получение заголовка страницы
  55. public final function title()
  56. {
  57. // if ($this->isCached($this->id('title'))) {
  58. // return $this->get($this->id('title'));
  59. // } else {
  60. return parent::title();
  61. // }
  62. }
  63. // Получение содержимое страницы
  64. public final function content()
  65. {
  66. // if ($this->isCached($this->id('content'))) {
  67. // return $this->get($this->id('content'));
  68. // } else {
  69. return parent::content();
  70. // }
  71. }
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment