alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Question How to update an item in a cache where a value is a list of object #27

Closed simba22042 closed 6 years ago

simba22042 commented 6 years ago

How do I go about updating a particular object in cached object with a given key.

Here is an example

cache.GetOrAdd("latest-products", () => oProductService.GetProducts(), policy);

I am populating a list of product to "latest-products" and want to update a particular product in the given list and then update the cache.

oProductService.GetProducts() returns a list of Products

public class Products { public Guid Id {get;set;} public string Name { get; set; }

     public int Quantity { get; set; }
}
jornj commented 6 years ago

I assume you could do something like

oProductService.UpdateProduct(newValue);
var products = oProductService.GetProducts();
cache.Add("latest-products", products);
alastairtree commented 6 years ago

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.

simba22042 commented 6 years ago

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);
simba22042 commented 6 years ago

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.

alastairtree commented 6 years ago

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.

jornj commented 6 years ago

@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.