Comments (7)
You can't do new MemoryCache(new MemoryCacheOptions());
every time and expect it to contain the same data. You need to store the memory cache instance somewhere and re-use it between requests.
from caching.
Consider resolving IMemoryCache from DI
from caching.
apologies for my lack of understanding, If i have to store the memorycache instance then how does that make my access seamless? Maybe its due to my perception, I guess i was expecting it to work like System.Web.Caching.(currently using it for my mvc5 app)
I read somewhere that I may need to add services.AddCaching which gets me a copy of memorycache and distributedcache, is this still the case with the latest RC version?
in short, what I am trying to do is basically when my app starts, I want to store a collection into memory and be able to access this from any controller method.
What is the proper way to store a collection in cache and be able to use it from any controller within the mvc.
Also if you can point me to latest docs where I can read more on how this is suppose to work then maybe i will be better informed about this.
Thank you
from caching.
Even in System.Web you didn't create a new instance every time, you used the MemoryCache.Default instance. We've just moved it from the static Default to an injected service. You can find out more about injecting services over at https://github.com/aspnet/mvc
Yes, services.AddCaching is how to create the needed services.
The caching docs are still in progress. https://docs.asp.net/en/latest/fundamentals/caching.html
from caching.
You're not supposed to new anything up. You're supposed to use the instance provided by the DI container. Declare in your constructor that you need IMemoryCache and you will be handed one. Use that one
from caching.
@zoinkydoink Did using the instance provided by the DI work for you? Are you still having issues?
from caching.
Closing this as I haven't heard any problems using the instance passed by the DI. Please reopen or file another one if you're still experiencing issues.
from caching.
Related Issues (20)
- Why should I use services.AddMemoryCache(); instead of services.AddDistributedMemoryCache(); HOT 2
- Use MemoryCache Inside ConfigureServices Method HOT 4
- Fix race condition that leads to nullref when connecting to redis cache [1.1] HOT 3
- Fix race condition that leads to nullref when connecting to redis cache [1.0] HOT 3
- Fix race condition that leads to nullref when connecting to redis cache [2.0] HOT 3
- Add Redis ConfigurationOptions to RedisCacheOptions HOT 3
- In-memory cache GetOrCreate with MemoryCacheEntryOptions HOT 1
- Update redis dependency [2.1] HOT 1
- Update redis dependency [2.0] HOT 4
- Using SizeLimit with MemoryCache cause EF Core queries to fail HOT 4
- React to changes in StackExchange.Redis 2.0 HOT 35
- Add documentation XML comments on IDistributedCache and associated classes/types. HOT 1
- MemoryCache does not find item when simple object used as a key. HOT 3
- Add Clear Method HOT 1
- Enumerator and .Contains() missing; need a way to retrieve Cache Keys HOT 9
- Remove the old Redis dependency
- Test failure: DoNotAddIfSizeOverflows HOT 8
- Flushing the cache HOT 1
- RemoveAsync not working with Azure RedisCache backed IDistributedCache? HOT 2
- SQL Server deadlocks with Microsoft.Extensions.Caching.SqlServer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caching.