-2

I often received messages objects and for each of them I need to query the database in order to achieve some additional information from one database table. Since we are performing many requests to dabatase that it's very inefficient.

So, how I could improve the performance ? I think I need to persist the table data to a local list or hashtable and update it when some modifications are happened in the database.

1 Answers1

1

.net has a MemoryCache object which you can use to persist the results of a query inorder to limit the number of requests.

https://docs.microsoft.com/en-gb/dotnet/api/system.runtime.caching.memorycache?view=netframework-4.7.1

Typically you don't want to persist the object very long, say a few seconds, as this prevents a high volume of requests per second while limiting the problem of having to refresh the cache when the data changes and the amount of memory the cache uses.

There are various advanced options for cache invalidation other than an absolute timeout though. You can set a trigger to invalidate the cache, or update the cached version when you do an update if required.

Obviously this becomes more complicated if you have a farm of servers.

Ewan
  • 83,178