Exemple #1
0
 public async Task <IActionResult> Post(string slug)
 {
     try
     {
         PostDetail post = null;
         if (Guid.TryParse(slug, out Guid id))
         {
             post = _postsRepository.FindById(id);
         }
         else
         {
             post = _postsRepository.GetPostBySlug(slug);
         }
         if (post != null)
         {
             post.Content = _postsRepository.FixContent(post.Content);
             try
             {
                 ///转化Markdown
                 post.Content = await _nodeService.InvokeAsync <string>("./Node/Parser", post.Content);
             }
             catch (Exception ex)
             {
             }
         }
         return(View(post));
     }
     catch
     {
         _logger.LogWarning($"Couldn't find the ${slug} post");
     }
     return(Redirect("/"));
 }
Exemple #2
0
        public IActionResult Post(Guid id)
        {
            var        cacheKey = $"Root_Post_{id}";
            string     cached;
            PostDetail result = null;

            if (_memoryCache.TryGetValue(cacheKey, out cached))
            {
                try
                {
                    result = JsonConvert.DeserializeObject <PostDetail>(cached);
                }
                catch
                {
                    result = null;
                }
            }
            try
            {
                if (result == null)
                {
                    var post = _repo.FindById(id);
                    if (post != null)
                    {
                        post.Content = _repo.FixContent(post.Content);
                        result       = post;
                        cached       = JsonConvert.SerializeObject(result);
                        _memoryCache.Set(cacheKey, cached, new MemoryCacheEntryOptions()
                        {
                            SlidingExpiration = TimeSpan.FromHours(12)
                        });
                    }
                }
                return(View(result));
            }
            catch
            {
                _logger.LogWarning($"Couldn't find the ${id} post");
            }
            return(Redirect("/"));
        }