数据库或文件中的值更改时如何加载缓存 [英] How to load Cache when value changes in database or file Changes

查看:104
本文介绍了数据库或文件中的值更改时如何加载缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道缓存是一种服务器端处理状态管理的方法.当数据库中的某些值发生更改时如何更新缓存以及如何在缓存中加载新数据的页面请也提供一些C#代码解决方案.

As we know cache is a server side handling state management.How we update cache when some values changes in database.and page get loaded with new data in cache.Please give some solution an c# code also.

推荐答案

有趣的问题,有几个可能的答案.
您使用缓存系统的原因是,您不必在每个页面请求上轮询数据库.但是有时数据确实会更改,并且缓存将过时,在某些应用程序中这可能是个问题.

如果您使用的是ASP.NET的内置缓存功能,则无需做很多工作.解决此问题的默认方法是让缓存在一段时间(超时)后自行清除,默认情况下,ASP.NET缓存会执行此操作.
您可以在此处了解更多信息: http://msdn.microsoft.com /en-us/library/6hbbsfk6(v=vs.100).aspx 和此处 ^ ]
诀窍是找出良好的超时时间,答案实际上取决于每分钟数据库更改的次数以及过时信息的重要性.对我来说20分钟的超时通常可以正常工作,但这实际上取决于您正在处理的数据类型.

如果100%准确的数据确实很重要,那么还有另一种方法可以做到,但是它要求更改数据库中数据的唯一方法是通过您的应用程序.
如果是这样,您每次用户更改数据库时都可以简单地清除缓存,这样就永远不会过时.但是,这会影响您的性能,但是取决于应用程序的规模和每分钟的更改次数,这可能是可以接受的.要从缓存中删除项目,只需调用.Remove用您的密钥获取数据库值,请参阅第二个链接以获取有关此信息的更多信息.

希望对您有所帮助:-)
Interesting question, with several possible answers.
A reason you would use a caching system is so that you don''t have to poll the database on each page request. However sometimes data does change, and your cache will be outdated, and in some applications that can be quite a problem.

If you are using the build-in caching features of ASP.NET you won''t have to do much work. The default way to handle this problem is by having your cache clear itself after a certain time, the timeout, the ASP.NET Cache does this by default.
You can read more about it here: http://msdn.microsoft.com/en-us/library/6hbbsfk6(v=vs.100).aspx and here http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.100).aspx[^]
The trick is to find out what a good timeout is, the answer really depends on the number of database changes per minute, and importance of outdated information. For me 20 minutes of timeout typically works fine, but it really depends on the kind of data you are handling.

If 100% accurate data is really important, there is another way to do it, but it requires the only way to change data in the database is via your application.
If so, you can simply clear the cache each time a user changes the database, this way it will never be outdated. This will however affect your performance, but depending on the scale of the application and the number of changes per minute, this might be acceptable. To remove the items from the cache, simply call .Remove with your key for the database values, see the 2nd link for more information on that.

I hope this helped :-)


这篇关于数据库或文件中的值更改时如何加载缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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