Skip to content

CCache

Latest

Choose a tag to compare

@GtechGovind GtechGovind released this 23 Nov 05:57
New Features and Improvements:

Detailed Comments:

Added comprehensive comments throughout the class to describe the functionality of each method, parameter, and return type. This enhances code readability and maintainability.
Eviction Callback:

Added an optional EvictionCallback that is invoked when a cache entry is evicted. This callback provides flexibility to handle evictions, such as logging or additional actions when an entry is removed.
Logger Callback:

Introduced an optional LoggerCallback to log cache operations like GET, PUT, UPDATE, EVICT, and CLEAR. This feature allows integration with logging systems and can be used for debugging or monitoring purposes.
Improved Cache Entry Eviction:

The eviction strategy now follows the Least Recently Used (LRU) approach. The evict_lru method removes the least recently accessed entry when the cache is full, ensuring optimal space usage.
The evict method explicitly evicts an entry by key and invokes the eviction callback if provided.
Enhanced Thread Safety:

The class now uses a shared mutex (std::shared_mutex) to allow concurrent reads while ensuring thread safety during writes. This is ideal for scenarios with frequent reads and occasional writes.
Cache Hit/Miss Logging:

Added logging for cache hits (HIT) and misses (MISS) in the with_cache method, improving transparency and enabling better monitoring of cache performance.
Improved Function Names and Documentation:

The function names and comments have been refined for clarity and professionalism.
The method key_to_string now checks if the key is convertible to a std::string and provides a string representation for logging purposes, supporting both string and non-string key types.
Validation of Parameters in Constructor:

The constructor validates the max_size_ and ttl_millis_ parameters, throwing an std::invalid_argument exception if they are non-positive. This ensures the cache is created with valid configuration values.
Optional Cache Retrieval (with_cache):

The with_cache method provides a mechanism to fetch a value from the cache or compute and insert it if not present. This allows for lazy computation while ensuring the result is cached for future use.
Improved Access Order Management:

The update_access_order method ensures the cache maintains accurate access order for LRU eviction, pushing accessed keys to the back of the order list to mark them as most recently used.
Cache Entry Structure:

The CacheEntry struct now encapsulates both the value and its timestamp, making it easier to manage cache entries and check for expiration.
Bug Fixes:

None identified in this release.
Backward Compatibility:

This update maintains backward compatibility with previous versions of the class, although the new logging and eviction callback features provide enhanced functionality.