Exemplo n.º 1
0
 /**
  * 缓存管理API
  *
  * @param Request $request
  * @param Setting $settingModel
  * @param SmpJsonView $view
  *
  * @return \Focus\MVC\View
  */
 public function cacheAction(Request $request, Setting $settingModel, SmpJsonView $view)
 {
     // __tags__, __nav__, post_[id], list_[current], list_[cat]_[current], tag_[tagName]_[current]
     $key = $request->get('key');
     $clear = $request->get('clear', 'false');
     $token = $request->get('token', '');
     $byprefix = $request->get('byprefix', false);
     if (empty($key)) {
         return $view->assign('status', false)->assign('info', 'key_required');
     }
     $tokenEntity = $settingModel->getSetting('cache_token', 'system');
     if (empty($tokenEntity) || !isset($tokenEntity['setting_value']) || $token !== $tokenEntity['setting_value']) {
         return $view->assign('status', false)->assign('info', 'token_invalid');
     }
     // 通过前缀处理
     if ($byprefix === 'true') {
         $res = $this->getCache()->all($key);
         $view->assign('original', $res);
         if ($clear == 'true') {
             foreach ($res as $_key => $_val) {
                 $this->getCache()->delete($_key);
             }
         }
         return $view->assign('status', true)->assign('info', 'clear ' + count($res) + ' items');
     }
     $res = $this->getCache()->get($key);
     if ($res !== false) {
         $this->assign('original', $res);
         if ($clear == 'true') {
             return $view->assign('status', $this->getCache()->delete($key));
         }
     }
     return $view->assign('status', $res);
 }
Exemplo n.º 2
0
 /**
  * 根据标签名称获取相关文章
  *
  * @param Request $request
  * @param Tags $tagModel
  * @param Post $postModel
  * @param SmpJsonView $jsonView
  *
  * @return SmpJsonView
  */
 public function relatedPostsAction(Request $request, Tags $tagModel, Post $postModel, SmpJsonView $jsonView)
 {
     $tags = $tagModel->getTagsByNames(explode(',', $request->get('tags', '')));
     $tag_ids = [];
     foreach ($tags as $tag) {
         $tag_ids[] = $tag['id'];
     }
     $posts = $postModel->getPostsInfoByTags($tag_ids, 10);
     $jsonView->assign('posts', $posts);
     $jsonView->assign('status', true);
     return $jsonView;
 }
Exemplo n.º 3
0
 /**
  * 文章列表页 (标签)
  *
  * @param Request $request
  * @param \Demo\Models\Post $postModel
  * @param \Demo\Models\Tags $tagModel
  *
  * @return string
  */
 public function tagAction(Request $request, \Demo\Models\Post $postModel, \Demo\Models\Tags $tagModel)
 {
     $tagName = $request->get('tag');
     $current = intval($request->get('page', 1));
     $key = "tag_{$tagName}_{$current}";
     $html = $this->getCache()->get($key);
     if (false === $html) {
         $tag = $tagModel->getTagByName($tagName);
         $posts = $postModel->getPostsByTag($tag['id'], $current);
         $this->assign('posts', $posts['data']);
         $this->assign('page', $posts['page']);
         $this->assign('__tag__', $tag);
         $this->assign('__navcur__', '技不压身');
         $this->assign('parsedown', new \Parsedown());
         $this->assign('catModel', new Category());
         $html = $this->view('index')->render();
         $this->getCache()->set($key, $html);
     }
     return $html;
 }