开发者

How to perform caching in MVC

I want to perform caching of data for 1 day. In my MVC model I am getting the data from the database and the using it on my View. I want to add the data in cache if not there.If it is a开发者_高级运维lready in cache then getting the result directly form there. In my model I have a function result() in that I have used caching as

if (HttpContext.Current.Cache[ID] == null)
{   
  query = db.Employee.FirstOrDefault(x=>x.id.Equals(ID));  
          HttpContext.Current.Cache.Insert
            (ID, query, null,DateTime.Now.AddDays(1),  
               System.Web.Caching.Cache.NoSlidingExpiration);
} else query = (Employee)HttpContext.Current.Cache[ID];

But here caching works only for current request and after that again data is retrived from database and a new insertion is performed in cache for the same data. I want the data in cache for 1 day. Please provide me the way to cache my data.

Thanks.


Do you want to cache the entire output from your actions or is it just your database queries?

If so, use the OutputCache attribute on your actions like so:

[OutputCache(Duration = 86400, VaryByParam = "None")]
public ActionResult Index()
{
    var data = GetFromDatabase();
    return View(data);
}

86400 means that we want to cache it for 24 hours.

Note that this will cache the entire view, so all your users will see the same. If you have any user-specific content, leave a comment and i'll try to give you a new solution.


If it's possible - cache ViewResults. Easier and better.

For raw caching, I'm using this and it works as expected (accross multiple requests) =>

public static class CacheManager
    {
        public static bool Exists
            (string cacheKey, HttpContextBase context)
        {
            return context.Cache[cacheKey] != null;
        }

        public static object Get
            (string cacheKey, HttpContextBase context)
        {
            return context.Cache[cacheKey];
        }

        public static T Get<T>
            (string cacheKey, HttpContextBase context)
            where T : class
        {
            return context.Cache.Get(cacheKey) as T;
        }

        public static T Get<T>
            (string cacheKey, HttpContextBase context, Func<T> getItemCallback)
            where T : class
        {
            T item = Get<T>(cacheKey, context);
            if (item == null) {
                item = getItemCallback();
                //by default - caching for 1 day
                if (item!=null)
                    context.Cache.Insert(cacheKey, item, null, 
                        DateTime.Now.AddDays(1),TimeSpan.Zero);
            }

            return item;
        }

        public static void Save<T>
            (string cacheKey, HttpContextBase context, T value)
            where T : class
        {
            context.Cache.Insert(cacheKey, value);
        }
    }

Usage =>

 public IList<Database> AllDatabases
        {
            get
            {
                return CacheManager.Get
                   (CacheKeys.AllDatabases, ControllerContext.HttpContext,
                    () => databaseRepository.GetAll());
            }
        }

Only - I believe passing context base is unnecessary complexity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜