/** * Check and setup Redis server * * @param array $settings * @throws ConfigurationException */ public function __construct($settings = array()) { $this->handler = Proxy\Cache::getInstance(); if ($this->handler instanceof Nil) { throw new ConfigurationException("Cache configuration is missed or disabled. Please check 'cache' configuration section"); } }
/** * Retrieve reflection for anonymous function * @param string $file * @throws ApplicationException * @return Reflection */ public function reflection($file) { // cache for reflection data if (!($reflection = Cache::get('reflection:' . $file))) { $reflection = new Reflection($file); $reflection->process(); Cache::set('reflection:' . $file, $reflection); Cache::addTag('reflection:' . $file, 'reflection'); } return $reflection; }
/** * Get user privileges * * @param integer $roleId * @return array */ public function getRolePrivileges($roleId) { $cacheKey = 'privileges:role:' . $roleId; if (!($data = Cache::get($cacheKey))) { $data = Db::fetchColumn("SELECT DISTINCT CONCAT(p.module, ':', p.privilege)\n FROM acl_privileges AS p, acl_roles AS r\n WHERE p.roleId = r.id AND r.id = ?\n ORDER BY CONCAT(p.module, ':', p.privilege)", array((int) $roleId)); Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY); Cache::addTag($cacheKey, 'privileges'); } return $data; }
namespace Application; use Bluz\Proxy\Cache; use Bluz\Proxy\Layout; return function ($id = null) use($bootstrap, $view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Cache Data']); /* @var Bootstrap $this */ Layout::title('Check cache'); // try to load profile of current user if (!$id && $this->user()) { $id = $this->user()->id; } if (!$id) { throw new \Exception('User not found', 404); } /** * @var Users\Row $userRow */ if (!($userRow = Cache::get('user:'******'user:'******'User not found', 404); } $view->user = $userRow; };
<?php /** * Build list of routers * * @author Anton Shevchuk * @created 12.06.12 12:27 */ /** * @namespace */ namespace Application; use Bluz\Common\Nil; use Bluz\Proxy\Cache; use Bluz\Proxy\Messages; return function () { /** * @var Bootstrap $this */ if (!Cache::getInstance() instanceof Nil) { Cache::flush(); Messages::addSuccess("Cache is flushed"); } else { Messages::addNotice("Cache is disabled"); } };
/** * Return information about tables columns * * @return array */ public function getColumns() { if (empty($this->columns)) { $columns = Cache::get('table:columns:' . $this->table); if (!$columns) { $connect = DbProxy::getOption('connect'); $columns = DbProxy::fetchColumn(' SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$connect['name'], $this->getName()]); Cache::set('table:columns:' . $this->table, $columns); Cache::addTag('table:columns:' . $this->table, 'db'); } $this->columns = $columns; } return $this->columns; }
* @namespace */ namespace Application; use Bluz\Common\Nil; use Bluz\Proxy\Cache; use Bluz\Proxy\Messages; return function () { /** * @var Bootstrap $this */ if (!Cache::getInstance() instanceof Nil) { // routers Cache::delete('router:routers'); Cache::delete('router:reverse'); // roles Cache::deleteByTag('roles'); Cache::deleteByTag('privileges'); // reflection data Cache::deleteByTag('reflection'); // db metadata Cache::deleteByTag('db'); // view data Cache::deleteByTag('view'); // html data Cache::deleteByTag('html'); Messages::addSuccess("Cache is cleaned"); } else { Messages::addNotice("Cache is disabled"); } };
/** * Constructor of Router */ public function __construct() { $routers = Cache::get('router:routers'); $reverse = Cache::get('router:reverse'); if (!$routers or !$reverse) { $routers = array(); $reverse = array(); $path = Application::getInstance()->getPath() . '/modules/*/controllers/*.php'; foreach (new \GlobIterator($path) as $file) { /* @var \SplFileInfo $file */ $module = $file->getPathInfo()->getPathInfo()->getBasename(); $controller = $file->getBasename('.php'); $reflection = Application::getInstance()->reflection($file->getRealPath()); if ($routes = $reflection->getRoute()) { foreach ($routes as $route => $pattern) { if (!isset($reverse[$module])) { $reverse[$module] = array(); } $reverse[$module][$controller] = ['route' => $route, 'params' => $reflection->getParams()]; $rule = [$route => ['pattern' => $pattern, 'module' => $module, 'controller' => $controller, 'params' => $reflection->getParams()]]; // static routers should be first if (strpos($route, '$')) { $routers = array_merge($routers, $rule); } else { $routers = array_merge($rule, $routers); } } } } Cache::set('router:routers', $routers); Cache::set('router:reverse', $reverse); } $this->routers = $routers; $this->reverse = $reverse; }
use Bluz\Proxy\Cache; use Bluz\Proxy\Db; use Bluz\Proxy\Messages; return function ($acl) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ $callback = function () use($acl) { /** * @var Bootstrap $this */ Db::query('DELETE FROM acl_privileges'); foreach ($acl as $roleId => $modules) { foreach ($modules as $module => $privileges) { foreach ($privileges as $privilege => $flag) { Db::query('INSERT INTO acl_privileges SET roleId = ?, module = ?, privilege = ?', array($roleId, $module, $privilege)); } } } }; if (empty($acl)) { Messages::addError('Privileges set is empty. You can\'t remove all of them'); } elseif (Db::transaction($callback)) { Cache::deleteByTag('privileges'); Messages::addSuccess('All data was saved'); } else { Messages::addError('Internal Server Error'); } $this->redirectTo('acl', 'index'); };
* @accept HTML * @accept JSON * @privilege Management * * @param int $id * @return bool * @throws Exception */ return function ($id) { /** * @var Controller $this */ $user = Users\Table::findRow($id); if (!$user) { throw new Exception('User ID is incorrect'); } if (Request::isPost()) { $roles = Request::getParam('roles'); // update roles Db::delete('acl_users_roles')->where('userId = ?', $user->id)->execute(); foreach ($roles as $role) { Db::insert('acl_users_roles')->set('userId', $user->id)->set('roleId', $role)->execute(); } // clean cache Cache::delete('user:'******'User roles was updated'); return false; } $this->assign('user', $user); $this->assign('roles', Roles\Table::getInstance()->getRoles()); };
<?php /** * Build list of routers * * @author Anton Shevchuk * @created 12.06.12 12:27 */ /** * @namespace */ namespace Application; use Bluz\Common\Nil; use Bluz\Proxy\Cache; use Bluz\Proxy\Layout; use Bluz\Proxy\Messages; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), __('Cache')]); if (!Cache::getInstance() instanceof Nil) { $view->adapter = get_class(Cache::getInstance()->getAdapter()); } else { $view->adapter = null; Messages::addNotice("Cache is disabled"); } };
/** * Retrieve reflection for anonymous function * @return Reflection * @throws \Bluz\Common\Exception\ComponentException */ protected function setReflection() { // cache for reflection data if (!($reflection = Cache::get('reflection:' . $this->module . ':' . $this->controller))) { $reflection = new Reflection($this->getFile()); $reflection->process(); Cache::set('reflection:' . $this->module . ':' . $this->controller, $reflection); Cache::addTag('reflection:' . $this->module . ':' . $this->controller, 'reflection'); } $this->reflection = $reflection; }
<?php /** * Build list of routers * * @author Anton Shevchuk * @created 12.06.12 12:27 */ /** * @namespace */ namespace Application; use Bluz\Common\Nil; use Bluz\Proxy\Cache; use Bluz\Proxy\Layout; use Bluz\Proxy\Messages; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), $view->ahref('Cache', ['cache', 'index']), __('Statistics')]); if (!Cache::getInstance() instanceof Nil) { $view->adapter = Cache::getInstance()->getAdapter(); } else { Messages::addNotice("Cache is disabled"); $this->redirectTo('cache', 'index'); } };
/** * Get all user roles in system * * @param integer $userId * @return array of identity */ public function getUserRolesIdentity($userId) { $cacheKey = 'roles:user:'******'roles'); Cache::addTag($cacheKey, 'user:' . $userId); } return $data; }
*/ /** * @namespace */ namespace Application; use Bluz\Common\Nil; use Bluz\Controller\Controller; use Bluz\Proxy\Cache; use Bluz\Proxy\Layout; use Bluz\Proxy\Messages; use Bluz\Proxy\Response; /** * Statistics * * @privilege Management * @return void */ return function () { /** * @var Controller $this */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([Layout::ahref('Dashboard', ['dashboard', 'index']), Layout::ahref('Cache', ['cache', 'index']), __('Statistics')]); if (!Cache::getInstance() instanceof Nil) { $this->assign('adapter', Cache::getInstance()->getAdapter()); } else { Messages::addNotice("Cache is disabled"); Response::redirectTo('cache', 'index'); } };