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

epic-cache

v1.1.0

Published

# Install

Downloads

5

Readme

Simple nodejs caching module

Install

npm i epic-cache

Cache classes

Cache

The base abstract class extended by the other cache classes.

Only used to create other types of cache.

GenericCache

The main cache, it holds a map of <String, CacheType = any>

Create the instance

import { GenericCache } from 'epic-cache';
const cache = new GenericCache<CacheType>(options);
  • CacheType: (default: any) Type of the elements you want to store in cache.
  • options?: GenericCacheOptions

GenericCacheOptions

  • maxSize: (default: 1000) max elements to store in cache

  • sizeExceededStrategy: (default: no-cache) action to perform when an element is add to the cache when it's full.

    • no-cache: don't put the new element in the cache
    • throw-error: throw an error
  • clearExpiredOnSizeExceeded: (default: true) clear all the expired keys before trying to an element to the cache when it's full

  • defaultExpireTime: (default: 15m) default expire time assigned to elements add to cache without their own expireTime

    • The type of defaultExpireTime is Time | TimeString | number

Read more about the time classes

  • expireOnInterval: (default: true) check and remove the expired keys every interval (the cache will always remove the expired keys when you try to get them regardless of this value)

  • expireCheckInterval: (default: 10m) the time interval to check and remote expired keys

    • The type of expireCheckInterval is Time | TimeString | number

Read more about the time classes

Methods

Add an element

import { GenericCache, CacheElement } from 'epic-cache';
const cache = new GenericCache<number>();

// Add MattAge to the cache with value 31 with default expireTime of the cache
cache.set('MattAge', CacheElement.from(31));

// Add TaylorAge to the cache with value 22 with 10 seconds before expiration
cache.set('TaylorAge', CacheElement.from(22, '10s'));

// Note: you can also use the add function - The behavior is the exact same
cache.add('TaylorAge', CacheElement.from(22, '10s'))

Read more about the CacheElement class

Get an element

import { GenericCache, CacheElement } from 'epic-cache';
const cache = new GenericCache<number>();

cache.set('TaylorAge', CacheElement.from(22));

// Get the element from the cache
const taylorAge = cache.get('TaylorAge'); // 22

Delete an element

// TODO: Create the code

Events

// TODO: Create the code and the docs

CacheElement

// TODO: Create the code and the docs

Time classes

When you need to provide a time value you can use one of this three methods:

  • TimeString type
import type { TimeString } from 'epic-cache';

const oneSecond: TimeString = '1s';
const fiveSeconds: TimeString = '5s';
const oneMinute: TimeString = '1m';
const oneHour: TimeString = '1h';
  • number as milliseconds
const oneSecond = 1000;
const fiveSeconds = 1000 * 5;
const oneMinute = 1000 * 60;
const oneHour = 1000 * 60 * 60;
  • Time class
import { Time } from 'epic-cache';

// You can pass a number or a TimeString as value
const oneSecond = new Time(1000);
const fiveSecond = new Time('5s');

// You can also use the static method from to create an instance
const oneMinute = Time.from(1000 * 60);
const oneHour = Time.from('1h');