memorycache

How to use MemoryCache insted of Timer to trigger a method?

旧巷老猫 提交于 2019-12-10 21:45:19
问题 The following method handle concurrent request by waiting the result of an already running operation. Requests for data may come in simultaneously with same/different credentials. For each unique set of credentials there can be at most one GetCurrentInternal call in progress, with the result from that one call returned to all queued waiters when it is ready. private readonly ConcurrentDictionary<Credentials, Lazy<Data>> _dataToCredentialMap = new ConcurrentDictionary<Credentials, Lazy<Data>>(

MemoryCache UpdateCallback not working

Deadly 提交于 2019-12-10 14:50:11
问题 I'm trying to create a pool of connections to a third-party API, and have connections expire after an interval if they are not in use. When they expire, they need to be disconnected via the third-party API. It appeared that MemoryCache (System.Runtime.Caching) would handle this. UpdateCallback seems to behave oddly, though. A simple LINQPad example: void Main() { var cache = MemoryCache.Default; var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(1);

How can I use both DiskLruCache(DiskCache) and LruCache(MemoryCache)?

左心房为你撑大大i 提交于 2019-12-10 11:59:35
问题 I'm developing Image Loader using both cache. and here is my logic. 1st, start get the images on the disk cache (when app starting) 2nd, check the memory cache. if it is, load from Cache memory. 3nd, If the image doesn't exist in memory cache, check the Disk cache. then put the image into the memorycache. 4th, If there isn't the image both memory cache and disk cache, make the bitmap from url using the asynctask. then put the Disk cache. and This is my source. 1st, App's onCreate() protected

Self-renewing MemoryCache

拈花ヽ惹草 提交于 2019-12-10 10:52:10
问题 I have implemented a cache that renews the containing values when they expire. My code is this: class MyCache { private static readonly MemoryCache Cache = MemoryCache.Default; private CacheItemPolicy _errorpolicy; private CacheItemPolicy _warnPolicy; private CacheEntryRemovedCallback _warnCacheEntryRemovedCallback; private CacheEntryRemovedCallback _errorCacheEntryRemovedCallback; public void AddErrors(string key, object errors) { _errorCacheEntryRemovedCallback = ErrorItemRemovedCallback;

Disposing MemoryCache in Finalizer throws AccessViolationException

六月ゝ 毕业季﹏ 提交于 2019-12-10 03:36:38
问题 EDIT See edit note at the bottom of the question for additional detail. Original question I have a CacheWrapper class which creates and holds onto an instance of the .NET MemoryCache class internally. MemoryCache hooks itself into AppDomain events, so it will never be garbage-collected unless it is explicitly disposed. You can verify this with the following code: Func<bool, WeakReference> create = disposed => { var cache = new MemoryCache("my cache"); if (disposed) { cache.Dispose(); } return

Load EhCache diskstore content into memory

半世苍凉 提交于 2019-12-09 16:11:26
问题 As said in EhCache documentation: In practice this means that persistent in-memory cache will start up with all of its elements on disk . [...] So, the Ehcache design does not load them all into memory on start up, but lazily loads them as required. I would like that the memory cache start up will all its elements in memory, how can I achieve that? The reason for this is that our website performs a lot of access to the cache, so the first time we visit the website it has very bad response

MemoryCache always returns “null” after first expiration

瘦欲@ 提交于 2019-12-07 07:51:33
问题 I have the following problem, I have this kind of cache management: public class CacheManager : ICacheManager { private readonly ObjectCache cache; public CacheManager() { this.cache = MemoryCache.Default; } public void AddItem(string key, object itemToCache, double duration) { var end = DateTime.Now.AddSeconds(duration); this.cache.Add(key, itemToCache, end); } public T Get<T>(string key) where T : class { try { return (T)this.cache[key]; } catch (Exception ex) { return null; } } public void

Memory Cache in dotnet core

蓝咒 提交于 2019-12-05 20:11:56
问题 I am trying to write a class to handle Memory cache in a .net core class library. If I use not the core then I could write using System.Runtime.Caching; using System.Collections.Concurrent; namespace n{ public class MyCache { readonly MemoryCache _cache; readonly Func<CacheItemPolicy> _cachePolicy; static readonly ConcurrentDictionary<string, object> _theLock = new ConcurrentDictionary<string, object>(); public MyCache(){ _cache = MemoryCache.Default; _cachePolicy = () => new CacheItemPolicy

MemoryCache always returns “null” after first expiration

大兔子大兔子 提交于 2019-12-05 17:06:11
I have the following problem, I have this kind of cache management: public class CacheManager : ICacheManager { private readonly ObjectCache cache; public CacheManager() { this.cache = MemoryCache.Default; } public void AddItem(string key, object itemToCache, double duration) { var end = DateTime.Now.AddSeconds(duration); this.cache.Add(key, itemToCache, end); } public T Get<T>(string key) where T : class { try { return (T)this.cache[key]; } catch (Exception ex) { return null; } } public void Remove(string key) { this.cache.Remove(key); } } is quite simple. Edit I (bug fixes in the text of the

Disposing MemoryCache in Finalizer throws AccessViolationException

混江龙づ霸主 提交于 2019-12-05 03:01:34
EDIT See edit note at the bottom of the question for additional detail. Original question I have a CacheWrapper class which creates and holds onto an instance of the .NET MemoryCache class internally. MemoryCache hooks itself into AppDomain events, so it will never be garbage-collected unless it is explicitly disposed. You can verify this with the following code: Func<bool, WeakReference> create = disposed => { var cache = new MemoryCache("my cache"); if (disposed) { cache.Dispose(); } return new WeakReference(cache); }; // with false, we loop forever. With true, we exit var weakCache = create