TheDizzle
TheDizzle

Reputation: 1574

Caching a string for 24 hours

I am trying to cache a string in my .net mvc application for 24 hours. I am having trouble finding a way to get the cache to expire. Currently I set the cache using

System.Runtime.Caching.MemoryCache.Default["activeKey"] = "success";

I use the cache by calling

var activeKey = System.Runtime.Caching.MemoryCache.Default["activeKey"];
if (activeKey != null && (string) activeKey == "success")
{
    return true;
}

However, I am unsure where to go from here as far as cache. I found docs for sliding and absolute cache, but am unsure exactly what to do.

Upvotes: 0

Views: 1844

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156918

The Add or AddOrGetExisting methods are the best you can get. They both have an argument to set the expiration (absoluteExpiration).

System.Runtime.Caching.MemoryCache.Default.Add(
    "activeKey",
    "success",
    new DateTimeOffset(DateTime.Now.AddDays(1))
)

Upvotes: 2

Related Questions