//===----------------------------------------------------------------------===// // DuckDB // // duckdb/storage/object_cache.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/common/common.hpp" #include "duckdb/common/string.hpp" #include "duckdb/common/unordered_map.hpp" #include "duckdb/common/mutex.hpp" #include "duckdb/main/client_context.hpp" #include "duckdb/main/database.hpp" namespace duckdb { class ClientContext; //! ObjectCache is the base class for objects caches in DuckDB class ObjectCacheEntry { public: virtual ~ObjectCacheEntry() { } virtual string GetObjectType() = 0; }; class ObjectCache { public: shared_ptr GetObject(const string &key) { lock_guard glock(lock); auto entry = cache.find(key); if (entry == cache.end()) { return nullptr; } return entry->second; } template shared_ptr Get(const string &key) { shared_ptr object = GetObject(key); if (!object || object->GetObjectType() != T::ObjectType()) { return nullptr; } return std::static_pointer_cast(object); } void Put(string key, shared_ptr value) { lock_guard glock(lock); cache[key] = std::move(value); } DUCKDB_API static ObjectCache &GetObjectCache(ClientContext &context); DUCKDB_API static bool ObjectCacheEnabled(ClientContext &context); private: //! Object Cache unordered_map> cache; mutex lock; }; } // namespace duckdb