Wednesday, 6 June 2012

Cache Helper class in c#

using System;
using System.Web;
using System.Web.Caching;

public static class CacheHelper
    {
        public static void Add<T>(string key, T o) where T : class
        {
            HttpContext.Current.Cache.Insert(key, o);
        }

        public static void Add<T>(string key, T o, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration) where T : class
        {
            HttpContext.Current.Cache.Insert(key, o, dependencies, absoluteExpiration, slidingExpiration);
        }

        public static void Clear(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        public static bool Exists(string key)
        {
            return HttpContext.Current.Cache[key] != null;
        }

        public static T Get<T>(string key) where T : class
        {
            try
            {
                return (T)HttpContext.Current.Cache[key];
            }
            catch
            {
                return null;
            }
        }
    }
--------------------------------------------------------------------------------------------------------------
 public class CacheManager
    {
        public List<Event> GetEventDetails()
        {
            PageInfo pageInfo = GetPageInfoGroup();
            return pageInfo.eventlists;
        }

        public List<StaticPage> GetStaticPage()
        {
            PageInfo pageInfo = GetPageInfoGroup();
            return pageInfo.pagelists;
        }

        public List<Practise> GetPractiseDetails()
        {
            PageInfo pageInfo = GetPageInfoGroup();
            return pageInfo.practiselists;
        }

        public List<Video> GetVideoDetails()
        {
            PageInfo pageInfo = GetPageInfoGroup();
            return pageInfo.videolists;
        }

        public List<Profile> GetProfileDetails()
        {
            PageInfo pageInfo = GetPageInfoGroup();
            return pageInfo.profilelists;
        }      

        private PageInfo GetPageData()
        {
            PageInfo info = new PageInfo();
            info.pagelists = GetPageList();
           // info.eventlists = GetEventList();
            info.practiselists = GetPractiseList();
            info.profilelists = GetProfileLists();
            info.videolists = GetVideoList();
            return info;
        }

        private List<Profile> GetProfileLists()
        {
            Profile info = new Profile();
            List<Profile> infolists = info.GetProfileDetails();

            return infolists;
        }

        private List<Practise> GetPractiseList()
        {
            Practise info = new Practise();
            List<Practise> infolists = info.GetPractiseDetails();

            return infolists;
        }

        private List<Event> GetEventList()
        {
            Event info = new Event();
            List<Event> infolists = info.GetEventDetails();

            return infolists;
        }

        private List<StaticPage> GetPageList()
        {
            StaticPage info = new StaticPage();
            List<StaticPage> infolists = info.GetStaticPageDetails();

            return infolists;
        }

        private List<Video> GetVideoList()
        {
            Video info = new Video();
            List<Video> infolists = info.GetAllActiveVideoDetails();

            return infolists;
        }

        private PageInfo GetPageInfoGroup()
        {
            PageInfo pageInfo = null;
            pageInfo = CacheHelper.Get<PageInfo>(Constants.CacheKeyName.CurrentDatePageAdInfoKey);
            if (pageInfo == null)
            {
                // Remove earlier cached data key
                CacheHelper.Clear(Constants.CacheKeyName.NonCurrentDatePageAdInfoKey);

                // Get Current date Release List from the database
                pageInfo = GetPageData();

                // Add Current date Release List in the cache
                CacheHelper.Add<PageInfo>(Constants.CacheKeyName.CurrentDatePageAdInfoKey, pageInfo);
            }

            return pageInfo;
        }
    }

No comments:

Post a Comment