Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(driver): new memory-meta driver #587

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sandros94
Copy link

@sandros94 sandros94 commented Feb 2, 2025

Resolves #583

while experimenting I've noticed that adding metadata's to the default memory drive would introduce even a small amount of overhead (depending on the platform). So instead of modifying the original driver I decided to experiment and then build a new one, for those that are actually interested in it.

It mimics the *time metadata from storages like fs, while supporting ttl (globally and per-key, to circumvent lru-cache limitation) and correctly dispose the setTimeout when the related key gets removed or storage disposed.

I also fixed the base issue that is present in the default memory driver, although I plan to fix it as well in a separate PR

  • Implementation
  • Tests
  • Docs

    This drive aims to be a more advanced version of the memory driver by adding metadata support, while introducing a small overhead and allocation cost.

    By supporting metadata, it allows for features like Time-To-Live (TTL),active by default, and optionally tracking a rough size of the stored data. The TTL can be set per item or globally.

    Each key has its own metadata, that includes:

    • ttl: remaining Time-To-Live in milliseconds, if set during creation.
    • atime: last access time.
    • mtime: last modified time.
    • ctime: last change time.
    • birthtime: Creation time.
    • size: Size in bytes, if enabled.

    ::note
    For memory efficency all *time values are stored in milliseconds since the Unix epoch. But returned as Date when called via getMeta.
    ::

    import { createStorage } from "unstorage";
    import memoryMetaDriver from "unstorage/drivers/memory-meta";
    
    const storage = createStorage({
      driver: memoryMetaDriver({
        base: "my-storage",   // Optional prefix to use for all keys.
        ttl: 1000 * 60 * 60,  // default `undefined`
        ttlAutoPurge: true,   // default `true`
        trackSize: true,      // default `false`
      }),
    });

    Options:

    • base: Optional prefix to use for all keys. Can be used for namespacing.
    • ttl: Default Time-To-Live for all items in milliseconds.
    • ttlAutoPurge: Whether to automatically purge expired items. (default: true)
    • trackSize: Whether to track the size of items in bytes. (default: false)

@sandros94 sandros94 requested a review from pi0 as a code owner February 2, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add meta and ttl to memory driver (new driver)
1 participant