pingora_cache

Struct HttpCache

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

The state machine for http caching

This object is used to handle the state and transitions for HTTP caching through the life of a request.

Implementations§

Source§

impl HttpCache

Source

pub fn new() -> Self

Create a new HttpCache.

Caching is not enabled by default.

Source

pub fn enabled(&self) -> bool

Whether the cache is enabled

Source

pub fn bypassing(&self) -> bool

Whether the cache is being bypassed

Source

pub fn phase(&self) -> CachePhase

Return the CachePhase

Source

pub fn upstream_used(&self) -> bool

Whether anything was fetched from the upstream

This essentially checks all possible CachePhase who need to contact the upstream server

Source

pub fn storage_type_is<T: 'static>(&self) -> bool

Check whether the backend storage is the type T.

Source

pub fn disable(&mut self, reason: NoCacheReason)

Disable caching

Source

pub fn bypass(&mut self)

Set the cache to bypass

§Panic

This call is only allowed in CachePhase::CacheKey phase (before any cache lookup is performed). Use it in any other phase will lead to panic.

Source

pub fn enable( &mut self, storage: &'static (dyn Storage + Sync), eviction: Option<&'static (dyn EvictionManager + Sync)>, predictor: Option<&'static (dyn CacheablePredictor + Sync)>, cache_lock: Option<&'static CacheLock>, )

Enable the cache

  • storage: the cache storage backend that implements storage::Storage
  • eviction: optionally the eviction manager, without it, nothing will be evicted from the storage
  • predictor: optionally a cache predictor. The cache predictor predicts whether something is likely to be cacheable or not. This is useful because the proxy can apply different types of optimization to cacheable and uncacheable requests.
  • cache_lock: optionally a cache lock which handles concurrent lookups to the same asset. Without it such lookups will all be allowed to fetch the asset independently.
Source

pub fn enable_tracing(&mut self, parent_span: Span)

Source

pub fn get_miss_span(&mut self) -> Option<SpanHandle>

Source

pub fn set_cache_key(&mut self, key: CacheKey)

Set the cache key

§Panic

Cache key is only allowed to be set in its own phase. Set it in other phases will cause panic.

Source

pub fn cache_key(&self) -> &CacheKey

Return the cache key used for asset lookup

§Panic

Can only be called after the cache key is set and the cache is not disabled. Panic otherwise.

Source

pub fn max_file_size_bytes(&self) -> Option<usize>

Return the max size allowed to be cached.

Source

pub fn set_max_file_size_bytes(&mut self, max_file_size_bytes: usize)

Set the maximum response body size in bytes that will be admitted to the cache.

Response header size does not contribute to the max file size.

Source

pub fn cache_found( &mut self, meta: CacheMeta, hit_handler: HitHandler, hit_status: HitStatus, )

Set that cache is found in cache storage.

This function is called after Self::cache_lookup() which returns the CacheMeta and HitHandler.

The hit_status enum allows the caller to force expire assets.

Source

pub fn cache_miss(&mut self)

Mark self to be cache miss.

This function is called after Self::cache_lookup() finds nothing or the caller decides not to use the assets found.

§Panic

Panic in other phases.

Source

pub fn hit_handler(&mut self) -> &mut HitHandler

Return the HitHandler

§Panic

Call this after Self::cache_found(), panic in other phases.

Source

pub fn miss_body_reader(&mut self) -> Option<&mut HitHandler>

Return the body reader during a cache admission (miss/expired) which decouples the downstream read and upstream cache write

Source

pub async fn finish_hit_handler(&mut self) -> Result<()>

Call this when cache hit is fully read.

This call will release resource if any and log the timing in tracing if set.

§Panic

Panic in phases where there is no cache hit.

Source

pub async fn set_miss_handler(&mut self) -> Result<()>

Set the MissHandler according to cache_key and meta, can only call once

Source

pub fn miss_handler(&mut self) -> Option<&mut MissHandler>

Return the MissHandler to write the response body to cache.

None: the handler has not been set or already finished

Source

pub async fn finish_miss_handler(&mut self) -> Result<()>

Finish cache admission

If self is dropped without calling this, the cache admission is considered incomplete and should be cleaned up.

This call will also trigger eviction if set.

Source

pub fn set_cache_meta(&mut self, meta: CacheMeta)

Set the CacheMeta of the cache

Source

pub async fn revalidate_cache_meta(&mut self, meta: CacheMeta) -> Result<bool>

Set the CacheMeta of the cache after revalidation.

Certain info such as the original cache admission time will be preserved. Others will be replaced by the input meta.

Source

pub fn revalidate_merge_header(&mut self, resp: &RespHeader) -> ResponseHeader

After a successful revalidation, update certain headers for the cached asset such as Etag with the fresh response header resp.

Source

pub fn revalidate_uncacheable( &mut self, header: ResponseHeader, reason: NoCacheReason, )

Mark this asset uncacheable after revalidation

Source

pub fn set_stale_updating(&mut self)

Mark this asset as stale, but being updated separately from this request.

Source

pub fn update_variance(&mut self, variance: Option<HashBinary>)

Update the variance of the CacheMeta.

Note that this process may change the lookup key, and eventually (when the asset is written to storage) invalidate other cached variants under the same primary key as the current asset.

Source

pub fn cache_meta(&self) -> &CacheMeta

Return the CacheMeta of this asset

§Panic

Panic in phases which has no cache meta.

Source

pub fn maybe_cache_meta(&self) -> Option<&CacheMeta>

Return the CacheMeta of this asset if any

Different from Self::cache_meta(), this function is allowed to be called in CachePhase::Miss phase where the cache meta maybe set.

§Panic

Panic in phases that shouldn’t have cache meta.

Source

pub async fn cache_lookup(&mut self) -> Result<Option<(CacheMeta, HitHandler)>>

Perform the cache lookup from the given cache storage with the given cache key

A cache hit will return CacheMeta which contains the header and meta info about the cache as well as a HitHandler to read the cache hit body.

§Panic

Panic in other phases.

Source

pub fn cache_vary_lookup( &mut self, variance: HashBinary, meta: &CacheMeta, ) -> bool

Update variance and see if the meta matches the current variance

cache_lookup() -> compute vary hash -> cache_vary_lookup() This function allows callers to compute vary based on the initial cache hit. meta should be the ones returned from the initial cache_lookup()

  • return true if the meta is the variance.
  • return false if the current meta doesn’t match the variance, need to cache_lookup() again
Source

pub fn is_cache_locked(&self) -> bool

Whether this request is behind a cache lock in order to wait for another request to read the asset.

Source

pub fn is_cache_lock_writer(&self) -> bool

Whether this request is the leader request to fetch the assets for itself and other requests behind the cache lock.

Source

pub fn take_write_lock(&mut self) -> WritePermit

Take the write lock from this request to transfer it to another one.

§Panic

Call is_cache_lock_writer() to check first, will panic otherwise.

Source

pub fn set_write_lock(&mut self, write_lock: WritePermit)

Set the write lock, which is usually transferred from Self::take_write_lock()

Source

pub fn can_serve_stale_error(&self) -> bool

Whether this asset is staled and stale if error is allowed

Source

pub fn can_serve_stale_updating(&self) -> bool

Whether this asset is staled and stale while revalidate is allowed.

Source

pub async fn cache_lock_wait(&mut self) -> LockStatus

Wait for the cache read lock to be unlocked

§Panic

Check Self::is_cache_locked(), panic if this request doesn’t have a read lock.

Source

pub fn lock_duration(&self) -> Option<Duration>

How long did this request wait behind the read lock

Source

pub fn lookup_duration(&self) -> Option<Duration>

How long did this request spent on cache lookup and reading the header

Source

pub async fn purge(&mut self) -> Result<bool>

Delete the asset from the cache storage

§Panic

Need to be called after the cache key is set. Panic otherwise.

Source

pub fn cacheable_prediction(&self) -> bool

Check the cacheable prediction

Return true if the predictor is not set

Source

pub fn response_became_cacheable(&self)

Tell the predictor that this response, which is previously predicted to be uncacheable, is cacheable now.

Source

pub fn response_became_uncacheable(&self, reason: NoCacheReason)

Tell the predictor that this response is uncacheable so that it will know next time this request arrives.

Source

pub fn tag_as_subrequest(&mut self)

Tag all spans as being part of a subrequest.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more