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

@aspiesoft/obj-memory-cache

v0.2.7

Published

A simple in memory cache that can watch files, or expire at a set time.

Downloads

8

Readme

Object Memory Cache

npm Libraries.io dependency status for latest release GitHub top language NPM

npm npm

paypal

This cache stores JSON objects, and strings into a javascript Map. Objects are stringified before storage, and parsed when retrieved. Before storage, this module uses another npm module (lzutf8) to compress the string and reduce storage.

When you set a cache, you can tell it to expire any amount of time in the future. When you get the cache data, the expiation of that item is checked, and removed if expired. Every 10 minutes, the entire cache is checked for expired items, to remove unused cache items.

You can also watch a file (or directory) for changes, or watch for when a file is added. When you listen for file changes, the cache items listening for that file change are removed.

Note: listening for file changes requires the chokidar npm module

Installation

npm install @aspiesoft/obj-memory-cache

# to add optional file watching
npm install chokidar

Setup

const memoryCache = require('@aspiesoft/obj-memory-cache');

// optional (to watch files for updates) (requires chokidar npm module)
memoryCache({watch: __dirname});
// note: this just sets the root dir to watch, and not the actual files

// by default, the cache is disabled in development, to help with debugging
// to test the cache in development, simply run this function
memoryCache.cacheDevelopment();

// you can also run this function with the false parameter to disable it later
memoryCache.cacheDevelopment(false);

Usage

function getData(){
    let cache = memoryCache.get('myCachePath');
    if(cache){return cache;}

    let data = {myJsonData: 'example data'};

    // use global cache
    memoryCache.set('myCachePath', data, {expire: '10m'}); // 10 minutes

    // build local cache
    const localCache = memoryCache.newCache(options);
    localCache.set('myCachePath', data, {expire: '10m'}); // does not effect global cache
    // note: if you loose the var, that local cache is lost

    return data;
}

function getFileData(filePath){
    let cache = memoryCache.get(filePath);
    if(cache){return cache;}

    let jsonData = require(filePath);

    let defaultJsonData = require('default/data/file/path');

    let result = {};

    Object.assign(result, defaultJsonData, jsonData);

    memoryCache.set(filePath, result, {listen: [filePath, 'default/data/file/path']});

    return result;
}

// basic cache functions (same for global cache and local cache) (example: memoryCache.get(), localCache.get())

// get item from cache
cache.get('index');

// set/add item to cache
cache.set('index', {data: 'my data'} || 'my data', {/* options */});

// remove item from cache
cache.delete('index');

// clear entire cache
cache.clear();

// watch files for cache
cache.watch([/* list of file paths */]);