Struct bkt::Bkt

source ·
pub struct Bkt { /* private fields */ }
Expand description

This struct is the main API entry point for the bkt library, allowing callers to invoke and cache subprocesses for later reuse.

Example:

let bkt = bkt::Bkt::in_tmp()?;
let cmd = bkt::CommandDesc::new(["curl", "https://expensive.api/foo"]);
let (result, age) = bkt.retrieve(&cmd, Duration::from_secs(60*60))?;
println!("Retrieved: {:?}\nAge: {:?}", result, age);

Implementations§

source§

impl Bkt

source

pub fn in_tmp() -> Result<Self>

Creates a new Bkt instance using the std::env::temp_dir as the cache location. If a BKT_TMPDIR environment variable is set that value will be preferred.

§Errors

If preparing the tmp cache directory fails.

source

pub fn create(root_dir: PathBuf) -> Result<Self>

Creates a new Bkt instance.

The given root_dir will be used as the parent directory of the cache. It’s recommended this directory be in a tmpfs partition, on an SSD, or similar, so operations are fast.

§Errors

If preparing the cache directory under root_dir fails.

source

pub fn scoped(self, scope: String) -> Self

Associates a scope with this Bkt instance, causing it to namespace its cache keys so that they do not collide with other instances using the same cache directory. This is useful when separate applications could potentially invoke the same commands but should not share a cache. Consider using the application’s name, PID, and/or a timestamp in order to create a sufficiently unique namespace.

source

pub fn cleanup_on_refresh(self, cleanup: bool) -> Self

By default a background cleanup thread runs on cache misses and calls to Bkt::refresh() to remove stale data. You may prefer to manage cleanup yourself if you expect frequent cache misses and want to minimize the number of threads being created. See Bkt::cleanup_once() and Bkt::cleanup_thread() if you set this to false.

source

pub fn retrieve<T>( &self, command: T, ttl: Duration ) -> Result<(Invocation, CacheStatus)>

Looks up the given command in Bkt’s cache. If found (and newer than the given TTL) returns the cached invocation. If stale or not found the command is executed and the result is cached and then returned.

The second element in the returned tuple reports whether or not the invocation was cached and includes information such as the cached data’s age or the executed subprocess’ runtime.

§Errors

If looking up, deserializing, executing, or serializing the command fails. This generally reflects a user error such as an invalid command.

source

pub fn retrieve_streaming<T>( &self, command: T, ttl: Duration, stdout_sink: impl Write + Send, stderr_sink: impl Write + Send ) -> Result<(Invocation, CacheStatus)>

Experimental This method is subject to change.

Looks up the given command in Bkt’s cache. If found (and newer than the given TTL) returns the cached invocation. If stale or not found the command is executed and the result is cached and then returned. Additionally, the invocation’s stdout and stderr are written to the given streams in real time.

The second element in the returned tuple reports whether or not the invocation was cached and includes information such as the cached data’s age or the executed subprocess’ runtime.

§Errors

If looking up, deserializing, executing, or serializing the command fails. This generally reflects a user error such as an invalid command.

source

pub fn refresh<T>( &self, command: T, ttl: Duration ) -> Result<(Invocation, Duration)>

Unconditionally executes the given command and caches the invocation for the given TTL. This can be used to “warm” the cache so that subsequent calls to execute are fast.

The second element in the returned tuple is the subprocess’ execution time.

§Errors

If executing or serializing the command fails. This generally reflects a user error such as an invalid command.

source

pub fn refresh_streaming<T>( &self, command: T, ttl: Duration, stdout_sink: impl Write + Send, stderr_sink: impl Write + Send ) -> Result<(Invocation, Duration)>

Unconditionally executes the given command and caches the invocation for the given TTL. This can be used to “warm” the cache so that subsequent calls to execute are fast. The invocation’s stdout and stderr are written to the given streams in real time in addition to being cached.

The second element in the returned tuple is the subprocess’ execution time.

§Errors

If executing or serializing the command fails. This generally reflects a user error such as an invalid command.

source

pub fn cleanup_once(&self) -> JoinHandle<Result<()>>

Initiates a single cleanup cycle of the cache, removing stale data in the background. This should be invoked by short-lived applications early in their lifecycle and then joined before exiting. execute_and_cleanup can be used instead to only trigger a cleanup on a cache miss, avoiding the extra work on cache hits. Long-running applications should typically prefer cleanup_thread which triggers periodic cleanups.

§Errors

The Result returned by joining indicates whether there were any unexpected errors while cleaning up. It should be Ok in all normal circumstances.

source

pub fn cleanup_thread(&self) -> JoinHandle<()>

Initiates an infinite-loop thread that triggers periodic cleanups of the cache, removing stale data in the background. It is not necessary to join() this thread, it will be terminated when the main thread exits.

Trait Implementations§

source§

impl Clone for Bkt

source§

fn clone(&self) -> Bkt

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Bkt

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Bkt

§

impl Send for Bkt

§

impl Sync for Bkt

§

impl Unpin for Bkt

§

impl UnwindSafe for Bkt

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, 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,

§

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>,

§

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>,

§

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

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

§

fn vzip(self) -> V