0

I am using C# and .Net Core.

I am storing 3rd-party REST call results in a local memory cache. I want to pull groups of data out of the cache and return them to my client.

For example, I call a REST service for Branch information and store each result in a memory cache with the branch's unique ID as the cache key... Branch-101, Branch-400, Branch-321 for example. (note, that I have lots of other data in my cache too, not just branch info)

I now want to be able to return to a client every Branch in my cache. Does anyone have any thoughts on the best way for me to do that?

Is there a way for me to query my cache and return an array or dictionary of every Branch that is in my cache?

Edit - Details of the cache code

Here is the code that attempts to pull data from cache. If the data is not in cache, it calls a delegate to get the data. The cache is flat at this point, but if it would be better to structure it differently, I can as I am just beginning the project:

The first line sets a default value (most often null) of the type I am looking for The next line generates a key based off of the type of object I am looking for. For example, if I am looking for a Branch object, the key will be "branch-" + the unique key of the branch

Next, I attempt to get the object out of cache. If the object is not there, I call the delegate and set the cache value to the result of calling the delegate:

    // Here is the delegate
    public delegate string GetData(int id);

    // Here is the code that pulls the data out of cache or 
    // calls a delegate to get rest data
    public T CacheOrCall<T>(int id, string nodename, GetData fn)
    {
        T o = default(T);
        string key = typeof(T).ToString() + "-" + id;
        string ret = null;
        bool isCached = this.cache.TryGetValue(key, out o);
        if (!isCached)
        {
            ret = fn(id);
            o = this.Deserialize<T>(ret, nodename);
            this.cache.Set(key, o, this.cacheOptions);
        }
        return o;
    }
Tseng
  • 61,549
  • 15
  • 193
  • 205
birwin
  • 2,524
  • 2
  • 21
  • 41
  • 2
    how is that cache organized (format/data structure)? could you show the parts of your code that currently deal with storing and retrieving data to/from that cache? – Cee McSharpface Mar 27 '17 at 15:20
  • @dlatikay - Thanks for your comment. I have edited the question to include these details – birwin Mar 27 '17 at 15:31
  • Is it possible to section an IMemoryCache? For example, place all Branches somewhere as a group and then pull that section when I need all of the branches? – birwin Mar 27 '17 at 15:43
  • 1
    as long as there are not thousands of entries, why not just enumerate all keys and fetch those that have key.StartsWith("Branch-")? like the foreach in http://stackoverflow.com/a/7334092/1132334. otherwise use just one cache entry for all the branches (nesting). – Cee McSharpface Mar 27 '17 at 15:48
  • Thank You. Yes, I have contemplated that. I was hoping for something a bit more elegant and more optimized, however, if that is the only approach, I may go with that. – birwin Mar 27 '17 at 15:59
  • In the end, I went with one entry for all branches. That entry was a dictionary object whose key was a branch identifier whose value was the branch object itself. – birwin Jun 27 '18 at 20:31
  • feel free to answer your own question with details of your solution, if you think it may benefit future readers who face a similar challenge. – Cee McSharpface Jun 27 '18 at 20:33

1 Answers1

0

In the end, I went with one entry for all branches. That entry was a dictionary object whose key was a branch identifier whose value was the branch object itself.

Dictionary<int, Branch>
birwin
  • 2,524
  • 2
  • 21
  • 41