【问题标题】:How to make Output cache for tweetsharp twitter feeds in asp net mvc5如何在 asp net mvc5 中为 tweetsharp twitter 提要制作输出缓存
【发布时间】:2015-12-29 21:04:44
【问题描述】:

我正在使用 tweetsharp nuget 包向我的网站添加推文。

我将部分视图放在页脚上,它在网站的everpage 上可见。

这是我在 Basecontroller 上的代码。

public PartialViewResult _PartialView_twitter_feed()
    {
        var service = new TwitterService("key", "key");

        service.AuthenticateWith("key", "key");

        IEnumerable<TwitterStatus> tweets = service.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions { ScreenName="my_screen_name", Count=5 });

        ViewBag.Tweets = tweets;

        return PartialView();
    }

我在页脚视图中这样称呼它。

 @Html.Action("_PartialView_twitter_feed", "base")

现在的问题是,如何在 asp net mvc 5 上为这个实现进行输出缓存或某种缓存?

我不想在每个页面视图中一次又一次地调用 twitter api。因为部分视图在页脚上。

我怎样才能让它只显示一次,直到用户离开我的网站?

实现这一目标的最佳做法是什么?

感谢您的帮助。

【问题讨论】:

    标签: asp.net asp.net-mvc caching twitter tweetsharp


    【解决方案1】:

    您可以使用 OutputCache 属性。希望下面的例子对你有所帮助。

    控制器

    [OutputCache(Duration = 6000)]
    public PartialViewResult Footer()
    {
        return PartialView("Footer");
    }
    
    public ActionResult MainPage1()
    {
        return View();
    }
    
    public ActionResult MainPage2()
    {
        return View();
    }
    

    查看

    MainPage1

    <h2>MainPage1</h2>
    
    @Html.Action("Footer")
    

    MainPage2

    <h2>MainPage2</h2>
    
    @Html.Action("Footer")
    

    当用户第一次访问MainPage1 时,Footer 部分视图将从服务器返回。从第二次开始,它将从缓存中返回。即使用户访问MainPage2,页脚部分视图也会从缓存中返回。您可以根据需要增加持续时间秒数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多