Giter Site home page Giter Site logo

Comments (6)

jornj avatar jornj commented on May 30, 2024

I assume you could do something like

oProductService.UpdateProduct(newValue);
var products = oProductService.GetProducts();
cache.Add("latest-products", products);

from lazycache.

alastairtree avatar alastairtree commented on May 30, 2024

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.

simba22042 avatar simba22042 commented on May 30, 2024

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.

simba22042 avatar simba22042 commented on May 30, 2024

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.

alastairtree avatar alastairtree commented on May 30, 2024

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.

jornj avatar jornj commented on May 30, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.