Where to implement caching - Class library or Windows service

I have a windows service that periodically calls a class library (in a workflow) using a timer. This class library has all the necessary functionality of the application, and the Windows service is just a simple hosting environment. The library as part of its execution should make a call to the database and get a bunch of records. These records do not change often (I think, weeks), and I would like to cache them in memory. Should I implement a caching structure in a class library or Windows service?

In fact, I'm a little unsure that, as soon as Windows boots up and then makes periodic calls to this class library, the application domain in which the library runs remains unchanged for all library executions (through a working thread every few minutes) . Because if this is not the case, the goal of implementing the cache inside the library seems pointless.

Can someone help me figure this out?

+4
source share
3 answers

This is the right design question, but I think you are approaching it from the wrong angle: instead of thinking about application domains and other things that can complicate your implementation of the functionality, think about where it belongs from the point of view of the logical design.

Here are some considerations that may affect your thinking:

  • Your class library provides a specific interface for its users. Does it make sense for all users who call your class library to sit behind a cache layer in memory? If the answer to this question is yes, then the caching function belongs to the class library.
  • Since the class library is a separate object, presumably you would like to hide some implementation details from library clients, such as the window service. If a service caches data for a period of time, the service will have knowledge that the data does not change often. If this is not desired, add caching functions to the class library.
  • If the nature of the data can change more often than for the specific Windows service you are writing, then caching belongs to the windows service.
  • If you plan to introduce additional features in the future that will monitor the state of the cache, for example, a way to force the cache to be deleted, the functionality belongs to the Windows service (although you could also put it in a library class and give your users explicit control over their state).
0
source

In a similar situation, I implemented a cache in the library (since this is your main code base), but made it independent of the main call service.

Something like that

class ServiceRun { private MyLibrary.LibraryContext _context; private Timer _timer; public ServiceRun() { _context = MyLibrary.Core.InitializeBaseData(); _timer = new Timer(10000); _timer.OnTick+= ()=> MyLibrary.Core.DoAction(_context); } } 

If LibraryContext care of its own integrity or service, you need to call something like _context.Refresh() with a different timer, which completely depends on your choice, because it is unlikely to depend on the data stored in it.

0
source

Your caching methods will be based on your implementation. From what you described above, your service is a simple wrapper. This shell calls the class library (I use through threads), which executes the actual process.

With this design, I would suggest implementing your "caching services" in the class library. Although classes in your library are executed and then deleted, there is no reason why your class library cannot keep cache references after other classes in the library complete.

Personally, since the class library requires cached objects, I see no reason why the service needs access to these objects. In addition, by maintaining a cache in your class library, you can "hide" your cached objects. Finally, another big positive result is debugging, and fixing bugs will be much easier. Since you can run the class library inside any other application, you are not required to debug the Windows service, which can be quite a challenge.

I think the real question is what kind of caching should you use, this greatly affects the shared memory consumed. Now this is a different question.

You have a lot of options for caching. The most common in-memory caching is performed using the MemoryCache function as part of the .Net infrastructure. This supports expreration policies and a complete Cacheing wrapper similar to the ASP.Net web implementation.

MSDN for the memory cache: http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

In the end, in my opinion, I would probably transfer my caching system to another class, using Singletons or through DependencyInjection to support cached objects.

Hope this helps.

Niko

0
source

Source: https://habr.com/ru/post/1494175/


All Articles