0

I'm trying to override value of specific key in cache. However, I would like to keep existing setup of cache dependencies.

This is example code to illustrate of what I'm trying to achieve.

    public ActionResult AddToCache()
    {
        HttpRuntime.Cache.Insert("test", "test123", null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration);

        return new ContentResult {Content = "Done."};
    }

    public ActionResult Override()
    {
        HttpRuntime.Cache["test"] = "43212";

        return new ContentResult { Content = "Overriden." };
    }

    public ActionResult Read()
    {
        string value, hit;

        if (HttpRuntime.Cache["test"] != null)
        {
            value = HttpRuntime.Cache["test"].ToString();
            hit = "HIT";
        }
        else
        {
            value = "Unknown";
            hit = "NO HIT";
        }

        return new ContentResult { Content = string.Format("{0} {1}", value, hit) };
    }

If you call AddToCache. You will find that Read gives you test123 for 30 seconds which is normal. However, I would like to override it yet keep original dependency that was put on the cache key.

Is that possible? What would be the best way of approaching this problem.

Luke
  • 1,872
  • 20
  • 31
  • What do you mean by keep the original dependency? And override what? – matt_lethargic Jun 02 '14 at 11:37
  • If you got given HttpRuntime.Cache and a key. How do you work out that in fact criteria of that cached entry was in fact `DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration`? – Luke Jun 02 '14 at 12:00

0 Answers0