phpist

Untitled

Oct 24th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. Объясните работу memcached, то что закомментировано
  2.  
  3. Листинг 23.12. Базовый класс для кэшируемых страниц. Файл pages/Cached.php
  4. <?php ## Базовый класс для кэшируемых страниц
  5. require_once "Page.php";
  6. class Cached extends Page
  7. {
  8. // Время действия кэша
  9. protected $expires;
  10. // Хранилище
  11. protected $store;
  12. // Конструктор класса
  13. public function __construct($title = '', $content = '', $expires = 0)
  14. {
  15. // Вызываем конструктор базового класса Page
  16. parent::__construct($title, $content);
  17. // Устанавливаем время жизни кэша
  18. $this->expires = $expires;
  19. // Подготовка хранилища
  20. // $this->store = new Memcached();
  21. // $this->store->addServer('localhost', 11211);
  22. // Размещение данных в хранилище
  23. $this->set($this->id('title'), $title);
  24. $this->set($this->id('content'), $content);
  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. public function id($name)
  52. {
  53. die("Что здесь делать? Неизвестно!");
  54. }
  55. // Получение заголовка страницы
  56. public final function title()
  57. {
  58. // if ($this->isCached($this->id('title'))) {
  59. // return $this->get($this->id('title'));
  60. // } else {
  61. return parent::title();
  62. // }
  63. }
  64. // Получение содержимое страницы
  65. public final function content()
  66. {
  67. // if ($this->isCached($this->id('content'))) {
  68. // return $this->get($this->id('content'));
  69. // } else {
  70. return parent::content();
  71. // }
  72. }
  73. }
  74. ?>
  75.  
  76. Листинг 23.11. Базовый класс страницы. Файл pages/Page.php
  77.  
  78. <?php ## Базовый класс страницы
  79. class Page
  80. {
  81. // Любая страница имеет заголовок
  82. protected $title;
  83. // И содержимое
  84. protected $content;
  85. // Конструктор класса
  86. public function __construct($title = '', $content = '')
  87. {
  88. $this->title = $title;
  89. $this->content = $content;
  90. }
  91. // Получение заголовка страницы
  92. public function title()
  93. {
  94. return $this->title;
  95. }
  96. // Получение содержимого страницы
  97. public function content()
  98. {
  99. return $this->content;
  100. }
  101. // Формирование HTML-представления страницы
  102. public function render()
  103. {
  104. echo "<h1>".htmlspecialchars($this->title())."</h1>";
  105. echo "</p>".nl2br(htmlspecialchars($this->content()))."</p>";
  106. }
  107. }
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment