EnterpriseLibrary 程序缓存

EL的缓存原理:(个人理解)

  我们在一般的情况是直接去数据库或者数据文件中找到数据来进行操作,但如果我们像减少这些在一定时间内比较固定的数据,而不想做无谓的查询操作来减少IO的操作,提高性能,比如一些OA系统中的系统配置等,这些东西一般都是不怎么变化的,而每个用户进来都要去数据库读取一次,那就给数据库造成无谓的压力了。所以我们要做缓存。

  如果我们要加入缓存,那和我们平常的数据库查询又有什么不同呢? 其实这个和Session很类似。在没有缓存的情况下,我们是直接调用方法去查询,那如果要加入缓存,那就是我们首先要去缓存中判断是否有我们需要的数据,这个区分数据的方法就是给这些数据加入一个key值,这样就可以根据key值来判断数据是否为空了,如果没有数据,那我们这个时候才去带哦用我们平常的查询方法,并且把查询出来的数据放入到缓存中。这里的缓冲会根据web.config中配置的时间来删除数据,这样就起到定时刷新缓存的作用。

 

一个EnterpriseLibrary Caching例子

第一,添加引用:Microsoft.Practices.EnterpriseLibrary.Caching.dll

第二,在代码中加入:using Microsoft.Practices.EnterpriseLibrary.Caching; 

第三,在web.config中定义,其中GIS Setting Cache Manager就是一个例子;

复制代码
< cachingConfiguration defaultCacheManager = " Default Cache Manager " >
        
< backingStores >
            
< add name = " inMemory "  type = " Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching " />
        
</ backingStores >
        
< cacheManagers >
            
< add name = " Default Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
            
< add name = " Long Time Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
            
< add name = " Login User Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
            
< add name = " GIS Setting Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
            
< add name = " Setting Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
            
< add name = " Version Cache Manager "  expirationPollFrequencyInSeconds = " 60 "  maximumElementsInCacheBeforeScavenging = " 1000 "  numberToRemoveWhenScavenging = " 10 "  backingStoreName = " inMemory " />
        
</ cacheManagers >
    
</ cachingConfiguration >
复制代码

第四,在cs文件中定义 

private   const   string  CacheManagerGisSetting  =   " GIS Setting Cache Manager " ;
private   const   string  CacheKey  =   " GisSetting " ;
private   static  CacheManager cache  =  CacheFactory.GetCacheManager(CacheManagerGisSetting);

第五,定义方法,这个方法是提供给其他地方调用,如果在缓存中能找到你需要的数据,那就返回,如果没有就去数据库查询。

复制代码
         ///   <summary>
        
///  获取预警配置缓存.
        
///   </summary>
        
///   <returns></returns>
         private   static  DataTable GetWarnSettingCache()
        {
            DataTable dtSetting 
=   null ;
            
if  (cache.Contains(CacheKey)) // 使用key值去缓存中查询.
                dtSetting  =   cache.GetData(CacheKey)  as  DataTable;
            
if  (dtSetting  !=   null ) // 如果不为空,那就返回缓存中的数据.
                 return  dtSetting;
            
if  (nodeShowWarnSettingService  !=   null ) // 如果这个实例化的类不为null.
            {
                dtSetting 
=  nodeShowWarnSettingService.GetAllList(); // 查询方法,去数据库中查询.
                cache.Add(CacheKey, dtSetting); // 加入到缓存当中.
                 return  dtSetting;
            }
            
return   null ;
        }
复制代码

其它地方调用这个方法就可以了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值