Friday, December 6, 2013

How to use SQLCacheDependency in ASP.NET MVC

Database
Database : Employee
Table : EmpDetails
Developer Command Prompt
aspnet_regsql.exe -S DBServerName -E -d database name -ed 
aspnet_regsql.exe -S DBServerName -E -d database name -t table name -et
Web.Config
  
    
      
        
          
        
      
    
  
Cache Class
public class SQLDependencyCacheProvider
    {
        private static System.Web.Caching.Cache _cache;

        public System.Web.Caching.Cache Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = HttpRuntime.Cache;
                }

                return _cache;
            }
        }

        public T GetCacheItem(string key)
        {
            var item = Cache.Get(key);
            return (item != null) ? (T)Convert.ChangeType(item, typeof(T)) : default(T);
        }

        public void InsertCacheItem(string key, T value)
        {
            Cache.Insert(key, value, new SqlCacheDependency("Employee", "EmpDetails"));
        }
    }


No comments:

Post a Comment