npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dalisoft/cache-ttl

v0.4.4

Published

Caching Library with TTL for Node.js and browser

Downloads

122

Readme

CacheTTL

Caching Library with TTL for Node.js and browser

NOTE: From latest release, the sync variant is removed due of stability, performance and security reasons

Features

  • Out-of-the-box Promise & Async/Await support
  • Fast
  • No duplication
  • Almost zero-config
  • Flexible
  • Memory-effecient
  • On browsers works too
  • Types declaration for IDE/Editor
  • File-based mode (only for Node.js)
  • Custom user mode defining

Import

// ES6
import CacheTTL from '@dalisoft/cache-ttl';

// or

// CommonJS
const CacheTTL = require('@dalisoft/cache-ttl');

// or

const CacheTTL = window.CacheTTL;

Example

const cache = new CacheTTL(1000 /* in ms */, saveAsFile?: boolean);

await cache.set('my-cache', () => 'i am live here around 1 sec'); // Returns String

await cache.set('my-response', async () => await axios({...})); // it's too lives here around 1 sec, returns Promise

or you can see how to define your own caching method (you can use Redis, MongoDB or everywhere). Async/Promise also support out-of-box

let _map = new Map();
const cache = new CacheTTL(5000, 'custom', {
  getTransform(key: string): any {
    return _map.get(key);
  },
  hasTransform(key: string): boolean {
    return _map.has(key);
  },
  setTransform(key: string, value: any): void {
    _map.set(key, value);
  },
  deleteTransform(key: string): void {
    _map.delete(key);
  },
});

For more info see tests. About invalidation, expire time and other things the core takes care of this, you shouldn't worry for these.

Note: For security reason, for custom and FS modes, functions and symbols are not supported. PR's are welcome to fix this.

Methods

.get(key: string): Promise<CacheItem>

Returns value of cache if still valid or null if value is expired

.has(key: string): Promise<Boolean>

Returns the value still valid or removed/expired?!

.set(key: string, value: AsyncFunction | Promise, ttl?: number): AsyncFunction | Promise

Creates new value for specified key and returns value

.expire(key: string, ttl?: number): AsyncFunction | Promise

Sets new expire for specified key and returns value

.getOrSet(key: string, value: AsyncFunction | Promise): AsyncFunction<value> | Promise<value>

Get if there a valid value or creates a new one

.delete(key: string): AsyncFunction | Promise

Removes the cache

.clear()

Clear cache instance to be empty

.destroy()

Destroys cache instance

License

MIT