Elegant Solution for POCO Caching
This is my second time diving into the vast ocean of Poco Cache library source code. And since this is my second time studying it, I actually get a much better understanding and overview of how the Poco Cache really works. It works by event handling. The moment there is a need for the elements to be removed, it will fire an event to notify the strategy on which elements it would like to remove, and it will then pass back these elements to be undergone actual removal operation. However, there is a drawback to this, you can’t do anything to the cache upon its expiration using Poco’s own library. Nevertheless, there’s a catch here. We can define our own strategy to do something to the parameters passing around before it goes expired.
Here comes the problem. The keys are only passed around but not the values. And even if you are trying to read the value from the key, it will make some modification to the cache data itself such as the ordering of the elements. I have been spending days and night trying to figure out how to sort this out. A lot of ideas keep streaming into my mind like trying to make a callback using rest api method(server sending request to server itself), passing down the resource memory upon initialization of the cache data(which gives rise to the problem of circular dependency), trying to call the cache data through event sender pointer(but it’s a const void pointer in which only constant functions can be used) , overloading the derived class function and etc, but none of these really work well and each has their respective problem. Finally and very fortunately, the entire problem has been solved by implenting a very simple method, that is, declare a new unordered map data memory which copies the cache data and keeps track of the cache data, and use the key from the expired cache data to get the value from this “doppelganger”. doppelganger