简体   繁体   English

具有多个数据库/ SqlServer的数据库相关缓存

[英]Database Dependent Caching w/ Multiple Databases/SqlServers

I'm working in an environment where a single web application (running in a web-farm) dynamically connects to different SQL Server databases (possibly on different servers) based on user selection (this part is non-negotiable, unless you can gift me $1,000,000 and/or some kind of time altering device). 我在一个环境中工作,其中单个Web应用程序(在Web场中运行)根据用户选择动态连接到不同的SQL Server数据库(可能在不同的服务器上)(这一部分不可商议,除非您可以赠予我) $ 1,000,000和/或某种时间更改装置)。

I'd like to cache some of the commonly used data from the database(s) in the application, such as "settings", and am looking for a best practice to use with the SqlCacheDependency class or a related database cache dependent solution (not opposed to a custom implementation of SQL notification or polling; notification is preferred over polling). 我想从应用程序中的数据库中缓存一些常用数据,例如“设置”,并正在寻找与SqlCacheDependency类或相关的数据库缓存相关解决方案配合使用的最佳做法(不相对于SQL通知或轮询的自定义实现;通知优先于轮询)。

So far, I'm thinking of using a collection of SqlCacheDependency objects (one for each database) that can be referenced by connection string name or related key. 到目前为止,我正在考虑使用SqlCacheDependency对象的集合(每个数据库一个),该对象可以由连接字符串名称或相关键引用。 So when the connection is changed to a different database, the cache can be requested/populated by a key of <databasekey>+<datakey> and use the appropriate SqlCacheDependency object (probably handled by a cache-managing wrapper). 因此,当连接更改为其他数据库时,可以通过键<databasekey> + <datakey>请求/填充缓存,并使用适当的SqlCacheDependency对象(可能由缓存管理包装器处理)。

Any ideas, suggestions, or experience in this area? 在这方面有什么想法,建议或经验吗?

Or is it just better to take the hit and always go to the database? 还是总是受到打击并总是进入数据库会更好?

you can use nhibernate or hibernate with 2nd level cache in your data access code. 您可以在数据访问代码中将nhibernate或hibernate与2级缓存一起使用。 for caching part you can use memcached or velocity (appfabric). 对于缓存部分,您可以使用memcached或Velocity(appfabric)。

In my opinion, you should not store all settings from different databases in your web application. 我认为,不应将来自不同数据库的所有设置存储在Web应用程序中。 The better way is to store only settings of the database that's currently in use. 更好的方法是仅存储当前使用的数据库设置。

You told that, at some point, user selects the database and you creating connection, opening it, reading the data and so on. 您告诉用户,在某个时候,用户选择数据库,然后创建连接,打开数据库,读取数据等。 This is a good place to read your settings only for this database and use them in the application. 这是一个仅读取此数据库的设置并在应用程序中使用它们的好地方。 At the same moment, you can adjust SqlCacheDependency to be notified when settings in your current database has changed. 同时,您可以调整SqlCacheDependency,以在当前数据库中的设置更改时得到通知。

When user selects another database, you just drop current settings and load another ones from newly selected database. 当用户选择另一个数据库时,您只需删除当前设置并从新选择的数据库中加载另一个设置。

This is much easier to implement, in my opinion. 我认为这更容易实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM