Repository navigation
CCache v0.0.4
Here's the updated changelog reflecting the addition of std::optional support:
Changelog:
-
Added
clearmethod:- A new method
clear()was added to theCCacheclass to remove all entries from the cache. It clears bothcache_map_andaccess_order_and logs the action if a logger is provided.
- A new method
-
Updated cache return types to
std::optional<Value>:- The
getmethod now returnsstd::optional<Value>instead of justValue. This change allows the method to return an empty state (i.e.,std::nullopt) when the key is not found or the cache entry has expired. - The
with_cachemethod also returnsstd::optional<Value>, making it consistent with the cache retrieval logic and enabling proper handling of cache misses.
- The
Example Usage of clear method:
CCache<int, std::string> cache;
cache.put(1, "value1");
cache.put(2, "value2");
// Clear all entries from the cache
cache.clear();Example of std::optional<Value> with get and with_cache:
auto value = cache.get(1);
if (value) {
// Value is found
std::cout << "Found: " << *value << std::endl;
} else {
// Value is not found or expired
std::cout << "Not found or expired" << std::endl;
}
auto computed_value = cache.with_cache(3, []() { return "computed_value"; });
std::cout << "Computed or fetched: " << computed_value.value() << std::endl;These changes introduce better error handling and caching behaviors through the use of std::optional.