From c0889e2420967bca27b1aa8e01ac31c7dc4ffcef Mon Sep 17 00:00:00 2001 From: Igor Simdyanov Date: Sun, 10 Jul 2022 16:02:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BB=D0=B0=D0=B2=D0=B0=2045:=20redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis/config.php | 4 ++ redis/keys.php | 6 ++ redis/methods.php | 21 +++++++ redis/mget.php | 8 +++ redis/mvc/controllers/controller.php | 35 ++++++++++++ redis/mvc/controllers/controllercache.php | 67 +++++++++++++++++++++++ redis/mvc/controllers/controllerlazy.php | 60 ++++++++++++++++++++ redis/mvc/controllers/router.php | 26 +++++++++ redis/mvc/decorators/decoratorfactory.php | 15 +++++ redis/mvc/decorators/userdecorator.php | 32 +++++++++++ redis/mvc/decorators/usersdecorator.php | 45 +++++++++++++++ redis/mvc/models/user.php | 11 ++++ redis/mvc/models/users.php | 24 ++++++++ redis/mvc/views/htmlview.php | 34 ++++++++++++ redis/mvc/views/rssview.php | 34 ++++++++++++ redis/mvc/views/userhtmlview.php | 4 ++ redis/mvc/views/userrssview.php | 4 ++ redis/mvc/views/usershtmlview.php | 4 ++ redis/mvc/views/usersrssview.php | 4 ++ redis/mvc/views/viewfactory.php | 18 ++++++ redis/mvc_use.php | 9 +++ redis/ping.php | 4 ++ redis/session.php | 9 +++ 23 files changed, 478 insertions(+) create mode 100644 redis/config.php create mode 100644 redis/keys.php create mode 100644 redis/methods.php create mode 100644 redis/mget.php create mode 100644 redis/mvc/controllers/controller.php create mode 100644 redis/mvc/controllers/controllercache.php create mode 100644 redis/mvc/controllers/controllerlazy.php create mode 100644 redis/mvc/controllers/router.php create mode 100644 redis/mvc/decorators/decoratorfactory.php create mode 100644 redis/mvc/decorators/userdecorator.php create mode 100644 redis/mvc/decorators/usersdecorator.php create mode 100644 redis/mvc/models/user.php create mode 100644 redis/mvc/models/users.php create mode 100644 redis/mvc/views/htmlview.php create mode 100644 redis/mvc/views/rssview.php create mode 100644 redis/mvc/views/userhtmlview.php create mode 100644 redis/mvc/views/userrssview.php create mode 100644 redis/mvc/views/usershtmlview.php create mode 100644 redis/mvc/views/usersrssview.php create mode 100644 redis/mvc/views/viewfactory.php create mode 100644 redis/mvc_use.php create mode 100644 redis/ping.php create mode 100644 redis/session.php diff --git a/redis/config.php b/redis/config.php new file mode 100644 index 0000000..2681c12 --- /dev/null +++ b/redis/config.php @@ -0,0 +1,4 @@ +connect('127.0.0.1', 6379); +$redis->select(1); diff --git a/redis/keys.php b/redis/keys.php new file mode 100644 index 0000000..591b79a --- /dev/null +++ b/redis/keys.php @@ -0,0 +1,6 @@ +'; +print_r($redis->keys('*')); +echo ''; diff --git a/redis/methods.php b/redis/methods.php new file mode 100644 index 0000000..1953c40 --- /dev/null +++ b/redis/methods.php @@ -0,0 +1,21 @@ +set('key', 'value'); +echo $redis->get('key'); // value +echo '
'; + +// Установка ключа на 10 мс +$redis->set('hello', 'world', 10); +echo $redis->get('hello'); // world +echo '
'; + +// Установка сразу нескольких ключей +$redis->mSet(['fst' => 'первый', 'snd' => 'второй']); +echo $redis->get('snd'); // второй +echo '
'; + +if ($redis->exists('fst')) { + echo $redis->get('fst'); // первый +} diff --git a/redis/mget.php b/redis/mget.php new file mode 100644 index 0000000..6aa23c9 --- /dev/null +++ b/redis/mget.php @@ -0,0 +1,8 @@ +keys('*'); + +echo '
';
+print_r($redis->mGet($keys));
+echo '
'; diff --git a/redis/mvc/controllers/controller.php b/redis/mvc/controllers/controller.php new file mode 100644 index 0000000..e6512d6 --- /dev/null +++ b/redis/mvc/controllers/controller.php @@ -0,0 +1,35 @@ +path = $path; + $this->router = Router::parse($path); + $class = 'MVC\\Models\\' . ucfirst($this->router->model); + $this->model = new $class(); + if($this->router->id) { + $this->model = $this->model->collection[$this->router->id]; + } + } + + public function render() : string + { + $class = get_class($this->model); + $class = substr($class, strrpos($class, '\\') + 1); + $decorator = \MVC\Decorators\DecoratorFactory::create( + $class, + $this->model); + $view = \MVC\Views\ViewFactory::create( + $this->router->ext, + $class, + $decorator); + + return $view->render(); + } +} diff --git a/redis/mvc/controllers/controllercache.php b/redis/mvc/controllers/controllercache.php new file mode 100644 index 0000000..8218141 --- /dev/null +++ b/redis/mvc/controllers/controllercache.php @@ -0,0 +1,67 @@ +path = $path; + $this->redis = $redis; + } + + public function render() : string + { + $cache = $this->redis->get($this->path); + if (!$cache) { + $decorator = \MVC\Decorators\DecoratorFactory::create( + $this->getClass(), + $this->getModel()); + $view = \MVC\Views\ViewFactory::create( + $this->getRouter()->ext, + $this->getClass(), + $decorator); + $cache = $view->render(); + $this->redis->set($this->path, $cache); + } + + return $cache; + } + + private function getClass() : string + { + if (empty($this->class)) { + $class = get_class($this->getModel()); + $this->class = substr($class, strrpos($class, '\\') + 1); + } + + return $this->class; + } + + private function getRouter() : Router + { + if (empty($this->router)) { + $this->router = Router::parse($this->path); + } + + return $this->router; + } + + private function getModel() : object + { + if (empty($this->model)) { + $class = 'MVC\\Models\\' . ucfirst($this->getRouter()->model); + $this->model = new $class(); + if ($this->getRouter()->id) { + $this->model = $this->model->collection[$this->getRouter()->id]; + } + } + + return $this->model; + } +} diff --git a/redis/mvc/controllers/controllerlazy.php b/redis/mvc/controllers/controllerlazy.php new file mode 100644 index 0000000..1153a4c --- /dev/null +++ b/redis/mvc/controllers/controllerlazy.php @@ -0,0 +1,60 @@ +path = $path; + } + + public function render() : string + { + $decorator = \MVC\Decorators\DecoratorFactory::create( + $this->getClass(), + $this->getModel()); + $view = \MVC\Views\ViewFactory::create( + $this->getRouter()->ext, + $this->getClass(), + $decorator); + + return $view->render(); + } + + private function getClass() : string + { + if (empty($this->class)) { + $class = get_class($this->getModel()); + $this->class = substr($class, strrpos($class, '\\') + 1); + } + + return $this->class; + } + + private function getRouter() : Router + { + if (empty($this->router)) { + $this->router = Router::parse($this->path); + } + + return $this->router; + } + + private function getModel() : object + { + if (empty($this->model)) { + $class = 'MVC\\Models\\' . ucfirst($this->getRouter()->model); + $this->model = new $class(); + if ($this->getRouter()->id) { + $this->model = $this->model->collection[$this->getRouter()->id]; + } + } + + return $this->model; + } +} diff --git a/redis/mvc/controllers/router.php b/redis/mvc/controllers/router.php new file mode 100644 index 0000000..3a40dfc --- /dev/null +++ b/redis/mvc/controllers/router.php @@ -0,0 +1,26 @@ + 1 ? $arr[1] : null); + } + + private function __construct( + string $model, + ?string $ext = null, + ?int $id = null) + { + $this->model = $model; + $this->ext = $ext; + $this->id = $id; + } +} diff --git a/redis/mvc/decorators/decoratorfactory.php b/redis/mvc/decorators/decoratorfactory.php new file mode 100644 index 0000000..87c3eeb --- /dev/null +++ b/redis/mvc/decorators/decoratorfactory.php @@ -0,0 +1,15 @@ +user = $user; + } + + public function title() : string + { + return implode(' ', [$this->user->first_name, $this->user->last_name]); + } + + public function body() : string + { + return '' . htmlspecialchars($this->title()) . ' '. + '(' . htmlspecialchars($this->user->email) . ')'; + + } + + public function items() : string + { + return ''. + '' . htmlspecialchars($this->title()) . '' . + '' . htmlspecialchars($this->user->email) . ''. + ''; + } +} diff --git a/redis/mvc/decorators/usersdecorator.php b/redis/mvc/decorators/usersdecorator.php new file mode 100644 index 0000000..a1f8975 --- /dev/null +++ b/redis/mvc/decorators/usersdecorator.php @@ -0,0 +1,45 @@ +users = $users; + } + + public function title() : string + { + return 'Пользователи'; + } + + public function collection_render( + callable $call, + string $separator = '
') : string + { + return implode( + $separator, + array_map($call, $this->users->collection) + ); + } + + public function body() : string + { + return $this->collection_render( + function($item){ + $decorated_item = new UserDecorator($item); + return $decorated_item->body(); + }); + } + + public function items() : string + { + return $this->collection_render( + function($item){ + $decorated_item = new UserDecorator($item); + return $decorated_item->items(); + }, ''); + } +} diff --git a/redis/mvc/models/user.php b/redis/mvc/models/user.php new file mode 100644 index 0000000..3790d30 --- /dev/null +++ b/redis/mvc/models/user.php @@ -0,0 +1,11 @@ +collection = $users; + } +} diff --git a/redis/mvc/views/htmlview.php b/redis/mvc/views/htmlview.php new file mode 100644 index 0000000..23ddb08 --- /dev/null +++ b/redis/mvc/views/htmlview.php @@ -0,0 +1,34 @@ + + + + {{{title}}} + + + {{{body}}} + +HTML; + + protected $replacements; + + public function __construct(object $decorator) + { + $this->replacements = [ + '{{{title}}}' => $decorator->title(), + '{{{body}}}' => $decorator->body() + ]; + } + + public function render() : string + { + return str_replace( + array_keys($this->replacements), + array_values($this->replacements), + self::LAYOUT); + } +} diff --git a/redis/mvc/views/rssview.php b/redis/mvc/views/rssview.php new file mode 100644 index 0000000..7c876f2 --- /dev/null +++ b/redis/mvc/views/rssview.php @@ -0,0 +1,34 @@ + + + + {{{title}}} + http://example.com/ + {{{items}}} + + +HTML; + + protected $replacements; + + public function __construct(object $decorator) + { + $this->replacements = [ + '{{{title}}}' => $decorator->title(), + '{{{items}}}' => $decorator->items() + ]; + } + + public function render() : string + { + return str_replace( + array_keys($this->replacements), + array_values($this->replacements), + self::LAYOUT); + } +} diff --git a/redis/mvc/views/userhtmlview.php b/redis/mvc/views/userhtmlview.php new file mode 100644 index 0000000..92d7735 --- /dev/null +++ b/redis/mvc/views/userhtmlview.php @@ -0,0 +1,4 @@ +render(); diff --git a/redis/ping.php b/redis/ping.php new file mode 100644 index 0000000..52ca4c2 --- /dev/null +++ b/redis/ping.php @@ -0,0 +1,4 @@ +connect('127.0.0.1', 6379); +echo $redis->ping(); // true diff --git a/redis/session.php b/redis/session.php new file mode 100644 index 0000000..f5238a3 --- /dev/null +++ b/redis/session.php @@ -0,0 +1,9 @@ +