Module buffer

Module buffer 

Source
Expand description

In-memory capture buffer for web content

This module provides a thread-safe, bounded buffer for storing captured web content with automatic cleanup of stale entries and FIFO eviction when capacity is reached.

§Features

  • Bounded size: Maximum 1000 captures by default
  • Time-based expiry: Entries expire after 1 hour by default
  • Thread-safe: Uses RwLock for concurrent read access
  • Memory efficient: Optional LZ4 compression for content
  • Background cleanup: Automatic removal of expired entries

§Example

use reasonkit_web::buffer::{CaptureBuffer, CaptureRecord};
use std::time::Duration;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    // Create buffer with defaults
    let buffer = Arc::new(CaptureBuffer::new());

    // Or with custom settings
    let buffer = Arc::new(CaptureBuffer::builder()
        .max_size(500)
        .max_age(Duration::from_secs(1800)) // 30 minutes
        .enable_compression(true)
        .build());

    // Start background cleanup
    buffer.start_cleanup_task();

    // Add a capture
    let record = CaptureRecord::new(
        "https://example.com".to_string(),
        "<html>...</html>".to_string(),
        "Extracted content...".to_string(),
        1234,
    );
    buffer.push(record).await;

    // Retrieve captures
    let recent = buffer.get_recent(10).await;
    println!("Recent captures: {}", recent.len());
}

Structs§

BufferConfig
Configuration for the capture buffer
BufferStats
Statistics about buffer usage
CaptureBuffer
Thread-safe in-memory buffer for captured web content
CaptureBufferBuilder
Builder for CaptureBuffer
CaptureRecord
A single captured page record

Constants§

DEFAULT_CLEANUP_INTERVAL_SECS
Default cleanup interval (5 minutes)
DEFAULT_MAX_AGE_SECS
Default maximum age of captures (1 hour)
DEFAULT_MAX_SIZE
Default maximum number of captures in the buffer

Functions§

shared_buffer
Create a new shared capture buffer
shared_buffer_with_config
Create a new shared capture buffer with custom config

Type Aliases§

SharedCaptureBuffer
Wrapper for using CaptureBuffer in Arc contexts