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;
}