Skip to main content

RequestCache

Struct RequestCache 

Source
pub struct RequestCache { /* private fields */ }
Expand description

A type-keyed store scoped to one GraphQL request.

At most one value of each Rust type is stored. Insertion returns an Arc, so cloning a value out of the cache (or handing it to several resolvers) does not clone the value itself.

RequestCache is cheap to clone: every clone refers to the same store, which means passing one into the execution context and one into a resolver is the same cache. A new request must call RequestCache::new to get an isolated store.

Reads take a shared lock and writes take an exclusive one, so resolvers running on several worker threads can share the cache without copying it. Contention is normally limited to the first access for each type, which is where the value is built.

use gurafi::RequestCache;

let cache = RequestCache::new();

let first = cache.get_or_insert_with(|| String::from("shared"));
let second = cache.get_or_insert_with(|| String::from("unused"));

assert!(std::sync::Arc::ptr_eq(&first, &second));
assert_eq!(&*first, "shared");

Implementations§

Source§

impl RequestCache

Source

pub fn new() -> Self

Create an empty request cache.

Create exactly one of these per GraphQL request. Sharing a cache between requests leaks values across them; that is sometimes desirable for immutable metadata, but it is not what this type promises.

Source

pub fn get<T>(&self) -> Option<Arc<T>>
where T: Any + Send + Sync,

Borrow a stored value, if one of type T has been inserted.

The returned Arc keeps the value alive even if the entry is simultaneously removed from the cache.

Source

pub fn get_or_insert_with<T>(&self, init: impl FnOnce() -> T) -> Arc<T> ⓘ
where T: Any + Send + Sync,

Return the stored value of type T, building it with init if it is absent.

When several threads race for the same missing type exactly one calls init; every caller receives the same Arc. The common case for a loader is therefore one construction per request, not one per resolver.

init runs while the cache is write-locked. It must not call back into this cache (that would deadlock) and should not block; construct the value and return it.

Source

pub fn insert<T>(&self, value: T) -> Option<Arc<T>>
where T: Any + Send + Sync,

Store value, returning the value it replaced.

Handles already given out for the previous value stay valid; only future lookups observe the replacement.

Source

pub fn remove<T>(&self) -> Option<Arc<T>>
where T: Any + Send + Sync,

Remove and return the stored value of type T.

Source

pub fn contains<T>(&self) -> bool
where T: Any + Send + Sync,

Whether a value of type T is currently stored.

Source

pub fn clear(&self)

Remove every stored value.

Existing Arc handles stay valid; only future lookups see the cache as empty.

Source

pub fn len(&self) -> usize

The number of distinct types currently stored.

Source

pub fn is_empty(&self) -> bool

Whether the cache holds no values.

Trait Implementations§

Source§

impl Clone for RequestCache

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RequestCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.