简体   繁体   English

ASP.NET Web Api:跨请求公开一个大型的静态集合

[英]ASP.NET Web Api: Exposing a large, static collection across requests

We have an API (ASP.NET Web Api 2, to be specific) which needs to traverse through a very large collection of entities in some algorithmic fashion in order to research the desired result. 我们有一个API(具体来说是ASP.NET Web Api 2),该API需要以某种算法方式遍历大量实体,以便研究所需的结果。

This collection is the same for every query performed. 对于每个执行的查询,此集合都是相同的。 It forms the data "source" for every call. 它为每个呼叫形成数据“源”。

Properties of the collection: 集合的属性:

  • Ordered 有序
  • Large (hundreds of thousands of objects) 大(数十万个对象)
  • Read-only 只读
  • Must be independently traverseable by concurrent threads from the application pool 必须可以被应用程序池中的并发线程独立遍历
  • The items in the collection are objects consisting of only a few value types. 集合中的项目是仅由几种值类型组成的对象。

Reading this collection from disk or database every time a request to the API is made results in a lot of overhead. 每次向API发出请求时,从磁盘或数据库中读取此集合都会导致大量开销。 So, we persist it statically, almost as a singleton. 因此,我们几乎以单例形式静态保留它。 This seems to work as static objects are shared between all requests to the application and have the same lifetime as the application domain. 这似乎起作用,因为在对应用程序的所有请求之间共享了静态对象,并且这些对象的寿命与应用程序域相同。 This results in almost instant requests from the API. 这几乎导致来自API的即时请求。

Is there perhaps a better pattern, practice or framework for such a problem? 对于这样的问题,也许有更好的模式,实践或框架吗?

If you really never need to refresh your data, your solution is ok. 如果您真的不需要刷新数据,则可以解决。

But only until you have to scale to more than one web server. 但是只有在您必须扩展到多个Web服务器之前。 Reached this point you'll probably need to be sure that all the web servers have the same data, and - depending on you environment and architecture - this could be tricky. 达到这一点时,您可能需要确保所有Web服务器具有相同的数据,并且-根据您的环境和体系结构-这可能很棘手。 Perhaps if you'll ever reach this point, you will ask another question on SO... 也许如果您能达到这一点,您将对SO提出另一个问题。

You can use cache servers like Redis or mecmached. 您可以使用Redis或mecmached之类的缓存服务器。

In this SO you have a comparison between them, which will also explain what they're all about, and what the difference is with your current implementation: Memcached vs. Redis? 在此SO中,您将对它们进行比较,这也将解释它们的含义,以及当前实现的区别: Memcached与Redis?

And, of course, here you have the official web sites for each: 并且,当然,这里有每个网站的官方网站:

There is even a third competitor: hazelcast . 甚至还有第三个竞争对手: hazelcast

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

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