Skip to content

Instantly share code, notes, and snippets.

@kana
Created December 3, 2010 04:32
Show Gist options
  • Save kana/726583 to your computer and use it in GitHub Desktop.
Save kana/726583 to your computer and use it in GitHub Desktop.
abstract class CacheTable<TKey, TValue>
{
public void Expire()
{
m_table = null;
}
abstract public IDictionary<TKey, TValue> CreateTable();
public TValue Lookup(TKey key)
{
return table[key];
}
public TValue Lookup(TKey key, TValue defaultValue)
{
TValue value;
if (!(table.TryGetValue(key, out value)))
value = defaultValue;
return value;
}
#region Internal
private IDictionary<TKey, TValue> table
{
get
{
if (m_table == null)
m_table = CreateTable();
return m_table;
}
}
private IDictionary<TKey, TValue> m_table = null;
#endregion
}
class FooBarCacheTable : CacheTable<char, string>
{
override public IDictionary<char, string> CreateTable()
{
return (
new [] {
"foo",
"bar",
"baz",
}
.ToLookup(x => x[0])
.Select(xs => xs.OrderBy(x => x).First())
.ToDictionary(x => x[0])
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment