CacheManager.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Library » Providers » Caching » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Library » Providers » Caching » CacheManager.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

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 http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Everest.Library.Providers.Caching{
    /// <summary>
    /// cache manager
    /// </summary>
    public static class CacheManager
    {
        volatile static IDictionary<string, IList<string>> cacheGroup = new Dictionary<string, IList<string>>();
        const string GroupKey = "__GroupName:{0}__Key:{1}__";
        /// <summary>
        /// cache provider
        /// </summary>
        static volatile ICacheProvider cacheProvider = null;
        static object lockHelper = new object();
        /// <summary>
        /// Gets the cache provider.
        /// </summary>
        /// <value>The cache provider.</value>
        public static ICacheProvider CacheProvider
        {
            get
            {
                if (cacheProvider == null)
                {
                    lock (lockHelper)
                    {
                        if (cacheProvider == null)
                        {
                            cacheProvider = UnityManager.Resolve<ICacheProvider>();
                        }
                    }
                }
                return cacheProvider;
            }
        }

        /// <summary>
        /// Adds new CacheItem to cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void Add(string key, object value)
        {
            CacheProvider.Add(key, value);
        }
        /// <summary>
        /// Adds the specified group name.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void Add(string groupName, string key, object value)
        {
            string cacheKey = GetCacheKey(groupName, key);
            AddToGroup(groupName, key);
            Add(cacheKey, value);
        }

        private static void AddToGroup(string groupName, string key)
        {
            if (!cacheGroup.ContainsKey(groupName))
            {
                lock (lockHelper)
                {
                    if (!cacheGroup.ContainsKey(groupName))
                    {
                        cacheGroup.Add(groupName, new List<string>());
                    }
                }
            }
            IList<string> keyList = cacheGroup[groupName];
            lock (lockHelper)
            {
                if (!keyList.Contains(key))
                {
                    keyList.Add(key);
                }
            }
        }
        public static string GetCacheKey(string groupName, string key)
        {
            return string.Format(GroupKey, groupName, key);
        }
        /// <summary>
        /// Adds new CacheItem to cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="priority">The priority.</param>
        /// <param name="refreshAction">The refresh action.</param>
        /// <param name="expirations">The expirations.</param>
        public static void Add(string key, object value, CacheItemPriority priority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            CacheProvider.Add(key, value, priority, refreshAction, expirations);
        }
        /// <summary>
        /// Adds the specified group name.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="priority">The priority.</param>
        /// <param name="refreshAction">The refresh action.</param>
        /// <param name="expirations">The expirations.</param>
        public static void Add(string groupName, string key, object value, CacheItemPriority priority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            string cacheKey = GetCacheKey(groupName, key);
            AddToGroup(groupName, key);

            Add(cacheKey, value, priority, refreshAction, expirations);
        }
        /// <summary>
        /// Returns true if key refers to item current stored in cache
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        ///   <c>true</c> if [contains] [the specified key]; otherwise, <c>false</c>.
        /// </returns>
        public static bool Contains(string key)
        {
            return CacheProvider.Contains(key);
        }
        /// <summary>
        /// Returns the number of items currently in the cache.
        /// </summary>
        /// <value>The count.</value>
        public static int Count
        {
            get { return CacheProvider.Count; }
        }

        /// <summary>
        /// Removes all items from the cache.
        /// </summary>
        public static void Flush()
        {
            CacheProvider.Flush();
        }
        /// <summary>
        /// Returns the value associated with the given key.
        /// </summary>
        /// <returns></returns>
        public static object Get(string key)
        {
            return CacheProvider.Get(key);
        }

        /// <summary>
        /// Gets the specified group name.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static object Get(string groupName, string key)
        {
            string cacheKey = GetCacheKey(groupName, key);
            return Get(cacheKey);
        }

        /// <summary>
        /// Removes the given item from the cache. If no item exists with that key, this method does nothing.
        /// </summary>
        /// <param name="key">The key.</param>
        public static void Remove(string key)
        {
            CacheProvider.Remove(key);
        }

        /// <summary>
        /// Removes the cache group.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        public static void RemoveCacheGroup(string groupName)
        {
            if (cacheGroup.ContainsKey(groupName))
            {
                IList<string> keyList = cacheGroup[groupName];
                if (keyList != null)
                {
                    lock (lockHelper)
                    {
                        while (keyList.Count > 0)
                        {
                            string key = keyList.First();
                            Remove(GetCacheKey(groupName, key));
                            keyList.Remove(key);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Removes the cache groups.
        /// </summary>
        /// <param name="groups">The groups.</param>
        public static void RemoveCacheGroups(IEnumerable<string> groups)
        {
            foreach (var group in groups)
            {
                RemoveCacheGroup(group);
            }
        }
        /// <summary>
        /// Removes the specified group name.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="key">The key.</param>
        public static void Remove(string groupName, string key)
        {
            Remove(GetCacheKey(groupName, key));
        }

        /// <summary>
        /// Removes all groups.
        /// </summary>
        public static void RemoveAllGroups()
        {
            RemoveCacheGroups(cacheGroup.Keys);
        }

        /// <summary>
        /// Gets the data by the specified key.
        /// if data not in cache,use dataFunc to get data and store into cache.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="dataFunc">The data func.</param>
        /// <param name="expirations">The expirations.</param>
        /// <returns></returns>
        public static T Get<T>(object lockHelper, string key, Func<T> dataFunc, CacheItemPriority priority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
            where T : class
        {
            T data = CacheManager.Get(key) as T;
            if (data == null)
            {
                lock (lockHelper)
                {
                    if (CacheManager.Get(key) as T == null)
                    {
                        data = dataFunc();
                        CacheManager.Add(key, data, priority, refreshAction, expirations);
                    }
                }
            }
            return data;

        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.