防止“;高速缓存”;类中方法

本文关键字:方法 高速缓存 防止 | 更新日期: 2023-09-27 18:28:53

我有以下代码:

public static string GetBlogPosts(string input)
        {
            var blogs = context.Blogs.OrderByDescending(a => a.Date).Take(3);
            string insertCode = "";
            foreach (var item in blogs)
            {
                insertCode += String.Format("<div class='blog-post'><h1>{0} <span class='blog-date sub'>({1}/{2})</span></h1>{3}</div>", 
                    item.Headline, item.Date.Day, item.Date.Month, item.Text);
            }
            // insert the code
            input = input.Replace("[widget=blogposts]", insertCode);
            return input;
        }

正如您可能看到的,我使用此方法将[widget=blogposts]替换为显示最新博客文章的实际代码。

唯一的问题是结果被缓存了,如果我编辑其中一篇博客文章,结果不会改变。

有没有办法关闭类中方法的"缓存"?是否以某种方式将上下文声明为静态缓存结果,并使其仅在代码编译时更新?

编辑!以下是类中的所有代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BandPage.Models;
namespace BandPage.Helpers
{
    public static class Widgets
    {
        public static BandPageContext context = new BandPageContext();
        public static string InsertWidgets(string input)
        {
            input = SocialMedia(input);
            input = GetBlogPosts(input);
            input = RecentBlogs(input);
            input = ContactForm(input);
            return input;
        }
        public static string SocialMedia(string input)
        {
            var socialMedia = context.SocialMedias.FirstOrDefault();
            string insertCode = "<div class='social-media'>";
            if (!String.IsNullOrEmpty(socialMedia.Facebook))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/facebook.png' /></a>", socialMedia.Facebook); 
            }
            if (!String.IsNullOrEmpty(socialMedia.Myspace))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/myspace.png' /></a>", socialMedia.Myspace);
            }
            if (!String.IsNullOrEmpty(socialMedia.Soundcloud))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/soundcloud.png' /></a>", socialMedia.Soundcloud);
            }
            if (!String.IsNullOrEmpty(socialMedia.Twitter))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/twitter.png' /></a>", socialMedia.Twitter);
            }
            if (!String.IsNullOrEmpty(socialMedia.Instagram))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/instagram.png' /></a>", socialMedia.Instagram);
            }
            if (!String.IsNullOrEmpty(socialMedia.GooglePlus))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/google_plus.png' /></a>", socialMedia.GooglePlus);
            }
            if (!String.IsNullOrEmpty(socialMedia.Youtube))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/youtube.png' /></a>", socialMedia.Youtube);
            }
            if (!String.IsNullOrEmpty(socialMedia.Spotify))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/spotify.png' /></a>", socialMedia.Spotify);
            }
            if (!String.IsNullOrEmpty(socialMedia.Vimeo))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/vimeo.png' /></a>", socialMedia.Vimeo);
            }
            insertCode += "</div>";
            // insert the code
            input = input.Replace("[widget=socialmedia]", insertCode);
            return input;
        }

        public static string GetBlogPosts(string input)
        {
            var blogs = context.Blogs.OrderByDescending(a => a.Date).Take(3);
            string insertCode = "";
            foreach (var item in blogs)
            {
                insertCode += String.Format("<div class='blog-post'><h1>{0} <span class='blog-date sub'>({1}/{2})</span></h1>{3}</div>", 
                    item.Headline, item.Date.Day, item.Date.Month, item.Text);
            }
            // insert the code
            input = input.Replace("[widget=blogposts]", insertCode);
            return input;
        }
        public static string RecentBlogs(string input)
        {
            var blogs = context.Blogs.OrderByDescending(a => a.Date).Take(10);
            string insertCode = "<div class='blog-recent'>";
            foreach (var item in blogs)
            {
                insertCode += String.Format("<h4><a href='/blog/{0}'>{1} <span class='blog-date'>({2}/{3})</span></a></h4>", 
                    item.BlogId, item.Headline, item.Date.Day, item.Date.Month);
            }
            insertCode += "</div>";
            // insert the code
            input = input.Replace("[widget=recentblogs]", insertCode);
            return input;
        }
        public static string ContactForm(string input)
        {
            string insertCode =     @"<form id='contact-form'>
                                    <input type='text' name='email' id='email' placeholder='Your e-mail address' />
                                    <input type='text' name='subject' id='subject' placeholder='Subject' />
                                    <div class='cheater'>
                                        <input type='text' name='cheater' id='cheater' />
                                    </div>
                                    <textarea name='message' id='message' placeholder='Your message'></textarea>
                                    <label for='submit'></label>
                                    <input type='submit' name='submit' id='submit' value='Send' class='submit-button' />
                                    </form>
                                    <script src='/Scripts/ContactForm.js' type='text/javascript'></script>";
            // insert the code
            input = input.Replace("[widget=contactform]", insertCode);
            return input;
        }
    }
}

防止“;高速缓存”;类中方法

所有方法(SocialMedia、GetBlogPosts、RecentBlogs、ContactForm)都以结束

input = input.Replace("[widget=socialmedia]", insertCode);
return input;

你认为这应该如何运作?

public static string InsertWidgets(string input)
{
    input = SocialMedia(input);
    input = GetBlogPosts(input);
    input = RecentBlogs(input);
    input = ContactForm(input);
    return input;
}

在第一种方法(SocialMedia)之后,输入字符串中的所有[widget=socialmedia]都被替换。

我通过将对象作为参数传递给方法来解决"缓存"问题。不确定这是否被认为是糟糕的编码结构,但至少它解决了无法获得更新值的问题。

如果有人能对数据库中的静态数据和获取数据有所了解,我将不胜感激。它是应该毫无痛苦地工作,还是以某种方式声明上下文为静态会使它不太容易获得最新的值?我在想,这可能与.NET中内置的一些东西有关,以提高性能?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BandPage.Models;
namespace BandPage.Helpers
{
    public static class Widgets
    {
        public static string InsertWidgets(string input)
        {
            BandPageContext context = new BandPageContext();
            input = SocialMedia(input, context.SocialMedias.FirstOrDefault());
            input = GetBlogPosts(input, context.Blogs.OrderByDescending(a => a.Date).Take(3));
            input = RecentBlogs(input, context.Blogs.OrderByDescending(a => a.Date).Take(10));
            input = ContactForm(input);
            return input;
        }
        public static string SocialMedia(string input, SocialMedia socialMedia)
        {
            string insertCode = "<div class='social-media'>";
            if (!String.IsNullOrEmpty(socialMedia.Facebook))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/facebook.png' /></a>", socialMedia.Facebook); 
            }
            if (!String.IsNullOrEmpty(socialMedia.Myspace))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/myspace.png' /></a>", socialMedia.Myspace);
            }
            if (!String.IsNullOrEmpty(socialMedia.Soundcloud))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/soundcloud.png' /></a>", socialMedia.Soundcloud);
            }
            if (!String.IsNullOrEmpty(socialMedia.Twitter))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/twitter.png' /></a>", socialMedia.Twitter);
            }
            if (!String.IsNullOrEmpty(socialMedia.Instagram))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/instagram.png' /></a>", socialMedia.Instagram);
            }
            if (!String.IsNullOrEmpty(socialMedia.GooglePlus))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/google_plus.png' /></a>", socialMedia.GooglePlus);
            }
            if (!String.IsNullOrEmpty(socialMedia.Youtube))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/youtube.png' /></a>", socialMedia.Youtube);
            }
            if (!String.IsNullOrEmpty(socialMedia.Spotify))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/spotify.png' /></a>", socialMedia.Spotify);
            }
            if (!String.IsNullOrEmpty(socialMedia.Vimeo))
            {
                insertCode += String.Format("<a href='{0}'><img src='/Images/icons/social-media/vimeo.png' /></a>", socialMedia.Vimeo);
            }
            insertCode += "</div>";
            // insert the code
            input = input.Replace("[widget=socialmedia]", insertCode);
            return input;
        }

        public static string GetBlogPosts(string input, IEnumerable<Blog> blogs)
        {
            string insertCode = "";
            foreach (var item in blogs)
            {
                insertCode += String.Format("<div class='blog-post'><h1>{0} <span class='blog-date sub'>({1}/{2})</span></h1>{3}</div>", 
                    item.Headline, item.Date.Day, item.Date.Month, item.Text);
            }
            // insert the code
            input = input.Replace("[widget=blogposts]", insertCode);
            return input;
        }
        public static string RecentBlogs(string input, IEnumerable<Blog> blogs)
        {
            string insertCode = "<div class='blog-recent'>";
            foreach (var item in blogs)
            {
                insertCode += String.Format("<h4><a href='/blog/{0}'>{1} <span class='blog-date'>({2}/{3})</span></a></h4>", 
                    item.BlogId, item.Headline, item.Date.Day, item.Date.Month);
            }
            insertCode += "</div>";
            // insert the code
            input = input.Replace("[widget=recentblogs]", insertCode);
            return input;
        }
        public static string ContactForm(string input)
        {
            string insertCode =     @"<form id='contact-form'>
                                    <input type='text' name='email' id='email' placeholder='Your e-mail address' />
                                    <input type='text' name='subject' id='subject' placeholder='Subject' />
                                    <div class='cheater'>
                                        <input type='text' name='cheater' id='cheater' />
                                    </div>
                                    <textarea name='message' id='message' placeholder='Your message'></textarea>
                                    <label for='submit'></label>
                                    <input type='submit' name='submit' id='submit' value='Send' class='submit-button' />
                                    </form>
                                    <script src='/Scripts/ContactForm.js' type='text/javascript'></script>";
            // insert the code
            input = input.Replace("[widget=contactform]", insertCode);
            return input;
        }
    }
}