Skip to main content

SchemaCache

Struct SchemaCache 

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

In-memory cache for parsed RDF schemas and symbol tables.

Provides fast caching with content-based hashing, TTL expiration, and LRU eviction. Ideal for repeated parsing of the same RDF schemas during a single session.

§Features

  • Content-based hashing: Automatically deduplicates identical schemas
  • TTL expiration: Configurable time-to-live (default: 1 hour)
  • LRU eviction: Automatic removal of least-recently-used entries when full
  • Hit/miss tracking: Built-in statistics for cache performance monitoring
  • Dual storage: Caches both raw parsed schemas and symbol tables

§Performance

  • Lookup: O(1) average case (HashMap-based)
  • Insertion: O(1) average case
  • Space overhead: ~2-3x original schema size (includes metadata)

§Examples

§Basic Usage

use tensorlogic_oxirs_bridge::{SchemaCache, SchemaAnalyzer};
use anyhow::Result;

fn main() -> Result<()> {
    let mut cache = SchemaCache::new();
    let turtle = r#"
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix ex: <http://example.org/> .
        ex:Person a rdfs:Class .
    "#;

    // First parse - cache miss
    let table1 = if let Some(cached) = cache.get_symbol_table(turtle) {
        cached
    } else {
        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(turtle)?;
        analyzer.analyze()?;
        let table = analyzer.to_symbol_table()?;
        cache.put_symbol_table(turtle, table.clone());
        table
    };

    // Second access - cache hit (much faster)
    let table2 = cache.get_symbol_table(turtle).expect("should be cached");

    // Statistics
    let stats = cache.stats();
    assert_eq!(stats.total_hits, 1);
    assert_eq!(stats.total_misses, 1);
    assert_eq!(stats.hit_rate, 0.5);
    Ok(())
}

§Custom TTL and Size

use tensorlogic_oxirs_bridge::SchemaCache;
use std::time::Duration;

// Cache with 30-minute TTL and max 50 entries
let cache = SchemaCache::with_settings(
    Duration::from_secs(30 * 60),  // TTL: 30 minutes
    50                              // Max size: 50 entries
);

§Cleanup

use tensorlogic_oxirs_bridge::SchemaCache;

let mut cache = SchemaCache::new();
// ... use cache ...

// Remove expired entries
cache.cleanup_expired();

// Clear everything
cache.clear();

§See Also

Implementations§

Source§

impl SchemaCache

Source

pub fn new() -> Self

Creates a new cache with default settings.

Default configuration:

  • TTL: 1 hour (3600 seconds)
  • Max entries: 100
§Examples
use tensorlogic_oxirs_bridge::SchemaCache;

let cache = SchemaCache::new();
Source

pub fn with_settings(ttl: Duration, max_size: usize) -> Self

Creates a cache with custom TTL and maximum size.

§Arguments
  • ttl - Time-to-live for cache entries
  • max_size - Maximum number of entries before LRU eviction kicks in
§Examples
use tensorlogic_oxirs_bridge::SchemaCache;
use std::time::Duration;

// 5-minute TTL, max 25 entries
let cache = SchemaCache::with_settings(Duration::from_secs(300), 25);
Source

pub fn get_schema( &mut self, content: &str, ) -> Option<(IndexMap<String, ClassInfo>, IndexMap<String, PropertyInfo>)>

Get cached schema by content hash

Source

pub fn put_schema( &mut self, content: &str, classes: IndexMap<String, ClassInfo>, properties: IndexMap<String, PropertyInfo>, )

Cache a parsed schema

Source

pub fn get_symbol_table(&mut self, content: &str) -> Option<SymbolTable>

Get cached SymbolTable by content hash

Source

pub fn put_symbol_table(&mut self, content: &str, table: SymbolTable)

Cache a SymbolTable

Source

pub fn cleanup_expired(&mut self)

Clear all expired entries

Source

pub fn clear(&mut self)

Clear all cache entries

Source

pub fn stats(&self) -> CacheStats

Get cache statistics

Trait Implementations§

Source§

impl Debug for SchemaCache

Source§

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

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

impl Default for SchemaCache

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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