.NET:使用CacheDependency在数据库或文件系统中进行缓存 [英] .NET: caching in database or filesystem with a CacheDependency
问题描述
我想知道是否有一种方法可以将.NET缓存(而不是内存中的缓存)存储在数据库表或文件中,还可以使用CacheDependency基础结构.我认为企业库也许可以做到这一点,但如果可能的话,我希望更简单一些.
I would like to know if there is a way to have the .NET cache stored in a database table or file, rather than the in-memory cache, but also use the CacheDependency infrastructure. I think the Enterprise Library may be able to do this but I would prefer something simpler if possible.
为说明起见,我希望将缓存的数据放入SQL表或磁盘上的文件中,而不是放在内存中.然后,我想指定一个CacheDependency,它可以是SqlCacheDependency,也可以是文件上的CacheDependency或AggregateCacheDependency.
To explain, I want the cached data to be put in an SQL table or into a file on disk, rather than in-memory. I then want to specify a CacheDependency, which could be an SqlCacheDependency, or a CacheDependency on a file or an AggregateCacheDependency.
原因是,我基于xml文档(每天大约更改一次)进行大量计算.这些计算的结果我缓存在内存中.但是,如果网站被重置,则缓存将丢失.最好在数据库中有一个备份缓存.
The reason is, I do intensive calculations based on xml documents (that change once a day or so). The results of these calculations I cache in memory. However if the website is reset, the cache is lost. It would be nice to have a backup cache in the database.
推荐答案
使用SqlCacheDependency .您需要自己手动管理目标数据,但是您可以在缓存中具有依赖关系,当目标表发生更改时,该依赖关系将自动失效.
Use SqlCacheDependency. You'll need to manually manage the target data yourself, but you can have a dependency in the cache which is automatically invalidated when the target table changes.
是的,您可以使用企业库中的缓存块来拥有一个缓存提供程序,该提供程序可以使用数据访问块(它是ASP.NET的替代方法)将项目存储在数据库中.尽管学习曲线更高,但这可能是最好的解决方案.
Yes, you can use the Caching Block in Enterprise Library to have a cache provider that stores items in a database using the Data Access Block, which is an alternative to the ASP.NET. That's likely to be the best solution, despite the higher learning curve.
我认为 SqlCacheDependency
更简单,因为您使用ASP.NET缓存首先存储本地内存版本以进行更快的处理,而当本地层没有可用的项目时,它会回退到数据库层.这是我的意思的示例:
I think SqlCacheDependency
is simpler because you use the ASP.NET cache to store a local memory version first for faster processing, falling back to the database layer when the local layer does not have the item available. Here's an example of what I mean:
-
您的Web应用程序调用
GetXmlDocument(字符串键)
来使用某些东西
该方法首先检查ASP.NET缓存中的项目-如果该项目存在,很好,您将其返回
The method checks the ASP.NET cache first for the item - if the item exists, great, you return that
如果该项目不存在,则检查数据库是否存在该项目-如果该项目存在,则使用 SqlCacheDependency
重新填充本地缓存项目数据库表,并返回XML数据
If the item doesn't exist, then you check the database for the existence of the item - if the item exists, you re-populate the local cache item with a SqlCacheDependency
on the database table, and return the XML data
如果该项目也不存在于数据库中,则从主要来源所在的位置检索XML数据,重新填充数据库,并使用 SqlCacheDependency重新填充本地缓存项目代码>,并返回XML数据
If the item doesn't exist in the database either, you retrieve the XML data from wherever the primary source is, re-populate the database, re-populate the local cache item with SqlCacheDependency
on the table, and return the XML data
这为您提供了一个自我管理的两层缓存.
This gives you a self-managing two-tiered cache.
这篇关于.NET:使用CacheDependency在数据库或文件系统中进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!