// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program. If not, see .
using System;
using System.Collections.Generic;
using System.Text;
namespace crudwork.Utilities
{
///
/// Provide a memory caching mechanism to save and retrieve results based on the associated key names
///
/// the key
/// the result
public class CacheManager
{
private Dictionary cache;
///
/// Create an empty instance
///
public CacheManager()
{
cache = new Dictionary();
}
///
/// Generate a key based on the input provided.
///
///
///
public string MakeKey(params string[] keys)
{
if (keys == null || keys.Length == 0)
throw new ArgumentNullException("keys");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.Length; i++)
{
if (i > 0)
sb.Append("Æ’"); // delimiter by ASCII 159
sb.Append(keys[i].ToUpper().Trim(' ', '\t'));
}
return sb.ToString();
}
///
/// Check whether or not the result exists in the cache by the associated key
///
///
///
public bool Exists(T key)
{
return cache.ContainsKey(key);
}
///
/// Retrieve the result from cache by the associated key
///
///
///
public U Get(T key)
{
return cache[key];
}
///
/// Add the result/key pair to the cache
///
///
///
public void Add(T key, U value)
{
if (cache.ContainsKey(key))
cache[key] = value;
else
cache.Add(key, value);
}
///
/// Remove the result from cache by the associated key
///
///
public void Delete(T key)
{
cache.Remove(key);
}
///
/// Clear all cache from memory
///
public void Clear()
{
cache.Clear();
}
///
/// Load cache entries from given file
///
///
public void Load(string filename)
{
throw new NotImplementedException("not done");
}
///
/// Save all cache entries to given file
///
///
public void Save(string filename)
{
throw new NotImplementedException("not done");
}
}
}