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-em

v1.2.0

Published

Cache Handling & Auto Reload for MySQL Databases

Downloads

4

Readme

Cache-em

Autocomplete Input Forms or Drop Down Menu's with data to be served from a SQL Database are a pain to implement. This is for 3 mains reasons:

  1. Securing Data by limiting only suggested data on the frontend.
  2. Reduce the number of queries to the Database.
  3. Data for forms is dynamic in nature fetched directly from the database, rather that being hard coded on the frontend or the backend.

Installation

Installation is done using npm install command:

$ npm install cache-em

Introduction

Cache-em looks forward to solve all the 3 problems with a single method. Specify your SQL Query, Reload Time, and you are good to go with the cached data each and every time.

Usage

This package comes with a single method to fast-forward the needs of most of the developers out there. More methods will soon be incorporated to help in certain specific cases.

Declaring Cache Query

After importing, you should first declare the cache query. Declaring a cache query is just about storing a SQL Query against which you require cache to be stored on the backend. While specifying this query, you also specify the time interval after which you want the cache to be updated from the database. Here is an example on how to use it:

var cachem = require('cache-em')
var mysql = require('mysql')

var pool = mysql.createPool({
    connectionLimit: '10',
    host: 'localhost',
    user: 'me',
    password: 'secret',
    database: 'my_db'
})

var cached_states = cachem(pool, 1000, 'SELECT id, stateName from States')

Here, the first parameter is the SQL Pool which should be used for obtaining a connection to execute the query. Pass the time (ms) for auto-refreshing the data in the cache object, and lastly pass the query itself.

Fetching Results

Fetching the results is as easy as using fetch method. Fetch method returns a promise, which will resolve with the data against the declared query. If the data is fetched while the data hasn't expired, there will be no new query to the database, and you will receive the results from the cache, but if the data is requested after it has expired, fetch method will automatically query the database, replace the old cache, and return the results back to you!

let data_states = await cached_states.fetch()

Clearing Cache

Clearing the cache might come in handy in some cases. Clear method lets you clear the cache at any instance and save your memory. One thing to be noted is that even after you clear the cache, the query and the time are still stored, which gives you the option to call back the fetch method to repopulate the cache.

cached_states.clear()

Force Refreshing

In some cases, upon updating the database, you would want to force a refresh for the cache data. This can be done by the refresh method that allows you to bypass the auto-refresh time delared earlier.

let refreshed_data_states = await cached_states.refresh()

Example

And here we present you with a working example, to help you fast-forward with the relatively small documentation ;)

var cachem = require('cache-em')
var mysql = require('mysql')

var pool = mysql.createPool({
    connectionLimit: '10',
    host: 'localhost',
    user: 'me',
    password: 'secret',
    database: 'my_db'
})

var cached_states = cachem(pool, 1000, 'SELECT id, stateName from States')

FetchStates = async () => {
    let data_states = await cached_states.fetch()
    return data_states
}

FetchStates()

Contribution

The package is in its budding stage. Refer to the github repository for all the suggestions, pull-requests, issues, everything.