使用web api REST保存信息 [英] hold information with web api REST

查看:96
本文介绍了使用web api REST保存信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试让我的API服务每5分钟查询一次我的SQL数据库,保持他的响应,并在接下来的5分钟电话中分享这个响应。



那么,有没有办法可以用计时器运行查询?我怎么能保持这种反应。

我尝试使用会话,但我不知道它是否正确,以及如何确切地说。

解决方案

在Web应用程序中不可能有计时器...

您可能要做的是将您的响应存储在应用程序级别并附带时间戳...如果时间戳比你想要的时间要早​​,你应该重新加载你的响应并存储它而不是当前的,否则返回现有的...

  if ((DateTime.UtcNow  -  Application [  LastQuery]) .TotalSeconds >   300 
{
申请[ 响应] = LoadData();
应用程序[ LastQuery] = DateTime.UtcNow;
}

// 使用应用程序[响应]作为答案< /跨度>


Hi,
i am trying to make my API service to query my SQL DB every 5 minute, hold his response and share this response with the all next 5 minutes calls.

So, is there a way that i can run the query with timer? and how can i hold the response.
I tried to work with session but i don't know if it is the right way and how exactly to that.

解决方案

Timer is not a possibility inside web application...
What you may do is to store your response on the Application level accompanied with a time stamp...If time stamp is older than the period you want you should reload your response and store it instead of the current, otherwise return the existing one...

if((DateTime.UtcNow - Application["LastQuery"]).TotalSeconds > 300)
{
  Application["Response"] = LoadData();
  Application["LastQuery"] = DateTime.UtcNow;
}

// use Application["Response"] as your answer


这篇关于使用web api REST保存信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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