Node.JS basic memory store class

Using the Node.JS singleton structure, i create a basic memory store :

/* ************************************************************************

	Licence : LGPLv3

	Version: 0.1

	Authors: deisss

	Date: 2012-08-04

	Date of last modification: 2012-08-04

	Description: Basic memory system to get data from it

************************************************************************ */
 * Basic memory system to get data from it
var singleton = function singleton(){
	var content = {};

	 * Set (and replace if needed) a data to store
	 * @param string key The key reference to store data
	 * @param mixed data The data to store into this system
	 * @param integer expire The number of milliseconds before deleting from store
	this.set = function(key, data, expire){
		//2h in ms : 7200000
		expire = expire || 7200000;

		//First deleting the content if there is (this force to stop previous timeout)

		//Appending the content to the room, or replace one if there is
		content[key] = {
			data : data,
			//The setTimeout will clear data after expire, to always keep a clean system
			timer : setTimeout(function(){
			}.bind(this), expire);

	 * Return a data stored, or null if there is nothing
	 * @param string key The key to store data
	 * @return mixed The founded data, or null if there is an error
	this.get = function(key){
		if(typeof(content[key]) !== "undefined" && content[key] !== null &&
			typeof(content[key].data) !== "undefined" && content[key].data !== null
			return content[key].data;
		return null;

	 * Delete the stored key if it is existing
	 * @param string key The key to delete associated data
	this.remove = function(key){
		if(typeof(content[key]) !== "undefined" && content[key] !== null &&
			typeof(content[key].timer) !== "undefined" && content[key].timer !== null
			//Erasing timer
			//Deleting the content
			delete content[key];

	 * Get back the full content array
	 * @return array The full content
	this.getStoreContent = function(){
		//Return the full store
		return content;

	//Disable the ability to call the singleton without using the getInstance function
	if(singleton.caller != singleton.getInstance){
		throw new Error("This object cannot be instanciated");

/* ************************************************************************
************************************************************************ */
singleton.instance = null;

 * Singleton getInstance definition
 * @return singleton class
singleton.getInstance = function(){
	if(this.instance === null){
		this.instance = new singleton();
	return this.instance;

module.exports = singleton.getInstance();

As always I put this source as LGPLv3 feel free to use it everywhere (including commercial app). To do so, just include the file and play with :

var store = require("myfileWithStoreClassInside.js");

//Store the pair "a_key" : "a_value" for just one second (after it will be deleted)
store.set("a_key", "a_value", 1000);

console.log("get : "+store.get("a_key"));

//After two seconds, the key is already delete, the system will return null
  console.log("get deleted : "+store.get("a_key"));
}, 2000);

You can also use the remove function to manually delete a previous key.


Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:


Vous commentez à l'aide de votre compte Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )


Connexion à %s

%d blogueurs aiment cette page :