using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// Summary description for MSCacheManager
/// </summary>
public class MSCacheManager
{
    public MSCacheManager()
    {

    }

    public static List<object> Items(string prefix)
    {
        prefix = prefix.ToLower();
        List<object> cachelist = new List<object>();
        prefix = prefix.ToLower();
        IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
        while (enumerator.MoveNext())
        {
            String key = (String)((DictionaryEntry)enumerator.Current).Key;
            if (key.StartsWith(prefix))
            {
                cachelist.Add(HttpRuntime.Cache[key]);
            }
        }
        return cachelist;
    }

    public static object Item(string key)
    {
        key = key.ToLower();
        return HttpRuntime.Cache[key];
    }

    /// <summary>
    /// Insert an item into the caching system
    /// </summary>
    /// <param name="key">Item Key for retrieval</param>
    /// <param name="obj">Object to insert</param>
    /// <param name="minutes">number of minutes for sliding expiration, -1 is no expiration</param>
    public static void Insert(string key, object obj, int minutes)
    {
        key = key.ToLower();
        MSCacheManager.PurgeCacheItems(key);
        if (Utility.IsNull(obj))
            return;
        try
        {
            if (minutes >= 0)
                HttpRuntime.Cache.Insert(key, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes, 0), System.Web.Caching.CacheItemPriority.Default, CacheItemRemoved);
            else
                HttpRuntime.Cache.Insert(key, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, CacheItemRemoved);
        }
        catch (Exception e)
        {
            throw new Exception("Cache key: " + key, e);
        }
    }

    public static void CacheItemRemoved(String key, object value, CacheItemRemovedReason removedReason)
    {
        //System.Diagnostics.Debug.Print("removing " + key);
    }

    /// <summary>
    /// add item to cache
    /// </summary>
    /// <param name="key">cache key</param>
    /// <param name="obj">object to insert</param>
    public static void Insert(string key, object obj)
    {
        key = key.ToLower();
        //embedded constants are the root of all evil
        if (!Utility.IsNull(obj))
            Insert(key, obj, 60);
    }

    /// <summary>
    /// Remove items from cache that start with the prefix and do
    /// not start with the nonprefix
    /// </summary>
    /// <param name="prefix">prefix for items to remove</param>
    /// <param name="nonprefix">prefix for items not to remove</param>
    static public void PurgeCacheItems(string prefix, string nonprefix)
    {
        prefix = prefix.ToLower();
        nonprefix = nonprefix.ToLower();
        IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
        while (enumerator.MoveNext())
        {
            String key = (String)((DictionaryEntry)enumerator.Current).Key;
            if (key.StartsWith(prefix) && !key.StartsWith(nonprefix))
            {
                HttpRuntime.Cache.Remove(key);
            }
        }
    }

    /// <summary>
    /// Remove all items from cache that start with the prefix
    /// </summary>
    /// <param name="prefix">prefix for cache items to remove</param>
    static public void PurgeCacheItems(string prefix)
    {
        prefix = prefix.ToLower();
        IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
        while (enumerator.MoveNext())
        {
            String key = (String)((DictionaryEntry)enumerator.Current).Key;
            key = key.ToLower();
            if (key.StartsWith(prefix))
            {
                HttpRuntime.Cache.Remove(key);
            }
        }
    }


}
