Browse Source

Add redis cache to use with syzoj-renderer

master
Menci 6 years ago
parent
commit
c367a4763e
  1. 12
      app.js
  2. 1
      config-example.json
  3. 5
      libs/highlight.js
  4. 12
      libs/markdown.js
  5. 2
      package.json

12
app.js

@ -1,5 +1,6 @@
let fs = require('fs'), let fs = require('fs'),
path = require('path'); path = require('path'),
util = require('util');
const serializejs = require('serialize-javascript'); const serializejs = require('serialize-javascript');
const commandLineArgs = require('command-line-args'); const commandLineArgs = require('command-line-args');
@ -81,6 +82,15 @@ global.syzoj = {
if (!module.parent) { if (!module.parent) {
await this.lib('judger').connect(); await this.lib('judger').connect();
} }
// redis and redisCache is for syzoj-renderer
const redis = require('redis');
this.redis = redis.createClient(this.config.redis);
this.redisCache = {
get: util.promisify(this.redis.get).bind(this.redis),
set: util.promisify(this.redis.set).bind(this.redis)
};
this.loadModules(); this.loadModules();
}, },
async connectDatabase() { async connectDatabase() {

1
config-example.json

@ -111,6 +111,7 @@
], ],
"session_secret": "@SESSION_SECRET@", "session_secret": "@SESSION_SECRET@",
"rabbitMQ": "amqp://localhost/", "rabbitMQ": "amqp://localhost/",
"redis": "redis://127.0.0.1:6379",
"email_jwt_secret": "@EMAIL_JWT_SECRET@", "email_jwt_secret": "@EMAIL_JWT_SECRET@",
"google_analytics": "UA-XXXXXXXX-X", "google_analytics": "UA-XXXXXXXX-X",
"gravatar_url": "https://cdn.v2ex.com/gravatar", "gravatar_url": "https://cdn.v2ex.com/gravatar",

5
libs/highlight.js

@ -1,5 +1,6 @@
const { highlight } = require('syzoj-renderer'); const { highlight } = require('syzoj-renderer');
const objectHash = require('object-hash');
module.exports = (code, lang, cb) => { module.exports = async (code, lang, cb) => {
highlight(code, lang).then(cb); highlight(code, lang, syzoj.redisCache).then(cb);
} }

12
libs/markdown.js

@ -20,16 +20,6 @@ const xss = new XSS.FilterXSS({
} }
}); });
const defaultCache = {
data: {},
get(key) {
return this.data[key];
},
set(key, val) {
this.data[key] = val;
}
};
function filter(html) { function filter(html) {
html = xss.process(html); html = xss.process(html);
if (html) { if (html) {
@ -39,5 +29,5 @@ function filter(html) {
}; };
module.exports = (markdownCode, callback) => { module.exports = (markdownCode, callback) => {
markdown(markdownCode, defaultCache, filter).then(callback); markdown(markdownCode, syzoj.redisCache, filter).then(callback);
}; };

2
package.json

@ -49,7 +49,9 @@
"node-7z": "^0.4.0", "node-7z": "^0.4.0",
"nodemailer": "^4.7.0", "nodemailer": "^4.7.0",
"object-assign-deep": "^0.4.0", "object-assign-deep": "^0.4.0",
"object-hash": "^1.3.1",
"randomstring": "^1.1.5", "randomstring": "^1.1.5",
"redis": "^2.8.0",
"request": "^2.74.0", "request": "^2.74.0",
"request-promise": "^4.1.1", "request-promise": "^4.1.1",
"sendmail": "^1.1.1", "sendmail": "^1.1.1",

Loading…
Cancel
Save