Giter Site home page Giter Site logo

v-core9 / v_core_cache Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 226 KB

Fast and simple Cache for Node and Web.

Home Page: https://www.npmjs.com/package/v_core_cache

License: MIT License

JavaScript 36.29% HTML 6.83% TypeScript 56.88%
cache caching expire nodejs web

v_core_cache's Introduction

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

v_core_cache's People

Contributors

v-core9 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

v_core_cache's Issues

Data Estimation Size

Estimated Size in Bytes stopped working after update to Map instead of Object for the inner cache variable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.