ObliviousSet

2.0.0 · active · verified Sun Apr 19

ObliviousSet provides a JavaScript Set-like data structure where each entry is associated with a Time-To-Live (TTL). Unlike traditional caching mechanisms that often rely on intervals or timeouts for eviction, ObliviousSet's design avoids these, enabling proper garbage collection of the set instance when no active references exist. The current stable version is 2.0.0, which targets modern Node.js environments (>=16). While a strict release cadence isn't published, the package is actively maintained with updates released as needed for features or bug fixes. Its key differentiator is the efficient, passive expiration model where entries are only 'removed' (marked as expired) when explicitly checked via the `has()` method or when iterating over active elements, rather than through eager background processes. This minimizes overhead and resource consumption.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create an ObliviousSet, add elements, check for their existence before and after expiration, and clear the set.

import { ObliviousSet } from 'oblivious-set';

// Create a set with a TTL of 100 milliseconds
const obliviousSet = new ObliviousSet(100);

// Add a value; its TTL starts now
obliviousSet.add('user_session_123');

// Check existence immediately
console.log('Has user_session_123 after add:', obliviousSet.has('user_session_123')); // true

// Wait for the TTL to expire
setTimeout(() => {
  console.log('Has user_session_123 after 150ms:', obliviousSet.has('user_session_123')); // false

  // Add another value and check its initial state
  obliviousSet.add('another_item');
  console.log('Has another_item:', obliviousSet.has('another_item')); // true

  // Clear all entries from the set
  obliviousSet.clear();
  console.log('Set size after clear:', obliviousSet.size); // 0
}, 150);

view raw JSON →