净缓存过期 [英] .Net Cache expiration

查看:107
本文介绍了净缓存过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我们使用Web服务来从数据库数据,并填充从Web服务中的数据缓存。 我使用绝对期满,且持续时间被设定为4小时。 因此,4小时后,缓存过期,因此web服务调用以重新填充数据在缓存中。

这个问题现在发生的事情是:

有时是有问题建立web服务和数据库,因此,网络服务之间的连接不返回数据

在这种情况下,高速缓存中填充了所有字段的空值和长达4小时的高速缓存(高达它再次期满的时刻)它不重新填充。

我在找2类型的解决方案: 1)我们可以在过期之前更新缓存中,然后调用Web服务。如果Web服务获取的数据后面,覆盖掉缓存。 2)缓存不会过期,并且每隔一段时间,我们产生一个线程用于更新缓存,如果我们找回数据。

不过,我不知道如何实现上述解决方案?有人可以给我技术部分的见解?感谢您的帮助!

填充缓存我目前的code:

  ctx.Cache.Insert(cachename,cacheValue,空,
              DateTime.Now.AddHours(Int32.Parse(siteViewModel.ApplicationSettings [CacheDurationHours])),System.Web.Caching.Cache.NoSlidingExpiration,
             System.Web.Caching.CacheItemPriority.Default,
             空值
             );
 

解决方案

传球空的相反,实施的 CacheItemRemovedCallback 作为最后一个参数的 Cache.Insert(..)。当检索值 重新填充缓存,你可以做一个空检查,并使用旧值暂时。

In my project, we use web services to get data from the DB and populate the data from web services to cache. I am using Absolute expiration , and the duration is set to 4 hours. So after 4 hours, cache expires and therefore the web service is called to re populate the data in the cache.

The problem happening now is :

Sometimes there is a problem establishing connection between web services and database and therefore the web services does not return data.

In this case the cache is populated with null values for all fields and up to 4 hours cache (up to the time it expires again) it is not re populated.

I am looking for 2 types of solutions: 1)we could renew the cache before it expires, then call the web service. If the web service gets data back, overwrite the cache. 2) the cache never expires and every so often we spawn a thread which updates the cache if we get data back.

But I am not sure how to implement the above solutions? Can somebody give me insight on technical part? Thanks for the help!

MY current code of populating cache :

ctx.Cache.Insert("cachename", cacheValue, null,
              DateTime.Now.AddHours(Int32.Parse(siteViewModel.ApplicationSettings["CacheDurationHours"])), System.Web.Caching.Cache.NoSlidingExpiration, 
             System.Web.Caching.CacheItemPriority.Default,
             null
             );

解决方案

Instead of passing null, implement a CacheItemRemovedCallback as the last parameter to Cache.Insert(..). When retrieving the value to repopulate the cache, you can do a null check and use the old value for the time being.

这篇关于净缓存过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆