caching_ext: introduce noop-mode for cachelib and memcache caches

Summary: Sometimes we just don't want to cache at all.  Add a no-op mode so we can use the same code.

Differential Revision: D43410853

fbshipit-source-id: 20dd632df66dc85b4da18678ff56e5b83e987326
This commit is contained in:
Mark Juggurnauth-Thomas 2023-02-23 11:51:31 -08:00 committed by Facebook GitHub Bot
parent bab444d092
commit 50866019eb
4 changed files with 22 additions and 7 deletions

View File

@ -126,7 +126,7 @@ impl CachingChangesets {
#[cfg(test)]
pub fn cachelib_stats(&self) -> MockStoreStats {
match self.cachelib {
CachelibHandler::Real(_) => unimplemented!(),
CachelibHandler::Real(_) | CachelibHandler::Noop => unimplemented!(),
CachelibHandler::Mock(ref mock) => mock.stats(),
}
}
@ -134,7 +134,7 @@ impl CachingChangesets {
#[cfg(test)]
pub fn memcache_stats(&self) -> MockStoreStats {
match self.memcache {
MemcacheHandler::Real(_) => unimplemented!(),
MemcacheHandler::Real(_) | MemcacheHandler::Noop => unimplemented!(),
MemcacheHandler::Mock(ref mock) => mock.stats(),
}
}
@ -286,7 +286,7 @@ impl EntityStore<ChangesetEntryWrapper> for CacheRequest<'_> {
match mapping.memcache {
MemcacheHandler::Real(_) => true,
MemcacheHandler::Mock(..) => false,
MemcacheHandler::Mock(..) | MemcacheHandler::Noop => false,
}
}
}

View File

@ -22,6 +22,7 @@ use crate::CachelibKey;
pub enum CachelibHandler<T> {
Real(VolatileLruCachePool),
Mock(MockStore<T>),
Noop,
}
impl<T> From<VolatileLruCachePool> for CachelibHandler<T> {
@ -56,6 +57,7 @@ impl<T: Abomonation + Clone + Send + 'static> CachelibHandler<T> {
match self {
CachelibHandler::Real(ref cache) => get_cached(cache, key),
CachelibHandler::Mock(store) => Ok(store.get(key)),
CachelibHandler::Noop => Ok(None),
}
}
@ -66,6 +68,7 @@ impl<T: Abomonation + Clone + Send + 'static> CachelibHandler<T> {
store.set(key, value.clone());
Ok(true)
}
CachelibHandler::Noop => Ok(false),
}
}
@ -73,11 +76,15 @@ impl<T: Abomonation + Clone + Send + 'static> CachelibHandler<T> {
CachelibHandler::Mock(MockStore::new())
}
pub fn create_noop() -> Self {
CachelibHandler::Noop
}
#[cfg(test)]
pub(crate) fn gets_count(&self) -> usize {
use std::sync::atomic::Ordering;
match self {
CachelibHandler::Real(_) => unimplemented!(),
CachelibHandler::Real(_) | CachelibHandler::Noop => unimplemented!(),
CachelibHandler::Mock(MockStore { ref get_count, .. }) => {
get_count.load(Ordering::SeqCst)
}
@ -86,7 +93,7 @@ impl<T: Abomonation + Clone + Send + 'static> CachelibHandler<T> {
pub fn mock_store(&self) -> Option<&MockStore<T>> {
match self {
CachelibHandler::Real(_) => None,
CachelibHandler::Real(_) | CachelibHandler::Noop => None,
CachelibHandler::Mock(ref mock) => Some(mock),
}
}

View File

@ -18,6 +18,7 @@ use crate::mock_store::MockStore;
pub enum MemcacheHandler {
Real(MemcacheClient),
Mock(MockStore<Bytes>),
Noop,
}
impl From<MemcacheClient> for MemcacheHandler {
@ -33,6 +34,7 @@ impl MemcacheHandler {
client.get(key).await.map(|value| value.map(Bytes::from))
}
MemcacheHandler::Mock(store) => Ok(store.get(&key)),
MemcacheHandler::Noop => Ok(None),
}
}
@ -48,6 +50,7 @@ impl MemcacheHandler {
store.set(&key, value.into());
Ok(())
}
MemcacheHandler::Noop => Ok(()),
}
}
@ -63,6 +66,7 @@ impl MemcacheHandler {
// For now we ignore TTLs here
self.set(key, value).await
}
MemcacheHandler::Noop => Ok(()),
}
}
@ -70,11 +74,15 @@ impl MemcacheHandler {
MemcacheHandler::Mock(MockStore::new())
}
pub fn create_noop() -> Self {
MemcacheHandler::Noop
}
#[cfg(test)]
pub(crate) fn gets_count(&self) -> usize {
use std::sync::atomic::Ordering;
match self {
MemcacheHandler::Real(_) => unimplemented!(),
MemcacheHandler::Real(_) | MemcacheHandler::Noop => unimplemented!(),
MemcacheHandler::Mock(MockStore { ref get_count, .. }) => {
get_count.load(Ordering::SeqCst)
}

View File

@ -186,7 +186,7 @@ impl EntityStore<CachedChangesetEdges> for CacheRequest<'_> {
fn spawn_memcache_writes(&self) -> bool {
match self.caching_storage.memcache {
MemcacheHandler::Real(_) => true,
MemcacheHandler::Mock(..) => false,
MemcacheHandler::Mock(..) | MemcacheHandler::Noop => false,
}
}
}