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

cache-bucket

v1.1.4

Published

Cache your data with TTL. Using this package in node.js and browser.

Downloads

5

Readme

Introduction

Cache your data with TTL. Using this package in node.js and browser.

Installation

By npm

npm install cache-bucket

Or by yarn

yarn add cache-bucket

Support

Node.js

FileCache and MemoryCache is enable.

Notice that CacheFile will touch a file as storage. The default file path is: ./.filecache

// Based on file
import {cache} from 'cache-bucket/file-cache';

// Based on memory
import {cache} from 'cache-bucket/memory-cache';

Browser

LocalCache and SessionCache and MemoryCache is enable.

Notice that LocalCache is based on localStorage, and SessionCache is based on sessionStorage.

// Based on memory
import {cache} from 'cache-bucket/memory-cache';

// Based on localStorage
import {cache} from 'cache-bucket/local-cache';

// Based on sessionStorage
import {cache} from 'cache-bucket/session-cache';

Methods

get(key: string, defaultValue?: any) => any

Get your cache by key.

If cache is empty, defaultValue will be used. If parameter defaultValue is missing, method will respond null.

cache.get('foo'); // null
cache.get('foo', 'default-bar'); // default-bar

set(key: string, value: any, duration?: number) => void

Set cache data.

The parameter value type can be string, number, object, array. Cache data will expired after millSeconds when you provide duration.

cache.set('foo', 'bar');
cache.set('obj', {pkg: 'cache-bucket'});

// Expired after 3 second.
cache.set('array', ['cache', 'bucket'], 3000);

getOrSet(key: string, onEmpty: () => any, duration?: number) => any

Get cache data.

When cache data is missing, method will set data immediately. And then return it.

cache.getOrSet('foo', () => {
  return 'bar';
}); // bar

add(key: string, value: any, duration?: number) => boolean

Set cache data when key is not exist.

cache.add('foo', 'bar'); // true
cache.add('foo', 'new-bar'); // false

remove(key: string) => void

Delete a cache data.

cache.remove('foo');

clearExpired() => void

Clear all expired cache data

cache.clearExpired();

clearAll() => void

Clear all data.

cache.clearAll();

Advanced

Multiply instances.

import {MemoryCache} from 'cache-bucket/memory-cache';

const cache = new MemoryCache();

cache.set('foo', 'bar');
cache.get('foo'); // bar
import {FileCache} from 'cache-bucket/file-cache';

const cache = new FileCache('./.new-cachefile');

cache.set('foo', 'bar');
cache.get('foo'); // bar

Config

You can choose the file you want to put data when Using FileCache. Just creating config file cache-bucekt.json in project's root directory.

{
  "defaultFilePath": "./.custom-cache-file"
}