Skip to content
Browse files

Initial addition of HTML fragment caching via memcached.

  • Loading branch information...
1 parent e666dce commit 09bc861e9e14151174935a3e21a8d8f4ee24dc3f @robi42 committed May 26, 2010
Showing with 20 additions and 3 deletions.
  1. +4 −0 README.md
  2. +3 −0 app/config.js
  3. +8 −2 app/helpers.js
  4. BIN app/jars/memcached-2.5.jar
  5. +5 −1 app/model.js
View
4 README.md
@@ -8,6 +8,10 @@ Initially, create MySQL DB, e.g.:
mysql -uroot < app/config/init-mysql.sql
+Start memcached (in a separate shell session), e.g.:
+
+ memcached -vv
+
Now, to run the app launch ringo with the main script, e.g.:
ringo app/main.js
View
3 app/config.js
@@ -34,11 +34,14 @@ exports.authorName = 'Ringo User'; // Used by feeds.
exports.jars = [
'jars/mysql-connector-java-5.1.12-bin.jar',
+ 'jars/memcached-2.5.jar',
'jars/jdom-1.1.1.jar',
'jars/rome-1.0.jar'
];
exports.store = require('ringo/storage/hibernate');
+exports.memcached = {host: 'localhost', port: 11211};
+
exports.charset = 'UTF-8';
exports.contentType = 'text/html';
View
10 app/helpers.js
@@ -2,7 +2,7 @@ export('markdown_filter', 'archive_macro');
include('ringo/markdown');
require('core/array');
var {render} = require('ringo/skin');
-var {Post, months} = require('./model');
+var {Post, months, cache} = require('./model');
function markdown_filter(content) {
var markdown = new Markdown({
@@ -18,13 +18,17 @@ function markdown_filter(content) {
}
function archive_macro() {
+ var cached = cache.get('rl_archive_widget');
+ if (cached) {
+ return cached;
+ }
var years = Post.query().
orderBy('created desc').
select().
map(function (post) post.created.getFullYear()).
unique();
if (years.length) {
- return render('skins/archive.html', {
+ var widget = render('skins/archive.html', {
years: [{
value: year,
months: Post.query().
@@ -36,6 +40,8 @@ function archive_macro() {
unique()
} for each (year in years)]
});
+ cache.set('rl_archive_widget', 14 * 24 * 60 * 60, widget); // 2 weeks.
+ return widget;
}
}
View
BIN app/jars/memcached-2.5.jar
Binary file not shown.
View
6 app/model.js
@@ -1,6 +1,10 @@
-export('Post', 'queryPosts', 'months');
+export('Post', 'queryPosts', 'cache', 'months');
addToClasspath('./config');
include('ringo/markdown');
+var {memcached} = require('./config');
+
+var cache = cache || new net.spy.memcached.MemcachedClient(
+ new java.net.InetSocketAddress(memcached.host, memcached.port));
var months = [
'january', 'february', 'march', 'april', 'may', 'june', 'july',

0 comments on commit 09bc861

Please sign in to comment.
Something went wrong with that request. Please try again.