Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@isaacs/ttlcache

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@isaacs/ttlcache

The time-based use-recency-unaware cousin of [`lru-cache`](http://npm.im/lru-cache)

  • 1.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8M
decreased by-7.92%
Maintainers
1
Weekly downloads
 
Created

What is @isaacs/ttlcache?

@isaacs/ttlcache is a simple and efficient in-memory cache with time-to-live (TTL) support. It allows you to store key-value pairs with an expiration time, automatically removing expired items. This can be useful for caching results of expensive operations, reducing load on databases, or managing temporary data.

What are @isaacs/ttlcache's main functionalities?

Basic Cache Operations

This feature allows you to perform basic cache operations such as setting, getting, deleting, and checking the existence of a key in the cache.

const TTLCache = require('@isaacs/ttlcache');
const cache = new TTLCache({ ttl: 5000 }); // TTL in milliseconds

// Set a value
cache.set('key', 'value');

// Get a value
const value = cache.get('key');
console.log(value); // Outputs: 'value'

// Delete a value
cache.del('key');

// Check if a key exists
const exists = cache.has('key');
console.log(exists); // Outputs: false

Automatic Expiration

This feature demonstrates the automatic expiration of cache entries after the specified TTL. The key 'key' will be automatically removed from the cache after 2 seconds.

const TTLCache = require('@isaacs/ttlcache');
const cache = new TTLCache({ ttl: 2000 }); // TTL in milliseconds

// Set a value
cache.set('key', 'value');

// Wait for 3 seconds
setTimeout(() => {
  const value = cache.get('key');
  console.log(value); // Outputs: undefined (since the key has expired)
}, 3000);

Custom TTL for Each Entry

This feature allows you to set a custom TTL for each cache entry. The key 'key' will be automatically removed from the cache after 1 second.

const TTLCache = require('@isaacs/ttlcache');
const cache = new TTLCache();

// Set a value with a custom TTL
cache.set('key', 'value', 1000); // TTL in milliseconds

// Wait for 1.5 seconds
setTimeout(() => {
  const value = cache.get('key');
  console.log(value); // Outputs: undefined (since the key has expired)
}, 1500);

Other packages similar to @isaacs/ttlcache

FAQs

Package last updated on 01 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc