memorycache

Memory Cache in dotnet core

风格不统一 提交于 2019-12-04 03:08:43
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 { SlidingExpiration = TimeSpan.FromMinutes(15), RemovedCallback = x => { object o; _theLock.TryRemove(x

.net MemoryCache - notify on item removed

南笙酒味 提交于 2019-12-04 02:30:58
I'm using a .net Memory Cache with .NET 4.0 and c#, I want my application to be notified when an item is removed (so I can write that it has been removed to a log file or notify the UI, that the item is removed). Is there anyway to do this. I'm using System.Runtime.Caching.MemoryCache not System.Web.Caching EDIT : If you're using the System.Runtime.Caching.MemoryCache there is a callback on the CacheItemPolicy object for deletion, as well as one for update. myMemoryCache.Set("key", null, new CacheItemPolicy() {RemovedCallback = new CacheEntryRemovedCallback(CacheRemovedCallback) /* your other

Asp.Net Core: Use memory cache outside controller

拥有回忆 提交于 2019-12-03 20:05:27
问题 In ASP.NET Core its very easy to access your memory cache from a controller In your startup you add: public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); } and then from your controller [Route("api/[controller]")] public class MyExampleController : Controller { private IMemoryCache _cache; public MyExampleController(IMemoryCache memoryCache) { _cache = memoryCache; } [HttpGet("{id}", Name = "DoStuff")] public string Get(string id) { var cacheEntryOptions =

.Net Core MemoryCache PostEvictionCallback not working properly

匆匆过客 提交于 2019-12-01 04:33:35
I have set cache items with sliding expiration in a Microsoft.Extensions.Caching.Memory.MemoryCache. I want to trigger a callback everytime a cache item expires, but callback isn't triggered until I query the cache for the expired cache item. Here is the code: using System; using Microsoft.Extensions.Caching.Memory; namespace Memcache { public class Program { private static MemoryCache _cache; private static int _cacheExpSecs; public static void Main(string[] args) { _cache = new MemoryCache(new MemoryCacheOptions()); _cacheExpSecs = 2; var cacheEntryOptions = new MemoryCacheEntryOptions()

How to implement ListView caching in Android

我只是一个虾纸丫 提交于 2019-12-01 03:46:51
问题 I have a ListView which contains a large set of data. At the first time, I load all the data from a Webservice. Now I want to cache that data so that, if I'm to open that page again, I can fetch the data from the cache instead of querying the webservice again. How do I do that?. 回答1: I assume you're storing the data retrieved from WebService in a serializable object (as you stated in your question before you edited it.) You can store serializable objects into a file and load them later: Store

.Net Core MemoryCache PostEvictionCallback not working properly

让人想犯罪 __ 提交于 2019-12-01 01:14:13
问题 I have set cache items with sliding expiration in a Microsoft.Extensions.Caching.Memory.MemoryCache. I want to trigger a callback everytime a cache item expires, but callback isn't triggered until I query the cache for the expired cache item. Here is the code: using System; using Microsoft.Extensions.Caching.Memory; namespace Memcache { public class Program { private static MemoryCache _cache; private static int _cacheExpSecs; public static void Main(string[] args) { _cache = new MemoryCache

How to implement ListView caching in Android

本小妞迷上赌 提交于 2019-11-30 21:04:41
I have a ListView which contains a large set of data. At the first time, I load all the data from a Webservice. Now I want to cache that data so that, if I'm to open that page again, I can fetch the data from the cache instead of querying the webservice again. How do I do that?. I assume you're storing the data retrieved from WebService in a serializable object (as you stated in your question before you edited it.) You can store serializable objects into a file and load them later: Store: FileOutputStream fileOutputStream = yourContext.openFileOutput(fileName, Context.MODE_PRIVATE);

How can I detach the object reference on MemoryCache

大兔子大兔子 提交于 2019-11-30 18:25:33
I'm currently trying out the new MemoryCache in .Net 4 to cache a few bits of data in one of our apps. The trouble I have is the objects are updated and the cache appears to be persisting the changes e.g. public IEnumerable<SomeObject> GetFromDatabase(){ const string _cacheKeyGetDisplayTree = "SomeKey"; ObjectCache _cache = MemoryCache.Default; var objectInCache = _cache.Get(_cacheKeyGetDisplayTree) as IEnumerable<SomeObject>; if (objectInCache != null) return objectInCache.ToList(); // Do something to get the items _cache.Add(_cacheKeyGetDisplayTree, categories, new DateTimeOffset(DateTime

MemoryCache Thread Safety, Is Locking Necessary?

瘦欲@ 提交于 2019-11-30 10:26:59
问题 For starters let me just throw it out there that I know the code below is not thread safe (correction: might be). What I am struggling with is finding an implementation that is and one that I can actually get to fail under test. I am refactoring a large WCF project right now that needs some (mostly) static data cached and its populated from a SQL database. It needs to expire and "refresh" at least once a day which is why I am using MemoryCache. I know that the code below should not be thread

Memory Cache in web api

旧街凉风 提交于 2019-11-30 09:50:55
I was looking for Caching in my web api where i can use output of one api method(that changes once in 12hrs) for subsequesnt calls and then i found this solution on SO,but i am having a difficulty in understanding and using the below code private IEnumerable<TEntity> GetFromCache<TEntity>(string key, Func<IEnumerable<TEntity>> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable<TEntity>>(valueFactory); CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) }; //The line below returns