From c367a4763ecd09e375ae2e35d5962ae65bcd9530 Mon Sep 17 00:00:00 2001 From: Menci Date: Fri, 22 Mar 2019 00:07:44 +0800 Subject: [PATCH] Add redis cache to use with syzoj-renderer --- app.js | 12 +++++++++++- config-example.json | 1 + libs/highlight.js | 5 +++-- libs/markdown.js | 12 +----------- package.json | 2 ++ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index a8eb5bd..7ac8114 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,6 @@ let fs = require('fs'), - path = require('path'); + path = require('path'), + util = require('util'); const serializejs = require('serialize-javascript'); const commandLineArgs = require('command-line-args'); @@ -81,6 +82,15 @@ global.syzoj = { if (!module.parent) { 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(); }, async connectDatabase() { diff --git a/config-example.json b/config-example.json index 72a8afc..ba7bcc8 100644 --- a/config-example.json +++ b/config-example.json @@ -111,6 +111,7 @@ ], "session_secret": "@SESSION_SECRET@", "rabbitMQ": "amqp://localhost/", + "redis": "redis://127.0.0.1:6379", "email_jwt_secret": "@EMAIL_JWT_SECRET@", "google_analytics": "UA-XXXXXXXX-X", "gravatar_url": "https://cdn.v2ex.com/gravatar", diff --git a/libs/highlight.js b/libs/highlight.js index 0239a35..e6feca5 100644 --- a/libs/highlight.js +++ b/libs/highlight.js @@ -1,5 +1,6 @@ const { highlight } = require('syzoj-renderer'); +const objectHash = require('object-hash'); -module.exports = (code, lang, cb) => { - highlight(code, lang).then(cb); +module.exports = async (code, lang, cb) => { + highlight(code, lang, syzoj.redisCache).then(cb); } diff --git a/libs/markdown.js b/libs/markdown.js index 3618d76..0666363 100644 --- a/libs/markdown.js +++ b/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) { html = xss.process(html); if (html) { @@ -39,5 +29,5 @@ function filter(html) { }; module.exports = (markdownCode, callback) => { - markdown(markdownCode, defaultCache, filter).then(callback); + markdown(markdownCode, syzoj.redisCache, filter).then(callback); }; diff --git a/package.json b/package.json index b9fce8f..2ae48e6 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,9 @@ "node-7z": "^0.4.0", "nodemailer": "^4.7.0", "object-assign-deep": "^0.4.0", + "object-hash": "^1.3.1", "randomstring": "^1.1.5", + "redis": "^2.8.0", "request": "^2.74.0", "request-promise": "^4.1.1", "sendmail": "^1.1.1",