简体   繁体   English

使用ASP.NET MVC应用程序定期插入Azure数据库

[英]Periodically insertion into Azure database with ASP.NET MVC application

I am wondering how to implement a solution that will retrieve data that I have scraped, and use it to display in an ASP.NET MVC web application. 我想知道如何实现一个解决方案,它将检索我已经抓取的数据,并使用它来显示在ASP.NET MVC Web应用程序中。

The current implementation scrapes the data and displays it from the controller to the view, however by doing so, the request to view the web page will take very long due to the scraper running when a request to view the page with scraped data is processed. 当前实现刮擦数据并将其从控制器显示到视图,但是通过这样做,当处理查看具有抓取数据的页面的请求时,由于刮刀运行,查看网页的请求将花费很长时间。

Is there any implementation I can do to separate the data retrieval and the website? 我可以做任何实现来分离数据检索和网站吗?

Currently I have a console application scraper class that scrapes data, and a ASP.NET MVC web application that will display the data. 目前我有一个控制台数据的应用程序刮板类,以及一个显示数据的ASP.NET MVC Web应用程序。 How can I couple them together easily? 我怎样才能轻松将它们结合在一起?

Based on system size I think you can do 2 things: 根据系统大小,我认为你可以做两件事:

  • Periodically scrape data and save it in the memory 定期刮取数据并将其保存在内存中
  • Periodically scrape data and save it in the database 定期刮取数据并将其保存在数据库中

It is oblivious that if scrapped data is big you need to store it in database, otherwise you can keep it memory and highly boost performance. 遗憾的是,如果报废数据很大,您需要将其存储在数据库中,否则您可以保留内存并大大提高性能。

Running tasks in asp.net periodically is covered by background workers. 后台工作人员定期在asp.net中运行任务。 Some easy way to periodically run tasks is to initiate thread in Application_Start . 定期运行任务的一些简单方法是在Application_Start启动线程。 I don't go more deeply in implementation, because it is already answered. 我没有更深入地实施,因为它已经得到了回答。 You can reed it here: Best way to run scheduled tasks 你可以在这里reed: 运行计划任务的最佳方式

For saving data in memory you can use something like this: 为了在内存中保存数据,您可以使用以下内容:

public static class Global
{
    public static ConcurrentBag<ScrapedItem> ScrapedItems;
} 

*Note, it is necessary to use thread-safe collection, because of getting and adding to this collection will be done from different threads: one from background worker, one from request. *注意,有必要使用线程安全的集合,因为获取和添加到此集合将从不同的线程完成:一个来自后台工作者,一个来自请求。 Or you can use lock object when getting/setting to not thread safe collection. 或者,您可以在获取/设置为非线程安全集合时使用lock对象。

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

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