using System.Web;
using System.Collections;

namespace Stat.Cache
{
    class RemoveCache : IRemoveCache
    {
        #region ICache 成员
        /// <summary>
        /// 系统重启
        /// </summary>
        public void Restart()
        {
            Methods.ChangeCache(@"\web.config");
        }
        /// <summary>
        /// 清除广告缓存
        /// </summary>
        /// <returns></returns>
        public void RemoveAdCache()
        {
            ClearCache("AD_");
            /*-------------------------------------*/
            Methods.ChangeCache(@"\CacheTxt\AD.txt");
        }


        /// <summary>
        /// 清除友情缓存
        /// </summary>
        public void RemoveFrendLinkCache()
        {
            ClearCache("FL_");
            Methods.ChangeCache(@"\CacheTxt\FL.txt");
        }

        public void RemoveCompanyCache()
        {
            ClearCache("CompanyInfo");
            Methods.ChangeCache(@"\CacheTxt\Company.txt");
        }

        public void RemoveMessageCache()
        {
            ClearCache("Message_");
            Methods.ChangeCache(@"\CacheTxt\Message.txt");
        }

        public void RemoveNewsCache()
        {
            ClearCache("News_");
            Methods.ChangeCache(@"\CacheTxt\News.txt");

        }
        public void RemoveNotifyCache()
        {
            ClearCache("Notify_");
            Methods.ChangeCache(@"\CacheTxt\Notify.txt");
        }
        public void RemoveSysCache()
        {
            ClearCache("Config");
            Methods.ChangeCache(@"\CacheTxt\Sys.txt");
        }
        public void RemoveNavigateCache()
        {
            ClearCache("Navigate_");
            Methods.ChangeCache(@"\CacheTxt\Navigate.txt");
        }
        public void RemoveWebsiteCache(int webid)
        {
            System.Web.Caching.Cache mycache = HttpRuntime.Cache;
            mycache.Remove("Website_" + webid.ToString());//站点缓存
            mycache.Remove("Statsite_" + webid.ToString());//统计缓存
            mycache.Remove("StatRole_" + webid.ToString());//规则缓存
        }
        /*----------------------------------------------*/

        /// <summary>
        /// 清除系统配置缓存
        /// </summary>
        public void RemoveConfigCache()
        {
            Methods.ChangeCache(@"\CacheTxt\Sys_Config.txt");
        }

        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public void RemoveAllCache()
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }
        /// <summary>
        /// 获取所有的缓存
        /// </summary>
        /// <returns></returns>
        public System.Collections.IDictionaryEnumerator GetAllCache()
        {
            System.Web.Caching.Cache appCache = HttpRuntime.Cache;
            return appCache.GetEnumerator();
        }


        /// <summary>
        /// 清除某栏目文章缓存
        /// </summary>
        /// <param name="columnID"></param>
        public void RemoveColumnCache(int columnID)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                if (CacheEnum.Key.ToString().IndexOf("_Column" + columnID) > -1)
                {
                    _cache.Remove(CacheEnum.Key.ToString());
                }
            }
        }
        /// <summary>
        /// 清除某地区文章缓存
        /// </summary>
        /// <param name="areaID"></param>
        public void RemoveAreaCache(int areaID)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                if (CacheEnum.Key.ToString().IndexOf("_Area" + areaID) > -1)
                {
                    _cache.Remove(CacheEnum.Key.ToString());
                }
            }

        }
        #endregion

        private static void ClearCache(string cName)
        {
            System.Web.Caching.Cache caches = HttpRuntime.Cache;
            System.Collections.IDictionaryEnumerator CacheEnum = caches.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                if (CacheEnum.Key.ToString().IndexOf(cName) > -1)
                {
                    caches.Remove(CacheEnum.Key.ToString());
                }
            }
        }


    }
}
