/** * Update menu item * * @param MenuItem $item menu item instance * @return MenuItem */ public function updateItem(MenuItem $item) { $item = $this->repo->updateItem($item); $key = $this->getItemCacheKey($item->getKey()); $this->cache->put($key, $item); return $item; }
/** * Returns descendant of item * * @param Permission $item permission instance * @return array */ public function fetchDescendant(Permission $item) { $key = $this->getCacheKey($item->siteKey, $item->name, 'descendant'); if (!($descendants = $this->cache->get($key))) { $descendants = $this->repo->fetchDescendant($item); if (!empty($descendants)) { $this->cache->put($key, $descendants); } } return $descendants; }
/** * Retrieve routes by module name * * @param string $module module name * @return InstanceRoute[] */ public function fetchByModule($module) { $key = $this->getCacheKey($module); $routes = $this->cache->has($key) ? $this->cache->get($key) : call_user_func(function () use($module, $key) { $routes = $this->repo->fetchByModule($module); if (count($routes) > 0) { $this->cache->put($key, $routes); } return $routes; }); return $routes; }
/** * Remove cache data * * @param string $key config name * @return void */ protected function erase($key) { $segments = explode('.', $key); $head = reset($segments); if ($key == $head) { $this->cache->forget($this->getCacheKey($head)); } else { $cache = $this->make($this->getAll($head), $segments, null); $this->cache->put($this->getCacheKey($head), $cache); } foreach ($this->bag as $k => $item) { if (substr($k, 0, strlen($key)) == $key) { unset($this->bag[$k]); } } }
/** * Get all menu * * @param string $siteKey site key * @param array $with relation * @return Menu[] */ public function all($siteKey, $with = []) { $key = $this->getCacheKey($siteKey . '_all'); $subKey = $this->getWithKey($with); $data = $this->cache->get($key); if (!$data || !isset($data[$subKey])) { $menus = $this->repo->all($siteKey, $with); if (!empty($menus)) { $data = $data ?: []; $data[$subKey] = $menus; $this->cache->put($key, $data); } } else { $menus = $data[$subKey]; } return $menus; }
/** * get cached data * * @param string $siteKey site key * @param string $head root name * @return array */ protected function getData($siteKey, $head) { $key = $this->makeKey($siteKey, $head); if (!isset($this->bag[$key])) { $cacheKey = $this->getCacheKey($key); if (!($data = $this->cache->get($cacheKey))) { if (!($config = $this->repo->find($siteKey, $head))) { return []; } $descendant = $this->repo->fetchDescendant($siteKey, $head); $data = array_merge([$config], $descendant); $this->cache->put($cacheKey, $data); } $this->bag[$key] = $data; } return $this->bag[$key]; }