在类方法中防止“缓存”

时间:2013-08-25 15:28:52

标签: c# asp.net-mvc caching

我有以下代码:

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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您的所有方法( 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]

答案 1 :(得分:0)

我解决了“缓存”问题,将对象作为参数传递给方法。不确定这是否被认为是错误的编码结构,但至少它解决了没有获得更新值的问题。

如果有人能够了解静态数据并从数据库中获取数据,我将非常感激。它是应该无痛地工作,还是声明上下文静态以某种方式使它不太容易获得最新值?我认为它可能与.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;
        }
    }
}