* A cache component that uses Cachelib's BlockCache flash cache without RAM * cache. * * Cache items are buffered in RAM according to the region config specified in * BlockCache::Config. Allocations are parceled out from clean regions in memory * and therefore may fail if there are no clean regions available. Similarly, * reads load the region hosting the cache item into RAM. * * All APIs
| 42 | * All APIs are cancellable. |
| 43 | */ |
| 44 | class FlashCacheComponent : public CacheComponentWithStats { |
| 45 | public: |
| 46 | class PersistenceConfig : public utils::PersistenceConfigBase { |
| 47 | public: |
| 48 | static PersistenceConfig noPersistenceOrRecovery(); |
| 49 | static PersistenceConfig persistenceAndRecovery(size_t metadataSize); |
| 50 | static PersistenceConfig persistenceButNoRecovery(size_t metadataSize); |
| 51 | |
| 52 | FOLLY_ALWAYS_INLINE size_t metadataSize() const noexcept { |
| 53 | return metadataSize_; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | // Space reserved on the device for persistence and recovery. A good default |
| 58 | // is 0.5% of the cache size. |
| 59 | size_t metadataSize_; |
| 60 | |
| 61 | PersistenceConfig(bool persist, bool recover, size_t metadataSize) |
| 62 | : PersistenceConfigBase(persist, recover), |
| 63 | metadataSize_(metadataSize) {} |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Factory method to create a new FlashCacheComponent, i.e., wrapper around |
| 68 | * BlockCache. Validates the config before actually creating the cache. |
| 69 | * |
| 70 | * Note: don't set config.device, config.checkExpired or |
| 71 | * config.cacheBaseOffset - create() will set them internally |
| 72 | * |
| 73 | * @param name name of the flash cache |
| 74 | * @param config the BlockCache::Config to use for the cache |
| 75 | * @param device the device to use for the cache |
| 76 | * @param executorConfig the config for the fiber worker executor |
| 77 | * @param persistenceConfig persistence/recovery configuration |
| 78 | * @return FlashCacheComponent if the flash cache could be initialized, an |
| 79 | * error otherwise. |
| 80 | */ |
| 81 | static Result<FlashCacheComponent> create( |
| 82 | std::string name, |
| 83 | navy::BlockCache::Config&& config, |
| 84 | std::unique_ptr<navy::Device> device, |
| 85 | const utils::CoroFiberAdapter::Config& executorConfig = {}, |
| 86 | PersistenceConfig persistenceConfig = |
| 87 | PersistenceConfig::noPersistenceOrRecovery()) noexcept; |
| 88 | |
| 89 | // ------------------------------ Interface ------------------------------ // |
| 90 | |
| 91 | const std::string& getName() const noexcept override; |
| 92 | folly::coro::Task<Result<AllocatedHandle>> allocate( |
| 93 | Key key, uint32_t size, uint32_t creationTime, uint32_t ttlSecs) override; |
| 94 | folly::coro::Task<UnitResult> insert(AllocatedHandle&& handle) override; |
| 95 | folly::coro::Task<Result<std::optional<AllocatedHandle>>> insertOrReplace( |
| 96 | AllocatedHandle&& handle) override; |
| 97 | folly::coro::Task<Result<std::optional<ReadHandle>>> find(Key key) override; |
| 98 | folly::coro::Task<Result<std::optional<WriteHandle>>> findToWrite( |
| 99 | Key key) override; |
| 100 | folly::coro::AsyncGenerator<ReadHandle> iterator() override; |
| 101 | folly::coro::Task<Result<bool>> remove(Key key) override; |