Permalink
Browse files

in memory caching

  • Loading branch information...
jairajs89 committed Nov 4, 2012
1 parent 3dcdec2 commit 55f0391edfbfbac7e0ca15d55ff3b815b409379f
Showing with 12 additions and 36 deletions.
  1. +2 −3 package.json
  2. +10 −33 zerver.js
View
@@ -2,7 +2,7 @@
"author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
"name": "zerver",
"description": "client-integrated webapp server",
- "version": "0.5.2",
+ "version": "0.5.3",
"repository": {
"type": "git",
"url": "git://github.com/jairajs89/zerver.git"
@@ -17,8 +17,7 @@
"clean-css" : ">=0.8.0" ,
"stalker" : ">=0.0.17" ,
"findit" : ">=0.1.2" ,
- "socket.io" : ">=0.9.0" ,
- "redis-url" : ">=0.1.0"
+ "socket.io" : ">=0.9.0"
},
"bin": {
"zerver": "run.js"
View
@@ -14,7 +14,6 @@ var ROOT_DIR = process.cwd(),
GZIP_ENABLED = false,
COMPILATION_ENABLED = false,
CACHE_ENABLED = false,
- CACHE_PREFIX = 'ZERVER_FILE_',
GZIPPABLE = {
'application/json' : true ,
'application/javascript' : true ,
@@ -36,7 +35,8 @@ var ROOT_DIR = process.cwd(),
API_SCRIPT_MATCH;
var memoryCache = {},
- app, apis, redis;
+ fileCache = {},
+ app, apis;
var startTimestamp;
@@ -128,7 +128,6 @@ function configureZerver (port, apiDir, apiURL, debug, refresh, manifests, produ
}
fetchAPIs();
- setupRedis();
http.globalAgent.maxSockets = 50;
}
@@ -138,20 +137,6 @@ function fetchAPIs () {
apis.setup(API_DIR, REFRESH);
}
-function setupRedis () {
- try {
- require('redis-url')
- .connect(process.env.REDISTOGO_URL)
- .on('connect', function () {
- redis = this;
- })
- .on('error', function () {
- redis = null;
- });
- }
- catch (err) {}
-}
-
function handleRequest (request, response) {
var urlParts = url.parse(request.url),
handler = {
@@ -170,24 +155,16 @@ function handleRequest (request, response) {
}
function tryResponseFromCache (handler, pathname, isApiCall, fallback) {
- if (!CACHE_ENABLED || isApiCall || !redis || !(pathname in memoryCache)) {
+ if (!CACHE_ENABLED || isApiCall || !(pathname in memoryCache)) {
fallback(handler, pathname, isApiCall);
return;
}
- redis.get(CACHE_PREFIX + pathname, function (err, data) {
- if (err) {
- console.error('cache failure: ' + pathname);
- delete memoryCache[pathname];
- fallback(handler, pathname, isApiCall);
- return;
- }
+ var args = memoryCache[pathname],
+ data = fileCache[pathname];
- var args = memoryCache[pathname];
-
- handler.type = args.type;
- finishResponse(handler, args.status, args.headers, data || '', args.isBinary, true);
- });
+ handler.type = args.type;
+ finishResponse(handler, args.status, args.headers, data, args.isBinary, true);
}
function dynamicResponse (handler, pathname, isApiCall) {
@@ -286,7 +263,7 @@ function finishResponse (handler, status, headers, data, isBinary, noCache) {
var pathname = handler.pathname,
type = handler.type;
- if (!noCache && CACHE_ENABLED && redis && (type !== 'api') && (type !== 'scheme') && !(pathname in memoryCache)) {
+ if (!noCache && CACHE_ENABLED && (type !== 'api') && (type !== 'scheme') && !(pathname in memoryCache)) {
memoryCache[pathname] = {
type : type ,
status : status ,
@@ -301,10 +278,10 @@ function finishResponse (handler, status, headers, data, isBinary, noCache) {
str += String.fromCharCode( data[i] );
}
- redis.set(CACHE_PREFIX + pathname, str);
+ fileCache[pathname] = str || '';
}
else {
- redis.set(CACHE_PREFIX + pathname, data);
+ fileCache[pathname] = data || '';
}
}

0 comments on commit 55f0391

Please sign in to comment.