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

v_core_cache

v1.4.0

Published

Simple JavaScript Cache with expires/ttl and auto cleanups for Node and Web Application.

Downloads

29

Readme

v_core_cache

Simple Cache Solution for Node and Web.

Sections:

  1. 📑 How to use
  2. 🚗 Functions and Methods
  3. 🎪 Events
  4. ➰ Auto Cleanup Expired
  5. ❌ Deleted / Removed

📑 How to use

const { V_Core_Cache } = require('v_core_cache');
const cache = new V_Core_Cache();

// OR

const { createCache } = require('v_core_cache');
const cache = createCache();

🚗 Functions and Methods

1. Get Item Value

await cache.get(key)  //> anything you put in
cache.getSync(key)

2. Get Whole Cache

Returns all cache.

cache.getAll() //> object

3. Size of Cache

Returns the approximate size of the cache in bytes.

await cache.size();  //> 1507114 
cache.sizeSync();

4. Has Item?

Returns true if the key exists in the cache and is not expired.

cache.has(key)   

5. Set Item

Set/Create/Update an item in the cache. Will overwrite existing item.

await cache.set(key, data, expires?)
cache.setSync(key, data, expires?)  

6. Purge cache

Returns true if cache was successfully purged. Otherwise, returns false if cache is already empty.

await cache.purge()

7. Delete item from cache

await cache.del(key);  //> true/false
cache.delSync(key);

8. Stats

Returns stats about the cache.

cache.stats() //> { hits: 156, misses: 15, count: 33, size: 1507114 }

9. Purge Stats

This basically just resets counters for hits and misses.

cache.purgeStats()  //> { hits: 0, misses: 0, count: 33, size: 1507114 }

10. Get Item Expire Time

Returns the time in milliseconds when the item will expire.

cache.getExpire(key); //> 150123456789 [ Date.now() + expires]

11. Cleanup Expired Items

Returns the number of expired items removed.

await cache.cleanup(); 

12. Count Items

Returns the number of items in cache.

await cache.count();
cache.countSync(); 

🎪 Events

Management

1. Add Event Listener

cache.addListener("set", (data) => console.log(data));
// or
cache.on("set", (data) => console.log(data));

2. Remove Event Listener

cache.removeListener("set", (data) => console.log(data));
// or
cache.off("set", (data) => console.log(data));

3. Prepend Event Listener

cache.prependListener("set", (data) => console.log(data));
// or
cache.pre("set", (data) => console.log(data));

4. Get Registered EventNames

console.log(cache.eventNames());

5. Remove All Listeners

Removes all registered listeners for a single event

cache.removeAllListeners('set')

6. Purge All Listeners

Removes all registered listeners for all registered events

cache.purgeAllListeners()

Available events

1. SET

Returns {key, value} pair.

cache.on('set', (item) => console.log(item.key, item.value))

1.2 set with key

In this case we are returning the value only.

cache.on('set/{key}', (value) => console.log(value))

2. GET

cache.on('get', (item) => console.log(data)) //> { key, value } - value can be undefined 

3. HIT

cache.on('hit', (item) => console.log(item)) //> { key, value } 

4. MISS

cache.on('miss', (item) => console.log(item)) //> { key } 

5. PURGE

cache.on('purge', (status) => console.log(status)) //> true/false - can return false if already empty

6. PURGE_STATS

cache.on('purge_stats', (data) => console.log(data)) //> { hits, misses, count, size } - returns stats after purging them.

6. cleanup

returns number of affected items

cache.on('cleanup', (data) => console.log(data)) //> number 

6. addListener

New listener added

cache.on('addListener', (data) => console.log(data)) 

6. removeListener

Removed event listener

cache.on('addListener', (data) => console.log(data)) 

➰ Auto Cleanup Expired

const V_Core_Cache = require('v_core_cache');
const cache = new V_Core_Cache({ cleanInterval: 250 }); // Number in milliseconds 

NOTE: When using autoCleanup you should stop the cleanup interval by calling cache.stopCleanup()


✅ Tests and Coverage with Jest

Test and Coverage with Jest