GlynnTucker.Cache QuickStart
About
The GlynnTucker.Cache assembly provides a data structure for caching slow data retrievals, for example data retrieved from a database server over the network. Think of it as a Hashtable that can automatically expire its data after a set amount of time or a specified period of inactivity, on a per-object basis. It is written in C# and dual licensed under the GPL/MPL, it should work with any .NET language.
Preparation
As with any other assembly, add the assembly as a reference to your project. Add the statement using GlynnTucker.Cache;
to your source files as needed.
Usage: static methods.
The easiest way to use the cache is by calling the static methods provided.
Cache.AddContext("sample"); // Create a cache to store data string exampledotcom = new System.IO.StreamReader( System.Net.WebRequest .Create("http://www.example.com") .GetResponse() .GetResponseStream() ).ReadToEnd(); Cache.Add("sample", "http://www.example.com", exampledotcom); // Add a simple string Cache.Add("sample", 2, "string2", DateTime.Now.AddHours(1)); // Add a string with an expiration time. The entry will be removed in one hour. Cache.Add("sample", 3, "string3", TimeSpan.FromMinutes(5)); // Add a string with a sliding expiration. The entry will be removed once it hasn't been accessed for 5 minutes. Cache.Add("sample", 10, new System.Text.StringBuilder()); // Any type can be added. string s = (string)Cache.Get("sample", 3); // Example of retrieval Cache.ClearContext("sample"); // Delete all entries immediately.
Usage: create an instance.
For a cleaner design on complex projects, you can create an instance of an ICache.
ICache c = new GlynnTucker.Cache.SimpleMemoryCache(); string example = new System.IO.StreamReader( System.Net.WebRequest .Create("http://www.example.com") .GetResponse() .GetResponseStream() ).ReadToEnd(); c.Add(1, "string1"); c.Add(2, example, DateTime.Now.AddHours(1)); string s2 = (string)c.Get(2); c.Clear();