| 42 | |
| 43 | template <class KeyType, typename ValueType> |
| 44 | class Cache { |
| 45 | public: |
| 46 | explicit Cache(size_t capacity) : cache_(capacity) { LogCacheSize(capacity); } |
| 47 | |
| 48 | Cache() : Cache(GetCacheCapacity()) {} |
| 49 | |
| 50 | ValueType GetObjectCode(const KeyType& cache_key) { |
| 51 | std::optional<ValueType> result; |
| 52 | std::lock_guard<std::mutex> lock(mtx_); |
| 53 | result = cache_.get(cache_key); |
| 54 | return result != std::nullopt ? *result : nullptr; |
| 55 | } |
| 56 | |
| 57 | void PutObjectCode(const KeyType& cache_key, const ValueType& module) { |
| 58 | std::lock_guard<std::mutex> lock(mtx_); |
| 59 | cache_.insert(cache_key, module); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | LruCache<KeyType, ValueType> cache_; |
| 64 | std::mutex mtx_; |
| 65 | }; |
| 66 | } // namespace gandiva |