summaryrefslogtreecommitdiff
path: root/lib/history.js
diff options
context:
space:
mode:
authorWu Cheng-Han2017-02-03 21:39:08 +0800
committerWu Cheng-Han2017-02-03 21:39:08 +0800
commit92ad67b813c3b940f619983c7daee7cdf21c3f4e (patch)
treed250ac6d0f67272f0d4c4eb257110c569b65e166 /lib/history.js
parenta261c8e812282feeccd15b769a3b074bf52bfc74 (diff)
Update to remove history cache to lower application coupling
Diffstat (limited to 'lib/history.js')
-rw-r--r--lib/history.js147
1 files changed, 47 insertions, 100 deletions
diff --git a/lib/history.js b/lib/history.js
index 2723422c..e7fb3087 100644
--- a/lib/history.js
+++ b/lib/history.js
@@ -1,7 +1,6 @@
//history
//external modules
var async = require('async');
-var moment = require('moment');
//core
var config = require("./config.js");
@@ -14,45 +13,32 @@ var History = {
historyGet: historyGet,
historyPost: historyPost,
historyDelete: historyDelete,
- isReady: isReady,
updateHistory: updateHistory
};
-var caches = {};
-//update when the history is dirty
-var updater = setInterval(function () {
- var deleted = [];
- async.each(Object.keys(caches), function (key, callback) {
- var cache = caches[key];
- if (cache.isDirty) {
- if (config.debug) logger.info("history updater found dirty history: " + key);
- var history = parseHistoryToArray(cache.history);
- cache.isDirty = false;
- finishUpdateHistory(key, history, function (err, count) {
- if (err) return callback(err, null);
- if (!count) return callback(null, null);
- cache.updateAt = Date.now();
- return callback(null, null);
- });
- } else {
- if (moment().isAfter(moment(cache.updateAt).add(5, 'minutes'))) {
- deleted.push(key);
- }
- return callback(null, null);
+function getHistory(userid, callback) {
+ models.User.findOne({
+ where: {
+ id: userid
}
- }, function (err) {
- if (err) return logger.error('history updater error', err);
+ }).then(function (user) {
+ if (!user)
+ return callback(null, null);
+ var history = {};
+ if (user.history)
+ history = parseHistoryToObject(JSON.parse(user.history));
+ if (config.debug)
+ logger.info('read history success: ' + user.id);
+ return callback(null, history);
+ }).catch(function (err) {
+ logger.error('read history failed: ' + err);
+ return callback(err, null);
});
- // delete specified caches
- for (var i = 0, l = deleted.length; i < l; i++) {
- caches[deleted[i]].history = {};
- delete caches[deleted[i]];
- }
-}, 1000);
+}
-function finishUpdateHistory(userid, history, callback) {
+function setHistory(userid, history, callback) {
models.User.update({
- history: JSON.stringify(history)
+ history: JSON.stringify(parseHistoryToArray(history))
}, {
where: {
id: userid
@@ -60,72 +46,27 @@ function finishUpdateHistory(userid, history, callback) {
}).then(function (count) {
return callback(null, count);
}).catch(function (err) {
+ logger.error('set history failed: ' + err);
return callback(err, null);
});
}
-function isReady() {
- var dirtyCount = 0;
- async.each(Object.keys(caches), function (key, callback) {
- if (caches[key].isDirty) dirtyCount++;
- return callback(null, null);
- }, function (err) {
- if (err) return logger.error('history ready check error', err);
- });
- return dirtyCount > 0 ? false : true;
-}
-
-function getHistory(userid, callback) {
- if (caches[userid]) {
- return callback(null, caches[userid].history);
- } else {
- models.User.findOne({
- where: {
- id: userid
- }
- }).then(function (user) {
- if (!user)
- return callback(null, null);
- var history = [];
- if (user.history)
- history = JSON.parse(user.history);
- if (config.debug)
- logger.info('read history success: ' + user.id);
- setHistory(userid, history);
- return callback(null, history);
- }).catch(function (err) {
- logger.error('read history failed: ' + err);
- return callback(err, null);
- });
- }
-}
-
-function setHistory(userid, history) {
- if (Array.isArray(history)) history = parseHistoryToObject(history);
- if (!caches[userid]) {
- caches[userid] = {
- history: {},
- isDirty: false,
- updateAt: Date.now()
- };
- }
- caches[userid].history = history;
-}
-
-function updateHistory(userid, noteId, document) {
+function updateHistory(userid, noteId, document, time) {
if (userid && noteId && typeof document !== 'undefined') {
getHistory(userid, function (err, history) {
if (err || !history) return;
- if (!caches[userid].history[noteId]) {
- caches[userid].history[noteId] = {};
+ if (!history[noteId]) {
+ history[noteId] = {};
}
- var noteHistory = caches[userid].history[noteId];
+ var noteHistory = history[noteId];
var noteInfo = models.Note.parseNoteInfo(document);
noteHistory.id = noteId;
noteHistory.text = noteInfo.title;
- noteHistory.time = moment().valueOf();
+ noteHistory.time = time || Date.now();
noteHistory.tags = noteInfo.tags;
- caches[userid].isDirty = true;
+ setHistory(userid, history, function (err, count) {
+ return;
+ });
});
}
}
@@ -175,9 +116,10 @@ function historyPost(req, res) {
return response.errorBadRequest(res);
}
if (Array.isArray(history)) {
- setHistory(req.user.id, history);
- caches[req.user.id].isDirty = true;
- res.end();
+ setHistory(req.user.id, history, function (err, count) {
+ if (err) return response.errorInternalError(res);
+ res.end();
+ });
} else {
return response.errorBadRequest(res);
}
@@ -186,11 +128,13 @@ function historyPost(req, res) {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
- if (!caches[req.user.id].history[noteId]) return response.errorNotFound(res);
+ if (!history[noteId]) return response.errorNotFound(res);
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
- caches[req.user.id].history[noteId].pinned = (req.body.pinned === 'true');
- caches[req.user.id].isDirty = true;
- res.end();
+ history[noteId].pinned = (req.body.pinned === 'true');
+ setHistory(req.user.id, history, function (err, count) {
+ if (err) return response.errorInternalError(res);
+ res.end();
+ });
} else {
return response.errorBadRequest(res);
}
@@ -205,16 +149,19 @@ function historyDelete(req, res) {
if (req.isAuthenticated()) {
var noteId = req.params.noteId;
if (!noteId) {
- setHistory(req.user.id, []);
- caches[req.user.id].isDirty = true;
- res.end();
+ setHistory(req.user.id, [], function (err, count) {
+ if (err) return response.errorInternalError(res);
+ res.end();
+ });
} else {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
- delete caches[req.user.id].history[noteId];
- caches[req.user.id].isDirty = true;
- res.end();
+ delete history[noteId];
+ setHistory(req.user.id, history, function (err, count) {
+ if (err) return response.errorInternalError(res);
+ res.end();
+ });
});
}
} else {