Answer the question
In order to leave comments, you need to log in
Caching data in server memory?
Good morning, this question is very interesting. The idea is that we have a repository pattern and wrap it with a cache, for example, it will be possible to implement this using MemoryCache . But this raises a number of other questions :
Answer the question
In order to leave comments, you need to log in
1. In MemoryCache there is a concept of Expiration (Time of life of a piece of data). Those. you conditionally set how long to store the given object in the cache. Depends on your tasks. Let's say 5 minutes.
You set 5 minutes, for the object you put it in MemoryCache using the Add method.
Upon receipt, do Get - if the object has not expired, the object will be returned, if it has expired - get null - check for null - get data the old fashioned way, set Expiration again - and do Add again.
If you need to force clear - do Remove.
There is also the AddOrGetExisting method - it sort of combines the logic and simplifies the code.
2. To write from different threads, you need to write the implementation yourself. Here is an example - MemCache.cs
4. There are implementations of wrappers over MemoryCache, I can’t recommend anything. But they are in Google/github. Study.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question