在哪里实现缓存-类库或Windows服务 [英] Where to implement caching - Class Library or Windows Service

查看:37
本文介绍了在哪里实现缓存-类库或Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,该服务使用计时器定期(在工作线程上)调用类库.此类库具有所有必需的应用程序功能,Windows Service只是简单的托管环境.库作为其执行的一部分,需要调用数据库并获取一堆记录.这些记录不经常更改(想想几周),我想将它们缓存在内存中.我应该在类库或Windows服务中实现缓存结构吗?

I have a Windows Service that calls a class library (on a worker thread) periodically using a timer. This class library has all the required application functionality and the Windows Service is nothing but a simple hosting environment. The library as part of its execution needs to make a call to the DB and fetch a bunch of records. These records don't change frequently (think weeks) and I'd like to cache them in memory. Should I implement the caching structure in the class library or the Windows Service?

基本上我不确定的是,一旦Windows Service加载,然后定期调用该类库,该库运行的应用程序域对于该库的所有执行(通过工作程序)是否保持不变线程每隔几分钟).因为如果不是这样,在库中实现缓存的目的似乎毫无意义.

Essentially what I am a bit unsure of is, once a Windows Service loads, and then makes periodic calls to this class library does the app domain that the library runs in remain the same for all executions of the library (via a worker thread every few minutes). Because if it isn't, the purpose of implementing the cache inside the library seems pointless.

有人可以帮助我理解这一点吗?

Could someone help me understand this?

推荐答案

这是一个有效的设计问题,但我认为您是从错误的角度来解决这个问题:而不是考虑应用程序域和其他可能使它变得更难的事情为实现该功能,请从逻辑设计的角度考虑它的位置.

This is a valid design question, but I think you are approaching it from a wrong angle: rather than thinking about app domains and other things that may make it harder for you to implement the functionality, think where it belongs from the point of view of the logical design.

以下一些因素可能会影响您的思维:

Here are some considerations that may influence your thinking:

  • 您的类库为用户提供了一定的接口.对于所有调用您的类库并位于内存高速缓存层后面的用户来说,这有意义吗?如果对这个问题的回答是是",则缓存功能属于类库.
  • 由于类库是一个单独的实体,因此您大概想向该库的客户隐藏一些实现细节,例如窗口的服务.如果服务要在一段时间内缓存数据,则该服务将拥有数据不频繁更改的知识.如果不希望这样,请将缓存功能放在类库中.
  • 如果数据的性质可以比您正在编写的特定Windows服务更频繁地更改,则缓存属于Windows服务.
  • 如果您计划将来实现可控制缓存状态的其他功能(例如,强制缓存无效的方法),则该功能属于Windows服务(尽管您也可以将其放在类中)库,并为其用户提供对其状态的明确控制.)

这篇关于在哪里实现缓存-类库或Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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