1

I have a question about cache manager (http://cachemanager.michaco.net/), it works great but I am in dire need of get all values from this as for now I keep all keys in separate collection and just iterate them and get values from cache that way:

private readonly List<string> keys;

public void AddToCache(string key, string[] record)
{
    this.keys.Add(key);
    this.cache.Add(key, record);
}

public void DumpFromCacheToCsv(bool isRequest)
{
    foreach (var cachedkey in keys.ToList())
    {
       if (cachedkey == null) continue;
       //do something with data
    }
}

I am aware that this is not ideal solution, as i searched that documentation I can not fond anything about this, so is there any other way to get all values from that cache mechanism.

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • 1
    Why? That's *not* how a cache is supposed to work. In fact, there's no guarantee that the item whose key you've stored can still be found in the cache. Are you looking for ways to cache a list of items as a single record perhaps? – Panagiotis Kanavos Sep 17 '19 at 10:49
  • If you want to export data to a file you probably need to *avoid* caching and just export the data. – Panagiotis Kanavos Sep 17 '19 at 10:51
  • There is no writer that can write that fast as I need, that's why I need cache – Wojciech Szabowicz Sep 17 '19 at 10:53
  • 1
    Caches speed up reading not writing. – Panagiotis Kanavos Sep 17 '19 at 11:15
  • Well yes, but I need cache to keep up with communication provider to gather all communication (at least its major values), and then get all values from this and dump it to file. As I exchange message at time interval about 1 ms per request - response, no writer can keep up. – Wojciech Szabowicz Sep 17 '19 at 11:18
  • The cache won't make a slow exchange run any faster. It may keep *some* of that data in memory but in the end it has to request all of it. To speed up the export it would have to hold all of the data or a large part of it (over 50%) in the cache. In that case though, why not use a normal collection like a Dictionary, or Queue? If you need multithreaded operations there are ConcurrentQueue, BlockingCollection, InputBuffer, Channels etc. – Panagiotis Kanavos Sep 17 '19 at 11:22
  • Perhaps you need a buffer between the data producer and the consumer that exports it instead of a cache? – Panagiotis Kanavos Sep 17 '19 at 11:26
  • Hmm, I could use F# mailbox processor to create that buffer – Wojciech Szabowicz Sep 17 '19 at 11:27
  • You don't need to. I already mentioned InputBuffer from TPL Dataflow. You can use ActionBlock to both buffer *and* process incoming messages. Lately, System.Threading.Channels added Go-like channel support. It's lower-level than an ActionBlock but allows greater flexibility. If you post what you actually try to do people will be able to prospose solutions – Panagiotis Kanavos Sep 17 '19 at 11:31
  • 1
    Check [this question for example](https://stackoverflow.com/questions/57879338/contentious-paralleled-work-between-two-collections-of-objects). The bare-bones examples show how both Dataflow and Channels deal with the pub/sub problem. They don't go into details like bounded buffering or multi-threaded processing – Panagiotis Kanavos Sep 17 '19 at 11:32
  • Aye it works, thank you – Wojciech Szabowicz Sep 17 '19 at 13:57

0 Answers0