The ObjectCache interface provides an in-memory index for storing and retrieving MoqtObject instances. Objects are keyed by their Location (group/object coordinates) and maintained in sorted order for efficient binary search operations.MOQtail provides two built-in implementations optimized for different use cases:
MemoryObjectCache
Unlimited in-memory storage with automatic sorting
RingBufferObjectCache
Fixed-size cache with automatic eviction of oldest objects
The ObjectCache interface defines the contract for all cache implementations:
export interface ObjectCache { /** Insert a new MoqtObject, preserving sorted order */ add(obj: MoqtObject): void /** Return objects whose Location is >= start and < end (end exclusive) */ getRange(start?: Location, end?: Location): MoqtObject[] /** Return the object whose Location exactly matches or undefined if absent */ getByLocation(location: Location): MoqtObject | undefined /** Current number of cached objects */ size(): number /** Remove all cached objects */ clear(): void}
Retrieve objects within a specific range using Location boundaries:
// Get all objects in group 1const startLoc = new Location(1, 0)const endLoc = new Location(2, 0) // Exclusiveconst group1Objects = cache.getRange(startLoc, endLoc)// Get all objects from a specific point onwardsconst fromLocation = new Location(1, 5)const remainingObjects = cache.getRange(fromLocation)// Get all objectsconst allObjects = cache.getRange()
// Retrieve a specific object by locationconst location = new Location(1, 0)const object = cache.getByLocation(location)if (object) { console.log(`Found object: group ${object.groupId}, object ${object.objectId}`)} else { console.log('Object not found in cache')}
import { RingBufferObjectCache } from 'moqtail'// Create a ring buffer with capacity for 100 objectsconst cache = new RingBufferObjectCache(100)// Default capacity is 100 if not specifiedconst defaultCache = new RingBufferObjectCache()
Rolling window buffering - Maintain a fixed-size window of content
// Buffering live video frames with a 5-second windowconst framesPerSecond = 30const windowSeconds = 5const frameCache = new RingBufferObjectCache(framesPerSecond * windowSeconds)// As new frames arrive, old ones are automatically evictedfor (const frame of liveVideoStream) { frameCache.add(frame)}
import { HybridTrackSource, RingBufferObjectCache } from 'moqtail'// Create cache for recent historical dataconst recentCache = new RingBufferObjectCache(300) // Last 10 seconds at 30fps// Hybrid source supports both live streaming and historical fetchconst hybridSource = new HybridTrackSource(videoObjectStream, recentCache)// Live objects are automatically added to cache// Subscribers can fetch recent history or subscribe to live stream
ObjectCache implementations are not thread-safe. Concurrent mutation from web workers or multiple threads is not supported. Callers must implement their own synchronization if needed.