Override value in cache without affecting dependency

94 views Asked by At

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.

0

There are 0 answers