* A CacheComponent that uses Cachelib's LruAllocator RAM cache without the * flash/NVM cache. * * Although all APIs use coroutines (according to the CacheComponent interface), * they are synchronous under the hood. */
| 43 | * they are synchronous under the hood. |
| 44 | */ |
| 45 | class RAMCacheComponent : public CacheComponentWithStats { |
| 46 | public: |
| 47 | using LatencySamplingConfig = |
| 48 | CacheComponentStatsCollector::LatencySamplingConfig; |
| 49 | |
| 50 | /** |
| 51 | * Configuration for cache persistence. If provided, the cache will use shared |
| 52 | * memory so that state can be persisted across shutdown/recovery cycles. |
| 53 | */ |
| 54 | class PersistenceConfig : public utils::PersistenceConfigBase { |
| 55 | public: |
| 56 | static PersistenceConfig noPersistenceOrRecovery(); |
| 57 | static PersistenceConfig persistenceAndRecovery(std::string cacheDir, |
| 58 | void* baseAddr = nullptr); |
| 59 | static PersistenceConfig persistenceButNoRecovery(std::string cacheDir, |
| 60 | void* baseAddr = nullptr); |
| 61 | |
| 62 | // Note: make return val non-const so we can move cacheDir out |
| 63 | FOLLY_ALWAYS_INLINE std::string& cacheDir() const noexcept { |
| 64 | return cacheDir_; |
| 65 | } |
| 66 | FOLLY_ALWAYS_INLINE void* baseAddr() const noexcept { return baseAddr_; } |
| 67 | |
| 68 | private: |
| 69 | mutable std::string cacheDir_; |
| 70 | void* baseAddr_; |
| 71 | |
| 72 | PersistenceConfig(bool persist, |
| 73 | bool recover, |
| 74 | std::string&& cacheDir, |
| 75 | void* baseAddr) |
| 76 | : PersistenceConfigBase(persist, recover), |
| 77 | cacheDir_(std::move(cacheDir)), |
| 78 | baseAddr_(baseAddr) {} |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * Pool configuration. RAMCacheComponent includes only 1 pool - to add more |
| 83 | * pools, create more RAMCacheComponents. |
| 84 | */ |
| 85 | struct PoolConfig { |
| 86 | folly::StringPiece name_{"default"}; |
| 87 | size_t size_; |
| 88 | const std::set<uint32_t> allocSizes_; |
| 89 | MMLru::Config mmConfig_; |
| 90 | std::shared_ptr<RebalanceStrategy> rebalanceStrategy_{nullptr}; |
| 91 | std::shared_ptr<RebalanceStrategy> resizeStrategy_{nullptr}; |
| 92 | bool ensureProvisionable_{false}; |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Factory method to create a new RAMCacheComponent. Validates the config |
| 97 | * before actually creating the cache. Also creates a default pool used for |
| 98 | * all caching in this component. |
| 99 | * |
| 100 | * Note: NVM caching or pool rebalancing *must not* be enabled in the config. |
| 101 | * Returns an error if either are enabled in the config. |
| 102 | * |