Added Cache file for tmdb queries and controller check the cache and returns it if hit. This is to limit our request to a third party library.

This commit is contained in:
2017-10-22 14:09:57 +02:00
parent b82efddeba
commit f57878813d
2 changed files with 58 additions and 4 deletions

View File

@@ -0,0 +1,44 @@
const assert = require('assert');
const establishedDatabase = require('src/database/database');
class Cache {
constructor(database) {
this.database = database || establishedDatabase
this.queries = {
'read': 'SELECT value, time_to_live, created_at, DATETIME("now", "localtime") as now, ' +
'DATETIME(created_at, "+" || time_to_live || " seconds") as expires ' +
'FROM cache WHERE key = ? AND now < expires',
'create': 'INSERT OR REPLACE INTO cache (key, value, time_to_live) VALUES (?, ?, ?)',
};
}
/**
* Retrieve an unexpired cache entry by key.
* @param {String} key of the cache entry
* @returns {Object}
*/
get(key) {
return Promise.resolve()
.then(() => this.database.get(this.queries.read, [key]))
.then((row) => {
assert(row, 'Could not find cache enrty with that key.');
return JSON.parse(row.value);
})
}
/**
* Insert cache entry with key and value.
* @param {String} key of the cache entry
* @param {String} value of the cache entry
* @param {Number} timeToLive the number of seconds before entry expires
* @returns {Object}
*/
set(key, value, timeToLive = 60) {
const json = JSON.stringify(value);
return Promise.resolve()
.then(() => this.database.run(this.queries.create, [key, json, timeToLive]))
.then(() => value);
}
}
module.exports = Cache;

View File

@@ -1,6 +1,10 @@
const SearchHistory = require('src/searchHistory/searchHistory');
const configuration = require('src/config/configuration').getInstance(); const configuration = require('src/config/configuration').getInstance();
const Cache = require('src/tmdb/cache');
const TMDB = require('src/tmdb/tmdb'); const TMDB = require('src/tmdb/tmdb');
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const cache = new Cache();
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
const searchHistory = new SearchHistory();
/** /**
* Controller: Search for movies by query, page and optionally adult * Controller: Search for movies by query, page and optionally adult
@@ -8,10 +12,17 @@ const tmdb = new TMDB(configuration.get('tmdb', 'apiKey'));
* @param {Response} res * @param {Response} res
* @returns {Callback} * @returns {Callback}
*/ */
function searchMoviesController(req, res) { function searchMediaController(req, res) {
const user = req.loggedInUser;
const { query, page, type } = req.query; const { query, page, type } = req.query;
Promise.resolve() Promise.resolve()
.then(() => {
if (user) {
return searchHistory.create(user, query);
}
return null;
}
.then(() => tmdb.search(query, page, type)) .then(() => tmdb.search(query, page, type))
.then((movies) => { .then((movies) => {
if (movies !== undefined || movies.length > 0) { if (movies !== undefined || movies.length > 0) {
@@ -19,11 +30,10 @@ function searchMoviesController(req, res) {
} else { } else {
res.status(404).send({ success: false, error: 'Search query did not return any results.'}) res.status(404).send({ success: false, error: 'Search query did not return any results.'})
} }
res.send(movies);
}) })
.catch((error) => { .catch((error) => {
res.status(500).send({ success: false, error: error.message }); res.status(500).send({ success: false, error: error.message });
}); });
} }
module.exports = searchMoviesController; module.exports = searchMediaController;