Answer the question
In order to leave comments, you need to log in
Missing entries from MemoryCache?
Uses System.Runtime.Caching.MemoryCache as key-value storage. An object can be saved twice (the second time is for updating the data). Moreover, the value of the ImportantInfo field in the saved object is not passed the second time, and it has to be taken from the old entry from the cache.
public static void Save(Entity entity)
{
var cache = MemoryCache.Default;
var idKey = entity.Id.ToString();
if (cache.Contains(idKey))
{
var message = string.Format(
"In the cache already has a record with identifier {0}. Existing record will be replaced. ImportantInfo will be extracted from old entity record.",
idKey);
log.Info(message);
entity.ImportantInfo = (cache.Get(idKey) as Entity).ImportantInfo;
}
var policy = new CacheItemPolicy
{
SlidingExpiration = Options.MemoryCachingTime //30 минут.
};
cache.Set(idKey, entity, policy);
}
<system.runtime.caching>
<memoryCache>
<namedCaches>
<add name="default"
cacheMemoryLimitMegabytes="0"
physicalMemoryLimitPercentage="0"
pollingInterval="00:02:00" />
</namedCaches>
</memoryCache>
</system.runtime.caching>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question