Created
September 11, 2012 08:08
-
-
Save jcuffe/3696818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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