Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created September 11, 2012 08:08
Show Gist options
  • Select an option

  • Save jcuffe/3696818 to your computer and use it in GitHub Desktop.

Select an option

Save jcuffe/3696818 to your computer and use it in GitHub Desktop.
var redis = require('redis'),
client = redis.createClient();
exports.freshKey = function() {
// create an 5 character alphanumeric string from a random number
candidate = genKey();
while (exists(candidate)) {
candidate = genKey();
}
return candidate;
};
exports.create = function(key, callback) {
// TODO: think of better callback arguments
if (!exists(key)) {
client.set(key, Date());
callback(null);
} else {
console.log(key + ' exists!')
callback(true);
}
};
var exists = function(key) {
return client.exists(key, function(err, exists) { return !!exists });
};
var genKey = function() {
return Math.random().toString(36).substring(2, 7);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment