1

Im using some cache control in my c# web-api server.

Im using the following code:

private static MemoryCache _cache = new MemoryCache("ExampleCache");

public static object GetItems(string key) {
    return AddOrGetExisting(key, () => InitItem(key));
}

private static List<T> AddOrGetExisting<T>(string key, Func<List<T>> valueFactory)
{
    var newValue = new Lazy<List<T>>(valueFactory);
    var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<List<T>>;
    try
    {
        return (oldValue ?? newValue).Value;
    }
    catch
    {
        _cache.Remove(key);
        throw;
    }       
}

private static List<string> InitItem(string key) {
    // im actually fetching a list from the db..but for the sake of the example
    return new List<string>()
}

now, everything works nicely. BUT, this time I want to update something in my db and after that, I want to update the cache control so I wont have to query my db.

Assume im using an object which looks like this

Public class Foo{
         public string Id;
         public List<string> values;
}

And assume T is Foo in this example

I need to add an item to the list which is stored in the _cache by Foo's Id field. I would insanely appericate if the process will be thread safe.

TIA.

6
  • So you can get list by passing the key in your memory cache and add item to that list...what is the problem? don't you have access to instance of memoryCache instance? Commented Feb 22, 2016 at 13:50
  • There is no threadsafe way to add an item to the list returned by _cache. Would you consider caching at a more granular level? Have each Foo be responsible for looking up and proxying to the cache or the database as required? (One possible way to solve the issue, at least) Alternately, you could use ConcurrentBag instead of list (I suggest you change the return type to IList<T> Commented Feb 22, 2016 at 13:50
  • @Viru am i getting the EXACT instance of Foo? changes on Foo are affeting the object in the memory cache? Because i thought a new pointer could be created and the changing wont affect to original object. willaien I didnt get your 2 last sentences but I dont think that there is no other threadsafe to do, i can simply use lock on every insertion like this and its safe, problem is with the bottle-neck which is created in the process.. Commented Feb 22, 2016 at 13:54
  • @willaien If the object im receving from the GetItems method is the original objet in the memory so i can implement a thread safe list.. but, is it working like this? as i was saying to Viru, i am not sure its working this way Commented Feb 22, 2016 at 13:55
  • @OriRefael List is reference type so changes you do will be affected to original object,, Commented Feb 22, 2016 at 14:00

1 Answer 1

3

You can get list by passing the key in your memory cache and add item to that list. List is reference type so changes you do will be affected to original object.

List<T> is not thread safe...Either you have to go about using lock mechanism or you can use ConcurrentBag<T> if order of elements are not important..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.