Skip to content

Commit

Permalink
feat: Add ability to clear user abilities cache (closes #888)
Browse files Browse the repository at this point in the history
  • Loading branch information
claustres committed Jun 24, 2024
1 parent 84a656d commit 6c6aa51
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
13 changes: 11 additions & 2 deletions core/api/services/authorisations/authorisations.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import LruCache from 'lru-cache'
import { LRUCache } from 'lru-cache'
import makeDebug from 'debug'
import { defineAbilities } from '../../../common/permissions.js'

Expand Down Expand Up @@ -92,7 +92,11 @@ export default {
const config = app.get('authorisation')
if (config && config.cache) {
// Store abilities of the N most active users in LRU cache (defaults to 1000)
this.cache = new LruCache(config.cache.maxUsers || 1000)
const max = config.cache.maxUsers || 1000
// LRU cache lib switched from positional parameters to options object at some point
// so that now we directly pass the options to it while before we used the max argument
if (!config.cache.max && !config.cache.ttl && !config.cache.maxSize) config.cache.max = max
this.cache = new LRUCache(config.cache)
debug('Using LRU cache for user abilities')
} else {
debug('Do not use LRU cache for user abilities')
Expand Down Expand Up @@ -135,5 +139,10 @@ export default {

const abilities = await this.getAbilities(subject)
return abilities
},

// Clear abilities
clearAbilities() {
if (this.cache) this.cache.clear()
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"jsdap": "^8.1.0",
"limiter": "^1.1.3",
"lodash": "^4.17.15",
"lru-cache": "^5.1.1",
"lru-cache": "^10.2.2",
"moment": "^2.24.0",
"mongodb": "^3.6.2",
"node-fetch": "^2.6.0",
Expand Down
17 changes: 5 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6355,6 +6355,11 @@ loupe@^2.3.1:
dependencies:
get-func-name "^2.0.0"

lru-cache@^10.2.2:
version "10.2.2"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==

lru-cache@^4.0.1:
version "4.1.5"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd"
Expand All @@ -6363,13 +6368,6 @@ lru-cache@^4.0.1:
pseudomap "^1.0.2"
yallist "^2.1.2"

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==
dependencies:
yallist "^3.0.2"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
Expand Down Expand Up @@ -9041,11 +9039,6 @@ yallist@^2.1.2:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
integrity sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==

yallist@^3.0.2:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==

yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
Expand Down

0 comments on commit 6c6aa51

Please sign in to comment.