Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Объясните работу memcached, то что закомментировано
- Листинг 23.12. Базовый класс для кэшируемых страниц. Файл pages/Cached.php
- <?php ## Базовый класс для кэшируемых страниц
- require_once "Page.php";
- class Cached extends Page
- {
- // Время действия кэша
- protected $expires;
- // Хранилище
- protected $store;
- // Конструктор класса
- public function __construct($title = '', $content = '', $expires = 0)
- {
- // Вызываем конструктор базового класса Page
- parent::__construct($title, $content);
- // Устанавливаем время жизни кэша
- $this->expires = $expires;
- // Подготовка хранилища
- // $this->store = new Memcached();
- // $this->store->addServer('localhost', 11211);
- // Размещение данных в хранилище
- $this->set($this->id('title'), $title);
- $this->set($this->id('content'), $content);
- }
- // Проверить есть ли позиция $key в кэше
- protected function isCached($key)
- {
- // return (bool) $this->store->get($key);
- }
- // Поместить в кэш по ключу $key значение $value
- // В случае если ключ уже существует:
- // 1. Не делать ничего, если $force принимает значение false
- // 2. Переписать, если $force принимает значение true
- protected function set($key, $value, $force = false)
- {
- // if ($force) {
- // $this->store->set($key, $value, $this->expires);
- // } else {
- // if($this->isCached($key)) {
- // $this->store->set($key, $value, $this->expires);
- // }
- // }
- }
- // Извлечение значения $key из кэша
- protected function get($key)
- {
- // return $this->store->get($key);
- }
- // Формируем уникальный ключ для хранилища
- public function id($name)
- {
- die("Что здесь делать? Неизвестно!");
- }
- // Получение заголовка страницы
- public final function title()
- {
- // if ($this->isCached($this->id('title'))) {
- // return $this->get($this->id('title'));
- // } else {
- return parent::title();
- // }
- }
- // Получение содержимое страницы
- public final function content()
- {
- // if ($this->isCached($this->id('content'))) {
- // return $this->get($this->id('content'));
- // } else {
- return parent::content();
- // }
- }
- }
- ?>
- Листинг 23.11. Базовый класс страницы. Файл pages/Page.php
- <?php ## Базовый класс страницы
- class Page
- {
- // Любая страница имеет заголовок
- protected $title;
- // И содержимое
- protected $content;
- // Конструктор класса
- public function __construct($title = '', $content = '')
- {
- $this->title = $title;
- $this->content = $content;
- }
- // Получение заголовка страницы
- public function title()
- {
- return $this->title;
- }
- // Получение содержимого страницы
- public function content()
- {
- return $this->content;
- }
- // Формирование HTML-представления страницы
- public function render()
- {
- echo "<h1>".htmlspecialchars($this->title())."</h1>";
- echo "</p>".nl2br(htmlspecialchars($this->content()))."</p>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment