Permalink
Browse files

make cache configurable

  • Loading branch information...
1 parent 171d5fb commit a86d8222d993ec5c4c33c7108d25d2a4089f04db @mountain committed Aug 8, 2010
Showing with 38 additions and 21 deletions.
  1. +14 −4 app/redirect.js
  2. +10 −10 app/templates/problem.erb
  3. +10 −5 config/i18n.js
  4. +2 −1 settings.json
  5. +2 −1 vendor/server.js
View
@@ -1,17 +1,24 @@
+var util = require('../vendor/util'),
+ sys = require('sys');
+
exports.app = function(env) {
var problem = env.templates['problem'],
cache = env.cache,
- admin = env.admin;
+ admin = env.admin,
+ msg = env.i18n.msg;
return function(req, res, lang, title) {
- var pageId = parseInt(title.toLowerCase(), 36),
+ var lang = lang || 'en',
+ pageId = parseInt(title.toLowerCase(), 36),
host = lang + '.wikipedia.org';
var url = cache?cache.getItem(lang + ':' + pageId):undefined;
if(url) {
res.redirect(url);
+ sys.puts('cache get: (' + lang + ':' + pageId + ')');
+ sys.puts('cache stats: (hits ' + cache.stats.hits + ', misses ' + cache.stats.misses + ')');
} else {
var http = require('http'),
wp = http.createClient(80, host),
@@ -39,13 +46,16 @@ exports.app = function(env) {
}
}
} catch (e) {
- require('sys').puts("error when parsing json: " + e);
+ sys.puts('error when parsing json: ' + e);
}
if(url) {
cache.setItem(lang + ':' + pageId, url);
+ sys.puts('cache put: (' + lang + ':' + pageId + ')');
+ sys.puts('cache stats: (hits ' + cache.stats.hits + ', misses ' + cache.stats.misses + ')');
res.redirect(url);
} else {
- var html = problem({});
+ var dir = util.htmlDir(env, lang);
+ var html = problem({lang: lang, msg: msg, dir: dir});
res.simpleHtml(200, html);
}
});
View
@@ -1,13 +1,13 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Network Problems</title>
- <link rel="stylesheet" href="/styles/style.css" type="text/css" media="screen" charset="utf-8" />
- </head>
- <body>
- <h2 id="title">Sorry, network problems!</h2>
- <p id="team">by <a href="http://github.com/mountain/shortify">Shortify</a></p>
- </body>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= lang %>" lang="<%= lang %>" dir="<%= dir %>">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title><%= msg[lang]['problem'] %></title>
+ <link rel="stylesheet" href="/styles/style.css" type="text/css" media="screen" charset="utf-8" />
+</head>
+<body>
+ <h2 id="title"><%= msg[lang]['problem'] %></h2>
+ <p id="team">by <a href="http://github.com/mountain/shortify">Shortify</a></p>
+</body>
</html>
View
@@ -7,35 +7,40 @@ exports.i18n = {
lable: 'Název: ',
wiki: 'Wiki',
shorten: 'Zkrátit!',
- notfound: 'Článek nebyl nalezen!'
+ notfound: 'Článek nebyl nalezen!',
+ problem: 'Sorry, problem occur!'
},
en: {
title: 'Wikipedia Shorturl Service',
lable: 'Title: ',
wiki: 'Wiki',
shorten: 'Shorten It!',
- notfound: 'Article was not found!'
+ notfound: 'Article was not found!',
+ problem: 'Sorry, problem occur!'
},
ml: {
title: 'വിക്കിപീഡിയ ചെറിയ യു.ആർ.എൽ. സേവനം',
lable: 'തലക്കെട്ട്: ',
wiki: 'വിക്കി',
shorten: 'ചെറുതാക്കൂ!',
- notfound: 'ലേഖനം കണ്ടെത്താനായില്ല!'
+ notfound: 'ലേഖനം കണ്ടെത്താനായില്ല!',
+ problem: 'Sorry, problem occur!'
},
he: {
title: 'שירות קיצור הכתובת של ויקיפדיה',
lable: 'שם הדף: ',
wiki: 'ויקי',
shorten: 'לקצר!',
- notfound: 'הדף לא נמצא!'
+ notfound: 'הדף לא נמצא!',
+ problem: 'Sorry, problem occur!'
},
zh: {
title: '维基缩址服务',
lable: '标题: ',
wiki: '维基',
shorten: '缩址!',
- notfound: '未找到条目!'
+ notfound: '未找到条目!',
+ problem: '抱歉,程序出现问题!'
}
}
};
View
@@ -1,5 +1,6 @@
{
"host": "localhost",
"port": 8080,
- "admin": ADMINEMAIL
+ "admin": ADMINEMAIL,
+ "cacheSize": 1000
}
View
@@ -20,7 +20,8 @@ exports.start = function(settings) {
_.extend(env, settings);
- env.cache = new (require('../lib/cache').Cache)(1000);
+ var cacheSize = env.cacheSize || 1000;
+ env.cache = new (require('../lib/cache').Cache)(cacheSize);
require('./template').load(env);

0 comments on commit a86d822

Please sign in to comment.