Comments (6)
I assume you could do something like
oProductService.UpdateProduct(newValue);
var products = oProductService.GetProducts();
cache.Add("latest-products", products);
from lazycache.
As @jornj said, just Add(...)
the updated list to the cache to ensure it is cached. See this section of the docs for more info.
from lazycache.
Sorry guys I still not get an item in a cached list to update.
So basically I have cached a list of objects using a key but now I am trying to update a particular object in a list and somehow object is not getting updated.
So oProductService.GetProducts() has an item in the list with an Id of 278E4538-0F40-4220-9F05-3AFCD532BE01 but once in cache I want to update it at some point. I have tried using the code below but does not seem to update the product with id 278E4538-0F40-4220-9F05-3AFCD532BE01.
Here is what I have.
var policy = new CacheItemPolicy()
{
Priority = CacheItemPriority.NotRemovable,
AbsoluteExpiration = DateTime.UtcNow.AddSeconds(5000),
RemovedCallback = OnCacheExpired,
};
cache.GetOrAdd("latest-products", () => oProductService.GetProducts(), policy);
var items = cache.Get<List<Products>>("latest-products");
Products product = new Products { Id = new Guid("278E4538-0F40-4220-9F05-3AFCD532BE01"),
Name = "Update Quqntity", Quantity = 10 };
cache.GetOrAdd("latest-products", () => product);
from lazycache.
I will be using SQL Dependency to get an update on an item but then do not want to call GetProducts again just want to update a particular item in the list that is because GetProducts is very slow and expensive operation.
from lazycache.
Have you tried recreating the list of products when it needs updating by creating a new list based on all the old items except the one updated item plus the new version of the updated item, and then calling Add(..)
to put that into the cache? GetOrAdd() wil never add to the cache if the key is already cached.
from lazycache.
@simba22042 , as @alastairtree mentioned, GetOrAdd will not add if it already exists. And in your code example, you do just that. Try replacing the last cache.GetOrAdd with a cache.Add.
from lazycache.
Related Issues (20)
- How to Proactively Repopulate Cached Items with EF? HOT 9
- Get doesn't call function delegate added with Add HOT 4
- Named binding error after upgrading ninject package HOT 1
- LazyCache.TryGetValue appears to return null value HOT 17
- Create a sectioned IAppCache
- how do I clean the cache items by wildcard of a key HOT 2
- Add TryGetValueAsync that evaluates GetValueFromAsyncLazy HOT 1
- Cache eviction policy when cache is full? HOT 1
- Absolute expiration does not work v2.4.0 HOT 1
- Implement MemoryCache.Trim() to allow explicit removal
- LongRunning shouldn't run more than once HOT 1
- About Cache availability mechanism HOT 2
- CachingService.TryGetValue returning wrong value HOT 2
- Cannot dispose of cache HOT 3
- How to create separate caches? Not seeing how this is done. HOT 1
- Do you have plans to upgrade to dotnet7 HOT 5
- Question/Proposal on the limit of Cache keying to strings vs. "TKey"
- Is it possible to have some kind of management console to show the list of keys and their expiry?
- Bug in Wiki: Why does the Cancellation Tokens example use two CancellationTokenSource?
- Singleton constraint conflict if IMemoryCache already exists
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 lazycache.