@@ -14,6 +14,11 @@ const httpFetch = require('node-fetch')
1414const DEFAULT_ACL_SUFFIX = '.acl'
1515const ACL = rdf . Namespace ( 'http://www.w3.org/ns/auth/acl#' )
1616
17+ // TODO: expunge-on-write so that we can increase the caching time
18+ // For now this cache is a big performance gain but very simple
19+ const EXPIRY_MS = 10000 // 10 seconds
20+ let temporaryCache = { }
21+
1722// An ACLChecker exposes the permissions on a specific resource
1823class ACLChecker {
1924 constructor ( resource , options = { } ) {
@@ -189,7 +194,7 @@ class ACLChecker {
189194 * @return {Function } Returns a `fetch(uri, callback)` handler
190195 */
191196function fetchLocalOrRemote ( mapper , serverUri ) {
192- return async function fetch ( url , graph = rdf . graph ( ) ) {
197+ async function doFetch ( url ) {
193198 // Convert the URL into a filename
194199 let body , path , contentType
195200
@@ -208,7 +213,17 @@ function fetchLocalOrRemote (mapper, serverUri) {
208213 body = await response . text ( )
209214 contentType = response . headers . get ( 'content-type' )
210215 }
211-
216+ return { body, contentType }
217+ }
218+ return async function fetch ( url , graph = rdf . graph ( ) ) {
219+ if ( ! temporaryCache [ url ] || temporaryCache [ url ] . timestamp < new Date ( ) . getTime ( ) - EXPIRY_MS ) {
220+ debug ( temporaryCache [ url ] ? `Repopulating cache, content is ${ new Date ( ) . getTime ( ) - temporaryCache [ url ] . timestamp } ms old` : 'Populating cache' , url )
221+ temporaryCache [ url ] = {
222+ timestamp : new Date ( ) . getTime ( ) ,
223+ promise : doFetch ( url )
224+ }
225+ }
226+ const { body, contentType } = await temporaryCache [ url ] . promise
212227 // Parse the file as Turtle
213228 rdf . parse ( body , graph , url , contentType )
214229 return graph
@@ -222,3 +237,8 @@ function lastSlash (string, pos = string.length) {
222237
223238module . exports = ACLChecker
224239module . exports . DEFAULT_ACL_SUFFIX = DEFAULT_ACL_SUFFIX
240+
241+ // Used in the unit tests:
242+ module . exports . clearAclCache = function ( ) {
243+ temporaryCache = { }
244+ }
0 commit comments