Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
106 lines (75 sloc) 3.28 KB



node-mysql-cache is a MySQL query cache for node.js on top of Felix Geisendörfer's MySQL client node-mysql.

The goal of this implementation is to reduce the load on the MySQL server and serve a large number of clients simultaniously without querying the database for every request.

It was first implemented to very quickly serve data from a large table (>5M records) which was only updated every few minutes. The concept to use a cache invalidator was created because of this use case.



var Client = require('mysql').Client,
    client = new Client(),
    querycache = require('node-mysql-cache');

// Prepare and connect MySQL
client.user = "john";
client.password = "doe";
client.query("USE mydatabase");

var intInvalidator = new querycache.IntervalInvalidator(60000);           // invalidates cache every minute
var dbInvalidator = new querycache.DatabaseTimestampInvalidator(
                                  "SELECT some_timestamp FROM sometable", // if the timestamp changes, the query is invalidated
                                  client,                                 // the MySQL client with an established connection
                                  5000);                                  // check the timestamp every 5 seconds

  var queryId = cache.registerQuery("SELECT data FROM largetable", intInvalidator);
  var queryId2 = cache.registerQuery("SELECT moredata FROM hugetable", dbInvalidator);

  // the ready event fires as soon as all queries have fetched their data for the first time
  cache.once('ready', function() {
    // start the webserver or whatever...

    // ...

    // get the data from cache
    var results = cache.getResults(queryId);
    response.writeHead(200, {'Content-Type': 'application/json; encoding=utf-8'});

Current status

This module is in a development stage. I use it for a small server already but that does not mean it's bug free.

If you find an error, please file an issue!


Since this is my first github project, I used some files from René Kijewski as templates. Thank you, I hope you don't mind!


This module was only tested using node >= 0.5.x.

Otherwise the requirements are the same as for node-mysql.


  • TODO


  • Document the API
  • Add unit tests


node-mysql-cache is licensed under the MIT license.

Jump to Line
Something went wrong with that request. Please try again.