简体   繁体   English

在ASP.NET中使用WCF服务,如何缓存?

[英]Consuming WCF Service in ASP.NET, how to cache?

I have two situations in this case: 在这种情况下,我有两种情况:

I want to query a WCF service and hold the data somewhere, because one of the web pages renders based on the data that's retrieved from the service. 我想查询WCF服务并将数据保存在某处,因为其中一个网页是根据从服务中检索到的数据来呈现的。 I don't want the page itself querying the service, but I'd rather have some sort of scheduled worker that runs once every a couple of minutes, and retrieves the data and holds it somewhere. 我不希望页面本身查询服务,但我希望有某种调度的工作程序每隔几分钟运行一次,并检索数据并将其保存在某个地方。

Where should I cache the service response, and what is the correct way to create the task to query the service every couple minutes? 我应该在哪里缓存服务响应,以及每两分钟创建一次查询服务的任务的正确方法是什么?

I think I could achieve this by saving the response to a static variable, alongside the last query date, and then check on the page load if enough time has passed, I call the service and refresh the data, else I use the static cache. 我想我可以通过将响应保存到静态变量以及最后一个查询日期来实现此目的,然后检查页面加载是否经过了足够的时间,然后调用服务并刷新数据,否则使用静态缓存。

This would also account for the case where no users access the page for a long time, and the site not futilely querying the service. 这也可以解决以下情况:长时间没有用户访问该页面,并且站点没有对服务进行无用的查询。

But it seems kind of rough, are there other, better ways to accomplish this kind of task? 但这似乎有些粗糙,还有其他更好的方法可以完成此类任务吗?

你看过速度了

You could indeed take another approach like having a scheduled program query the information and put it in an in-memory cache available to all the web servers in your farm. 您确实可以采取另一种方法,例如让计划的程序查询信息并将其放入服务器场中所有Web服务器可用的内存缓存中。 However, whether that would be better for your scenario depends on the size of your app and how much time/effort you want to spend on it. 但是,这是否适合您的情况取决于应用程序的大小以及您要花费多少时间/精力。

An in-memory cache is harder to implement/support than a static variable but it's sometimes better since static variables can be cleared up every time the server resets (eg after X number of minutes of inactivity) 内存中的缓存比静态变量更难实现/支持,但有时会更好,因为每次服务器重置时(例如,在X闲置数分钟后),静态变量都可以清除。

Depending on the size of your system I would start with the static variable, test drive the approach for a while and then decide if you need something more sophisticated. 根据系统的大小,我将从静态变量开始,测试一下该方法一会儿,然后确定是否需要更复杂的东西。

Nico: Why don't you write a simple console daemon that gets the data and stores it on your end in a database and then have your web app get the data from your local copy? Nico:为什么不编写一个简单的控制台守护程序来获取数据并将其存储在数据库中,然后让您的Web应用程序从本地副本获取数据? You can make that console app run every certain amount of time. 您可以使控制台应用程序每隔一定时间运行一次。 Inserting the data should not be a problem if you are using sql server 2008. You can pass datatable parameters to a stored proc and insert a whole table in one call. 如果使用的是sql server 2008,则插入数据应该没有问题。可以将datatable参数传递到存储的proc并在一个调用中插入整个表。 If you don't use Sql Server 2008, then serialize the whole collection returned by the web service and store in a table in one big blob column and record the timestamp when you got the data. 如果您不使用Sql Server 2008,则序列化Web服务返回的整个集合,并将其存储在一个大blob列中的表中,并在获取数据时记录时间戳。 You then can read the content of that column, deserealize your collection and reconstruct it to native objects for displaying on your page. 然后,您可以阅读该列的内容,取消集合化,并将其重建为本机对象以显示在页面上。

I've never seen (and I don't think its possible) to have your web app query the web service every certain amount of time. 我从未见过(而且我认为这不可能)让您的Web应用程序每隔一定时间查询Web服务。 Imagine the web site is idle for hours therefore no interaction from anybody. 想象一下,该网站闲置了几个小时,因此没有任何人进行任何交互。 That means that no events will fire and nothing will be queried. 这意味着不会发生任何事件,也不会查询任何内容。

Alternatively, you could create a dummy page executing a javascript function at certain intervals and have that javascript function make an ajax request to the server to get the data from the web service and cache the data. 或者,您可以创建一个虚拟页面,该页面以一定的时间间隔执行javascript函数,并使该javascript函数向服务器发出ajax请求,以从Web服务获取数据并缓存数据。 The problem is that the minute you walk out of that page, nothing will happen and you'll stop querying the web service. 问题在于,当您离开该页面的那一刻,将不会发生任何事情,并且您将停止查询Web服务。 I think this is silly. 我认为这很愚蠢。

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

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