pub struct LearningCache { /* private fields */ }Expand description
On-disk learning cache.
Keys are JSON-serialized CacheKey strings because JSON object keys must be strings.
Implementations§
Source§impl LearningCache
impl LearningCache
Sourcepub fn open_default() -> Result<Self, LearningCacheError>
pub fn open_default() -> Result<Self, LearningCacheError>
Open the default cache at ~/.wafrift/learning_cache.json.
§Errors
Returns an error if the home directory cannot be determined.
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self, LearningCacheError>
pub fn open(path: impl AsRef<Path>) -> Result<Self, LearningCacheError>
Open or create a cache at a specific path.
A corrupted cache file (kill-9 mid-save, disk corruption, partial
flush) is moved aside to <path>.corrupt-<epoch> and a fresh
empty cache is returned. Crashing the whole strategy engine on
one bad JSON file would lose all subsequent learning — better to
surface the corruption via tracing::warn and keep going.
§Errors
Returns an error only if the file exists, looks fine, and the underlying I/O still fails (permission denied, etc.).
Sourcepub fn get(&self, key: &CacheKey) -> Option<&CacheEntry>
pub fn get(&self, key: &CacheKey) -> Option<&CacheEntry>
Look up a cached pipeline.
Sourcepub fn record_success(&mut self, key: CacheKey, pipeline: EvasionPipeline)
pub fn record_success(&mut self, key: CacheKey, pipeline: EvasionPipeline)
Record a successful bypass.
Sourcepub fn record_failure(&mut self, key: CacheKey, pipeline: EvasionPipeline)
pub fn record_failure(&mut self, key: CacheKey, pipeline: EvasionPipeline)
Record a failed attempt.
Sourcepub fn save(&self) -> Result<(), LearningCacheError>
pub fn save(&self) -> Result<(), LearningCacheError>
Persist the cache to disk atomically.
Writes to a sibling <path>.tmp.<pid>.<epoch> file, fsyncs it,
then renames over the target path. A kill-9 between write and
rename leaves the previous good cache file untouched instead
of producing the half-written JSON that was poisoning subsequent
open calls.
§Errors
Returns an error if the file cannot be written or renamed.
Trait Implementations§
Source§impl Clone for LearningCache
impl Clone for LearningCache
Source§fn clone(&self) -> LearningCache
fn clone(&self) -> LearningCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for LearningCache
impl Debug for LearningCache
Source§impl Default for LearningCache
impl Default for LearningCache
Source§fn default() -> LearningCache
fn default() -> LearningCache
Source§impl<'de> Deserialize<'de> for LearningCache
impl<'de> Deserialize<'de> for LearningCache
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for LearningCache
impl RefUnwindSafe for LearningCache
impl Send for LearningCache
impl Sync for LearningCache
impl Unpin for LearningCache
impl UnsafeUnpin for LearningCache
impl UnwindSafe for LearningCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more