charged_cache.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <string>
  7. #include "port/port.h"
  8. #include "rocksdb/advanced_cache.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. class ConcurrentCacheReservationManager;
  11. // A cache interface which wraps around another cache and takes care of
  12. // reserving space in block cache towards a single global memory limit, and
  13. // forwards all the calls to the underlying cache.
  14. class ChargedCache : public CacheWrapper {
  15. public:
  16. ChargedCache(std::shared_ptr<Cache> cache,
  17. std::shared_ptr<Cache> block_cache);
  18. Status Insert(
  19. const Slice& key, ObjectPtr obj, const CacheItemHelper* helper,
  20. size_t charge, Handle** handle = nullptr,
  21. Priority priority = Priority::LOW, const Slice& compressed_val = Slice(),
  22. CompressionType type = CompressionType::kNoCompression) override;
  23. Cache::Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
  24. CreateContext* create_context,
  25. Priority priority = Priority::LOW,
  26. Statistics* stats = nullptr) override;
  27. void WaitAll(AsyncLookupHandle* async_handles, size_t count) override;
  28. bool Release(Cache::Handle* handle, bool useful,
  29. bool erase_if_last_ref = false) override;
  30. bool Release(Cache::Handle* handle, bool erase_if_last_ref = false) override;
  31. void Erase(const Slice& key) override;
  32. void EraseUnRefEntries() override;
  33. static const char* kClassName() { return "ChargedCache"; }
  34. const char* Name() const override { return kClassName(); }
  35. void SetCapacity(size_t capacity) override;
  36. inline Cache* GetCache() const { return target_.get(); }
  37. inline ConcurrentCacheReservationManager* TEST_GetCacheReservationManager()
  38. const {
  39. return cache_res_mgr_.get();
  40. }
  41. private:
  42. std::shared_ptr<ConcurrentCacheReservationManager> cache_res_mgr_;
  43. };
  44. } // namespace ROCKSDB_NAMESPACE