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
PersistentCache- File-based caching for cross-session persistenceCacheStats- Cache performance statistics
Implementations§
Source§impl SchemaCache
impl SchemaCache
Sourcepub fn new() -> Self
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();Sourcepub fn with_settings(ttl: Duration, max_size: usize) -> Self
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 entriesmax_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);Sourcepub fn get_schema(
&mut self,
content: &str,
) -> Option<(IndexMap<String, ClassInfo>, IndexMap<String, PropertyInfo>)>
pub fn get_schema( &mut self, content: &str, ) -> Option<(IndexMap<String, ClassInfo>, IndexMap<String, PropertyInfo>)>
Get cached schema by content hash
Sourcepub fn put_schema(
&mut self,
content: &str,
classes: IndexMap<String, ClassInfo>,
properties: IndexMap<String, PropertyInfo>,
)
pub fn put_schema( &mut self, content: &str, classes: IndexMap<String, ClassInfo>, properties: IndexMap<String, PropertyInfo>, )
Cache a parsed schema
Sourcepub fn get_symbol_table(&mut self, content: &str) -> Option<SymbolTable>
pub fn get_symbol_table(&mut self, content: &str) -> Option<SymbolTable>
Get cached SymbolTable by content hash
Sourcepub fn put_symbol_table(&mut self, content: &str, table: SymbolTable)
pub fn put_symbol_table(&mut self, content: &str, table: SymbolTable)
Cache a SymbolTable
Sourcepub fn cleanup_expired(&mut self)
pub fn cleanup_expired(&mut self)
Clear all expired entries
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics
Trait Implementations§
Source§impl Debug for SchemaCache
impl Debug for SchemaCache
Auto Trait Implementations§
impl Freeze for SchemaCache
impl RefUnwindSafe for SchemaCache
impl Send for SchemaCache
impl Sync for SchemaCache
impl Unpin for SchemaCache
impl UnwindSafe for SchemaCache
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
Mutably borrows from an owned value. Read more
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>
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 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>
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