Skip to content

Commit 7e35c32

Browse files
committed
Cache the entire response when no param values are present. Clear cache when updated via API.
1 parent 7313c84 commit 7e35c32

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

src/client/components/carousel.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import h from 'react-hyperscript';
33
import { Component } from 'react';
44
import _ from 'lodash';
55
import { formatDistanceToNow } from 'date-fns';
6-
import queryString from 'query-string';
76
import { makeClassList } from '../dom';
87

9-
import Document from '../../model/document';
108

119
export const CAROUSEL_CONTENT = {
1210
FIGURE: 'figure',
@@ -17,8 +15,6 @@ export class Carousel extends Component {
1715
constructor(props){
1816
super(props);
1917

20-
const DOCUMENT_STATUS_FIELDS = Document.statusFields();
21-
2218
this.state = {
2319
pagerLeftAvailable: false,
2420
pagerRightAvailable: false,
@@ -27,11 +23,7 @@ export class Carousel extends Component {
2723
content: props.content,
2824
refresh: props.refresh || (() => { // get all docs from service by default
2925
const url = `/api/document`;
30-
const params = queryString.stringify({
31-
status: [DOCUMENT_STATUS_FIELDS.PUBLIC].join(','),
32-
limit: 20
33-
});
34-
const doFetch = () => fetch(`${url}?${params}`);
26+
const doFetch = () => fetch(url);
3527
const toJson = res => res.json();
3628

3729
return tryPromise(doFetch).then(toJson);

src/server/routes/api/document/index.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ http.get('/statistics', function( req, res, next ){
11271127
*/
11281128

11291129

1130-
function getDocuments({ limit = 20, offset = 0, status, apiKey, ids }){
1130+
function getDocuments({ limit = 20, offset = 0, status = [ DOCUMENT_STATUS_FIELDS.PUBLIC ], apiKey, ids }){
11311131
let tables, total;
11321132

11331133
return (
@@ -1210,6 +1210,11 @@ function getDocuments({ limit = 20, offset = 0, status, apiKey, ids }){
12101210
);
12111211
}
12121212

1213+
1214+
const docCache = new LRUCache({
1215+
max: DOCUMENT_IMAGE_CACHE_SIZE
1216+
});
1217+
12131218
/**
12141219
* @swagger
12151220
*
@@ -1260,26 +1265,43 @@ function getDocuments({ limit = 20, offset = 0, status, apiKey, ids }){
12601265
* items:
12611266
* $ref: '#/components/Document'
12621267
*/
1263-
http.get('/', function( req, res, next ){
1264-
let limit = _.toInteger( _.get( req.query, 'limit', 50 ) );
1265-
let offset = _.toInteger( _.get( req.query, 'offset', 0 ) );
1266-
let apiKey = _.get( req.query, 'apiKey' );
1268+
http.get('/', async function( req, res, next ){
1269+
let docJSON, count;
1270+
const DOC_KEY = 'documents';
12671271

1268-
let ids = req.query.ids ? req.query.ids.split(/\s*,\s*/) : null;
1272+
const csv2Array = par => _.uniq( _.compact( par.split(/\s*,\s*/) ) );
1273+
const toInteger = par => parseInt( par );
12691274

1270-
let status;
1271-
if( _.has( req.query, 'status' ) ){
1272-
status = _.compact( req.query.status.split(/\s*,\s*/) );
1275+
let { limit, offset, apiKey, status, ids } = req.query;
1276+
const hasQuery = limit || offset || apiKey || status || ids;
1277+
if( limit ) limit = toInteger( limit );
1278+
if( offset ) offset = toInteger( offset );
1279+
if( ids ) ids = csv2Array( ids );
1280+
if( status ) status = csv2Array( status );
1281+
1282+
try {
1283+
if( hasQuery ){
1284+
const { total, results } = await getDocuments({ limit, offset, apiKey, status, ids });
1285+
count = total;
1286+
docJSON = results;
1287+
1288+
} else {
1289+
let hasValues = docCache.has( DOC_KEY );
1290+
if( !hasValues ){
1291+
let { total, results } = await getDocuments({});
1292+
docCache.set( DOC_KEY, { total, results } );
1293+
}
1294+
let { total, results } = docCache.get( DOC_KEY );
1295+
count = total;
1296+
docJSON = results;
1297+
}
1298+
res.set({ 'X-Document-Count': count });
1299+
res.json( docJSON );
1300+
1301+
} catch( err ) {
1302+
next( err );
12731303
}
12741304

1275-
return (
1276-
tryPromise( () => getDocuments({ limit, offset, status, apiKey, ids }) )
1277-
.then( ({ total, results }) => {
1278-
res.set({ 'X-Document-Count': total });
1279-
res.json( results );
1280-
})
1281-
.catch( next )
1282-
);
12831305
});
12841306

12851307
/**
@@ -2101,6 +2123,7 @@ http.patch('/:id/:secret', function( req, res, next ){
21012123
};
21022124

21032125
const onDocPublic = async doc => {
2126+
docCache.reset();
21042127
await AdminPapersQueue.addJob( async () => {
21052128
await updateRelatedPapers( doc );
21062129
await sendFollowUpNotification( doc );

0 commit comments

Comments
 (0)