0

I have started working on C#.net MVC3 project from last few months. I didn’t go through any tutorial to understand how .net works, but I was learning based on comparison between Java and .Net. Now I couldn’t understand some basic thing.

Here is my question I want to maintain application level cache (not session level or request level), so I created in memory dictionary object and want to play with it. Unfortunately cache objects are reinitializing for every request I guess even though I declared and initialized in global.asax.cs file.

Here is my code please help me.

CachedComponents.cs
==============

public class CachedComponents
   {
    private static readonly ILog log = LogManager.GetLogger("CachedComponents");
    private Dictionary<string, TridionComponent> componentCache = new Dictionary<string, TridionComponent>();

    public CachedTridionComponentsRepository()
    {
    }
 public bool IsExistInCache(string uri)
    {
        return componentCache.ContainsKey(uri);
    }

    public TridionComponent getCachedItem(string key)
    {
        if (componentCache.ContainsKey(key))
        {
            log.Info("[getCachedItem] Cached Item found " + key);
            return componentCache[key];
        }
        else
        {
            return null;
        }

    }

    public void Add(string key, TridionComponent value)
    {
        if (componentCache.ContainsKey(key))
        {
            log.Debug("[Add] Item is already Cached...Cache has been Updated...");
            componentCache[key] = value;
        }
        else
        {
            log.Debug("[Add] Item has been added to cache");
            componentCache.Add(key, value);
        }
    }

     }

Global.asax.cs
========= 
protected void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        log4net.Config.XmlConfigurator.Configure();


        CachedComponents componentsCache = new CachedComponents();
        Application.Add("seeta", "seeta");
        Application.Add("componentsCache", componentsCache);
    }

    MyBusiness.cs
    ============= 

    public class MyBusiness
{
    private CachedComponets cache = null;
    public MyBusiness() 
    {
        if(cache == null)
            cache = HttpContext.Cuurent.Application.get("componentsCache");
    }
    public TridionComponent myBusinessMethod() 
    {
        if (cache.IsExistInCach("uri"))
            return cache.getCachedItem("uri");
        TridionComponent tc = GetTridionComponent();
        cache.Add("uri",tc);
        return tc;
    }

}
0

1 Answer 1

1

Set to static your dictionary as you did with ILog. That should works

Sign up to request clarification or add additional context in comments.

3 Comments

static is meant for class variable stuff... how static is related to this... any how Ill try but Ill not have any hope...
look here stackoverflow.com/a/313352/394028. Maybe it can help (ignore the readonly part)
Thanks for your answer. It is not about static thing but your answer triggered me to push the declaration to class ( not with in function). Initially object declaration and creation with in the function. But now I created a object as member variable, now it is working. That means if CachedComponents declaration should be in Global.asax.cs file, not with in Application_Start() function. It might be the issue with scope of object. I really understand but it is working fine now. Regards, Seeta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.