Setup/Configuration
DotCache is a fairly easy system to set up and use. The system setup requires two steps. The first is the creation of config classes. There are two config classes that must be set up. The first is for logging and the second is for the cache system itself:

    public class Logging: BlammoNet.Config.Config
    {
        public Logging()
            : base()
        {
            LogFileLocation = HttpContext.Current.Server.MapPath("~/App_Data/Logs/");
        }
    }

    public class Cache : DotCache.Config.Config
    {
        public override bool EnableLogging { get { return true; } }
    }


I wont talk about the first class here as configuration of the logging system can be found here. The Cache class only sets EnableLogging to true. By default logging when things are added, changed, or removed from the cache is turned off. In this case we're turning it on.

The second step for setup is to actually initialize the various managers. For example, if I were to use this within an ASP.Net app, I would have a Global.asax file that looked like this:

    public class Global : System.Web.HttpApplication
    {
        protected static BlammoNet.Manager LogManager = null;
        protected void Application_Start(object sender, EventArgs e)
        {
            Gestalt.Manager.Instance.RegisterConfigFile(typeof(Default).Assembly);
            LogManager = BlammoNet.Manager.Instance;
            DotCache.Manager.Instance.Setup();
        }

        protected void Application_End(object sender, EventArgs e)
        {
            if (LogManager != null)
            {
                LogManager.Dispose();
                LogManager = null;
            }
            DotCache.Manager.Instance.Dispose();
        }
    }


The order that they are initialized is important, first Gestalt.Net needs to register all config objects. Secondly the log manager must be created. After that is done, DotCache itself is initialized. When everything is over, make sure to dispose of the log manager and DotCache.

Usage
Using DotCache is equally simple. A call to Manager.Instance.GetCache() gets the default cache. One of the nice things about DotCache is the ability to create multiple caches, thus allowing for separation of data without having to worry about accidentally overwriting something. In order to do this we simply feed the GetCache function a name. For instance, if we do the following:

Manager.Instance.GetCache("Cache1");
Manager.Instance.GetCache("Cache2");


DotCache will return two separate caches, Cache1 and Cache2. If it does not find them, it will automatically create them when they are called for. Once we have the cache that we want, there are a couple functions that we can call to get our data:
  • Get
  • Clear
  • Remove
  • Exists
  • Add

Get simply retrieves the object of a specified name:

MyObjType Object=Manager.Instance.GetCache().Get<MyObjType>("ObjectName");


Note that it's generic so it automatically returns the object as the given type. The next function is Clear, which simply removes everything from the cache.

Manager.Instance.GetCache().Clear();


The next is remove, which removes a specific item from the cache. For instance if we wanted to remove the ObjectName object from above we would do the following:

Manager.Instance.GetCache().Remove("ObjectName");


Next is Exists, which simply lets you know if something is currently within the cache:

    if(Manager.Instance.GetCache().Exists("ObjectName")
    {
    ...
    }


Lastly we have Add. Add is actually two functions. The first takes a string for a key and an object as the value to save within the cache:

Manager.Instance.GetCache().Add("ObjectName",ObjectToSave);


The second function takes three parameters, a string key, the object to save, and a TimeSpan. The TimeSpan determines when something in the cache is no longer valid. After that amount of time, it is automatically removed from the cache. Note that if you do not set a TimeSpan, it remains in the cache until you remove it:

Manager.Instance.GetCache().Add("ObjectName",ObjectToSave,new TimeSpan(2,0,0));


The above code will save ObjectToSave for two hours and then remove it from the cache. And with that you have everything you need to use DotCache.

Last edited Jun 22, 2010 at 6:18 PM by JaCraig, version 1

Comments

No comments yet.