Tribe Cache - Basic OverviewCreating The Cache Dictionary (Application Start) - without the use of an IOC container - Mvc 2.0 Global.asax example
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
new CacheFactory().CreateCache();
}
}
Creating The Cache Dictionary & Cache Cleanup Service (Application Start) - without the use of an IOC container - Mvc 2.0 Global.asax example
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
//This will create a cache cleanup service which runs in the background. This service will remove expired items from the cache every (X) - X is set from the TimeSpan
CacheFactory cacheFactory = new CacheFactory();
CacheExpirationServiceFactory cacheExpirationServiceFactory = new CacheExpirationServiceFactory(cacheFactory);
cacheExpirationServiceFactory.CreateCacheExpirationService(TimeSpan.FromSeconds(5));
}
}
Creating The Cache Dictionary (Application Start) - with the use of an IOC container (StructureMap) - Mvc 2.0 Global.asax example
public class CacheRegistry : StructureMap.Configuration.DSL.Registry
{
public CacheRegistry()
{
ObjectFactory.Configure(x => x.ForRequestedType<ICacheFactory>()
.TheDefaultIsConcreteType<CacheFactory>()
.CacheBy(InstanceScope.Singleton));
ObjectFactory.Configure(x => x.ForRequestedType<ICache>().TheDefaultIsConcreteType<Cache>());
ObjectFactory.GetInstance<ICacheFactory>().CreateCache();
}
}
public class Startup
{
public static void Start()
{
ObjectFactory.Configure(x => x.AddRegistry(new CacheRegistry()));
}
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
IOC.Startup.Start();
}
}
Creating The Cache Dictionary & Cache Cleanup Service (Application Start) - with the use of an IOC container (StructureMap) - Mvc 2.0 Global.asax example
public class CacheRegistry : StructureMap.Configuration.DSL.Registry
{
public CacheRegistry()
{
//Cache
ObjectFactory.Configure(x => x.ForRequestedType<ICacheFactory>()
.TheDefaultIsConcreteType<CacheFactory>()
.CacheBy(InstanceScope.Singleton));
ObjectFactory.Configure(x => x.ForRequestedType<ICache>().TheDefaultIsConcreteType<Cache>());
ObjectFactory.GetInstance<ICacheFactory>().CreateCache();
//Cache cleanup service
ObjectFactory.Configure(x => x.ForRequestedType<ICacheExpirationServiceFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.OfConcreteType<CacheExpirationServiceFactory>());
//Sets the time period between invocations of the cache expiration service cleanup task
ObjectFactory.GetInstance<ICacheExpirationServiceFactory>().CreateCacheExpirationService(TimeSpan.FromSeconds(5));
}
}
public class Startup
{
public static void Start()
{
ObjectFactory.Configure(x => x.AddRegistry(new CacheRegistry()));
}
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
IOC.Startup.Start();
}
}
Adding / Retrieving Items From The Cache - Absolute expiration - Specifies that an item expires at a set time, regardless of how often it is accessed
Tribe.Cache.Cache cache = new Tribe.Cache.Cache();
Person person = cache.Get("_person", () => new PersonService().Get(), TimeSpan.FromSeconds(1));
Adding / Retrieving Items From The Cache - Sliding expiration - Specifies how long after an item was last accessed that it expires.
Tribe.Cache.Cache cache = new Tribe.Cache.Cache();
Person person = cache.Get("_person", () => new PersonService().Get(), TimeSpan.FromSeconds(1), CacheExpiration.Sliding);
Adding / Retrieving Items From The Cache - MVC Controller Example using constructor injection
public class HomeController : Controller
{
private readonly ICache _cache;
public HomeController()
{}
public HomeController(ICache cache)
{
_cache = cache;
}
public ActionResult Index()
{
//Absolute expiration - Specifies that an item expires at a set time, regardless of how often it is accessed.
Person person = _cache.Get("_person", () => new PersonService().Get(), TimeSpan.FromSeconds(1));
// Sliding expiration - Specifies how long after an item was last accessed that it expires.
// Person person = _cache.Get("_person", () => new PersonService().Get(), TimeSpan.FromSeconds(1), CacheExpiration.Sliding);
return Content(person.ToString());
}
}
Deleting Items From The Cache
Tribe.Cache.Cache cache = new Tribe.Cache.Cache();
cache.Delete("key");
Simple Working Example
namespace Runner
{
class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public DateTime InstantiatedAt { get; set; }
public override string ToString()
{
return string.Format("{0}-{1}-{2}", PersonId, Name, InstantiatedAt.ToLongTimeString());
}
}
class PersonService
{
public Person Get()
{
return new Person {PersonId = 1, Name = "DOTNETGUYUK", InstantiatedAt = DateTime.Now};
}
}
class Program
{
static void Main(string[] args)
{
Tribe.Cache.Cache cache = new Tribe.Cache.Cache();
Person person = cache.Get("_time", () => new PersonService().Get(), TimeSpan.FromSeconds(3));
Console.WriteLine(person);
System.Threading.Thread.Sleep(2000);
person = cache.Get("_time", () => new PersonService().Get(), TimeSpan.FromSeconds(3));
Console.WriteLine(person);
System.Threading.Thread.Sleep(4000);
person = cache.Get("_time", () => new PersonService().Get(), TimeSpan.FromSeconds(3));
Console.WriteLine(person);
System.Threading.Thread.Sleep(2000);
person = cache.Get("_time", () => new PersonService().Get(), TimeSpan.FromSeconds(10));
Console.WriteLine(person);
cache.Delete("_time");
person = cache.Get("_time", () => new PersonService().Get(), TimeSpan.FromSeconds(10));
Console.WriteLine(person);
Console.ReadLine();
}
}
}