Skip to content

Commit 566ea86

Browse files
Add temporary cache in lib/acl-checker.js (#1467)
* Add temporary cache in lib/acl-checker.js * Set timeout to 10 seconds * Use Node 12 on Travis * reset acl cache in afterEach
1 parent 384b282 commit 566ea86

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "10"
4+
- "12"
55
- "lts/*"
66
- "node"
77

lib/acl-checker.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const httpFetch = require('node-fetch')
1414
const DEFAULT_ACL_SUFFIX = '.acl'
1515
const 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
1823
class ACLChecker {
1924
constructor (resource, options = {}) {
@@ -189,7 +194,7 @@ class ACLChecker {
189194
* @return {Function} Returns a `fetch(uri, callback)` handler
190195
*/
191196
function 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

223238
module.exports = ACLChecker
224239
module.exports.DEFAULT_ACL_SUFFIX = DEFAULT_ACL_SUFFIX
240+
241+
// Used in the unit tests:
242+
module.exports.clearAclCache = function () {
243+
temporaryCache = {}
244+
}

test/integration/acl-oidc-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const request = require('request')
44
const path = require('path')
55
const { loadProvider, rm, checkDnsSettings, cleanDir } = require('../utils')
66
const IDToken = require('@solid/oidc-op/src/IDToken')
7-
7+
const { clearAclCache } = require('../../lib/acl-checker')
88
const ldnode = require('../../index')
99

1010
const port = 7777
@@ -79,6 +79,10 @@ describe('ACL with WebID+OIDC over HTTP', function () {
7979
}).catch(console.error)
8080
})
8181

82+
afterEach(() => {
83+
clearAclCache()
84+
})
85+
8286
after(() => {
8387
if (ldpHttpsServer) ldpHttpsServer.close()
8488
cleanDir(rootPath)

0 commit comments

Comments
 (0)