Cache Handling & Auto Reload for MySQL Databases
npm install cache-em
$ 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.
$3
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:
` javascript
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.
$3
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!
` javascript
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.
` javascript
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.
` javascript
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 ;)
` javascript
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()
``