Last active
July 11, 2026 18:40
-
-
Save hoyhoy/66a0125e0017e99035fe69441e68ba97 to your computer and use it in GitHub Desktop.
Memoized Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cerrno> // Provides ENOENT | |
| #include <mutex> | |
| #include <shared_mutex> | |
| #include <string> | |
| #include <unordered_map> | |
| namespace CodingExamples { | |
| class Cache { | |
| private: | |
| std::unordered_map<std::string, std::string> _cache; | |
| mutable std::shared_mutex _mutex; | |
| std::string _expensive_computation(const std::string& key) { | |
| return "computed_" + key; | |
| } | |
| // Refactored to pass value by reference and return an int error code | |
| int _try_read_cache(const std::string& key, std::string& value) const { | |
| std::shared_lock<std::shared_mutex> read_lock(_mutex); | |
| if (_cache.contains(key)) { | |
| value = _cache.at(key); | |
| return 0; // Success | |
| } | |
| return ENOENT; // No such file or directory / Entry not found | |
| } | |
| // Refactored to return the cached entry by const reference | |
| const std::string& _set_write_cache(const std::string& key) { | |
| std::unique_lock<std::shared_mutex> write_lock(_mutex); | |
| // Double-check under the exclusive lock | |
| if (!_cache.contains(key)) { | |
| _cache[key] = _expensive_computation(key); | |
| } | |
| return _cache.at(key); // Use .at() to safely return a reference | |
| } | |
| public: | |
| std::string get_or_compute_correct(const std::string& key) { | |
| std::string cached_val; | |
| // 1. Fast Path: If it returns 0 (success), we use the populated cached_val | |
| if (_try_read_cache(key, cached_val) == 0) { | |
| return cached_val; | |
| } | |
| // 2. Slow Path: Compute, store, and return by reference | |
| return _set_write_cache(key); | |
| } | |
| }; | |
| } // namespace CodingExamples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment