@@ -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