mirror of
https://github.com/neondatabase/neon.git
synced 2026-05-19 06:00:38 +00:00
## Problem Current cache doesn't support any updates from the cplane. ## Summary of changes * Added redis notifier listner. * Added cache which can be invalidated with the notifier. If the notifier is not available, it's just a normal ttl cache. * Updated cplane api. The motivation behind this organization of the data is the following: * In the Neon data model there are projects. Projects could have multiple branches and each branch could have more than one endpoint. * Also there is one special `main` branch. * Password reset works per branch. * Allowed IPs are the same for every branch in the project (except, maybe, the main one). * The main branch can be changed to the other branch. * The endpoint can be moved between branches. Every event described above requires some special processing on the porxy (or cplane) side. The idea of invalidating for the project is that whenever one of the events above is happening with the project, proxy can invalidate all entries for the entire project. This approach also requires some additional API change (returning project_id inside the auth info).
73 lines
1.7 KiB
Rust
73 lines
1.7 KiB
Rust
use std::ops::{Deref, DerefMut};
|
|
|
|
/// A generic trait which exposes types of cache's key and value,
|
|
/// as well as the notion of cache entry invalidation.
|
|
/// This is useful for [`Cached`].
|
|
pub trait Cache {
|
|
/// Entry's key.
|
|
type Key;
|
|
|
|
/// Entry's value.
|
|
type Value;
|
|
|
|
/// Used for entry invalidation.
|
|
type LookupInfo<Key>;
|
|
|
|
/// Invalidate an entry using a lookup info.
|
|
/// We don't have an empty default impl because it's error-prone.
|
|
fn invalidate(&self, _: &Self::LookupInfo<Self::Key>);
|
|
}
|
|
|
|
impl<C: Cache> Cache for &C {
|
|
type Key = C::Key;
|
|
type Value = C::Value;
|
|
type LookupInfo<Key> = C::LookupInfo<Key>;
|
|
|
|
fn invalidate(&self, info: &Self::LookupInfo<Self::Key>) {
|
|
C::invalidate(self, info)
|
|
}
|
|
}
|
|
|
|
/// Wrapper for convenient entry invalidation.
|
|
pub struct Cached<C: Cache, V = <C as Cache>::Value> {
|
|
/// Cache + lookup info.
|
|
pub token: Option<(C, C::LookupInfo<C::Key>)>,
|
|
|
|
/// The value itself.
|
|
pub value: V,
|
|
}
|
|
|
|
impl<C: Cache, V> Cached<C, V> {
|
|
/// Place any entry into this wrapper; invalidation will be a no-op.
|
|
pub fn new_uncached(value: V) -> Self {
|
|
Self { token: None, value }
|
|
}
|
|
|
|
/// Drop this entry from a cache if it's still there.
|
|
pub fn invalidate(self) -> V {
|
|
if let Some((cache, info)) = &self.token {
|
|
cache.invalidate(info);
|
|
}
|
|
self.value
|
|
}
|
|
|
|
/// Tell if this entry is actually cached.
|
|
pub fn cached(&self) -> bool {
|
|
self.token.is_some()
|
|
}
|
|
}
|
|
|
|
impl<C: Cache, V> Deref for Cached<C, V> {
|
|
type Target = V;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.value
|
|
}
|
|
}
|
|
|
|
impl<C: Cache, V> DerefMut for Cached<C, V> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.value
|
|
}
|
|
}
|