@mojaloop/central-ledger 19.11.4-cache.2 → 19.11.4-cache.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojaloop/central-ledger",
3
- "version": "19.11.4-cache.2",
3
+ "version": "19.11.4-cache.3",
4
4
  "description": "Central ledger hosted by a scheme to record and settle transfers",
5
5
  "license": "Apache-2.0",
6
6
  "author": "ModusBox",
package/src/lib/cache.js CHANGED
@@ -6,55 +6,65 @@ const Config = require('../lib/config')
6
6
 
7
7
  const expiresIn = parseInt(Config.CACHE_CONFIG.EXPIRES_IN_MS)
8
8
  // Init memory client
9
- const catboxMemoryClient = new CatboxMemory.Engine({ maxByteSize: Config.CACHE_CONFIG.MAX_BYTE_SIZE })
10
- catboxMemoryClient.start()
9
+ let catboxMemoryClient
11
10
 
12
11
  class CacheClient {
13
- constructor (segment, generateFunc) {
12
+ constructor (segment, generateFunc, preloadCache) {
14
13
  this.generateFunc = generateFunc
15
- if (Config.CACHE_CONFIG.CACHE_ENABLED) this.policy = new Catbox.Policy({ generateFunc, expiresIn, generateTimeout: false }, catboxMemoryClient, segment)
14
+ this.segment = segment
15
+ this.preloadCache = preloadCache
16
+ }
17
+
18
+ async initCache (catboxMemoryClient) {
19
+ if (catboxMemoryClient) {
20
+ this.policy = new Catbox.Policy({
21
+ generateFunc: this.generateFunc,
22
+ expiresIn,
23
+ generateTimeout: false
24
+ },
25
+ catboxMemoryClient,
26
+ this.segment
27
+ )
28
+ }
29
+ return await this.preloadCache?.()
16
30
  }
17
31
 
18
32
  get (key) {
19
- return Config.CACHE_CONFIG.CACHE_ENABLED ? this.policy.get(key) : this.generateFunc(key)
33
+ return this.policy ? this.policy.get(key) : this.generateFunc(key)
20
34
  }
21
35
 
22
36
  drop (key) {
23
37
  return this.policy?.drop(key)
24
38
  }
25
-
26
- setGenerateFunc (generateFunc) {
27
- this.policy?.options({ generateFunc, expiresIn, generateTimeout: false })
28
- }
29
39
  }
30
40
 
31
- /*
32
- Each client should register itself during module load.
33
- The client meta should be:
34
- {
35
- id [MANDATORY]
36
- preloadCache() [OPTIONAL]
37
- this will be called to preload data
38
- }
39
- */
40
41
  let cacheClients = {}
41
42
 
42
- const registerCacheClient = (id, generateFunc) => {
43
- const newClient = new CacheClient(id, generateFunc)
43
+ const registerCacheClient = ({ id, preloadCache, generate }) => {
44
+ const newClient = new CacheClient(id, generate, preloadCache)
44
45
  cacheClients[id] = newClient
45
46
  return newClient
46
47
  }
47
48
 
48
49
  const initCache = async function () {
49
- // Read config
50
- for (const client of Object.values(cacheClients)) {
51
- await client.get('all') // preload cache
50
+ if (isCacheEnabled()) {
51
+ // Init catbox.
52
+ catboxMemoryClient = new CatboxMemory.Engine({
53
+ maxByteSize: Config.CACHE_CONFIG.MAX_BYTE_SIZE
54
+ })
55
+ await catboxMemoryClient.start()
56
+
57
+ // Init each registered cache client
58
+ for (const client of Object.values(cacheClients)) {
59
+ await client.initCache(catboxMemoryClient)
60
+ }
61
+ } else {
62
+ catboxMemoryClient = null
52
63
  }
53
64
  }
54
65
 
55
66
  const destroyCache = async function () {
56
- catboxMemoryClient.stop()
57
- catboxMemoryClient.start()
67
+ catboxMemoryClient?.stop()
58
68
  }
59
69
 
60
70
  const dropClients = function () {
@@ -75,6 +85,6 @@ module.exports = {
75
85
  isCacheEnabled,
76
86
 
77
87
  // exposed for tests
78
- Catbox,
88
+ CatboxMemory,
79
89
  dropClients
80
90
  }
@@ -38,7 +38,7 @@ const enumAllCacheKey = 'all'
38
38
  */
39
39
  const _getAllEnums = () => cacheClient.get(enumAllCacheKey)
40
40
 
41
- const generateFunc = async function (key) {
41
+ const generate = async function (key) {
42
42
  const allEnums = {}
43
43
  for (const enumId of Enums.enumsIds) {
44
44
  allEnums[enumId] = await Enums[enumId]()
@@ -59,7 +59,7 @@ exports.getEnums = async (id) => {
59
59
 
60
60
  exports.initialize = async () => {
61
61
  /* Register as cache client */
62
- cacheClient = Cache.registerCacheClient('enum', generateFunc)
62
+ cacheClient = Cache.registerCacheClient({ id: 'enum', generate, preloadCache: _getAllEnums })
63
63
  }
64
64
 
65
65
  exports.invalidateEnumCache = async () => {
@@ -53,7 +53,7 @@ const buildUnifiedCachedData = (allExternalParticipants) => {
53
53
 
54
54
  const getExternalParticipantsCached = () => cacheClient.get(epAllCacheKey)
55
55
 
56
- const generateFunc = async function (key) {
56
+ const generate = async function (key) {
57
57
  const allParticipants = await externalParticipantModel.getAll()
58
58
  return buildUnifiedCachedData(allParticipants)
59
59
  }
@@ -63,7 +63,7 @@ const generateFunc = async function (key) {
63
63
  */
64
64
  const initialize = () => {
65
65
  /* Register as cache client */
66
- cacheClient = cache.registerCacheClient('externalParticipants', generateFunc)
66
+ cacheClient = cache.registerCacheClient({ id: 'externalParticipants', generate, preloadCache: getExternalParticipantsCached })
67
67
  }
68
68
 
69
69
  const invalidateCache = async () => {
@@ -66,7 +66,7 @@ const buildUnifiedParticipantsData = (allParticipants) => {
66
66
 
67
67
  const getParticipantsCached = () => cacheClient.get(participantsAllCacheKey)
68
68
 
69
- const generateFunc = async function (key) {
69
+ const generate = async function (key) {
70
70
  const allParticipants = await ParticipantModel.getAll()
71
71
  return buildUnifiedParticipantsData(allParticipants)
72
72
  }
@@ -75,7 +75,7 @@ const generateFunc = async function (key) {
75
75
  Public API
76
76
  */
77
77
  exports.initialize = async () => {
78
- cacheClient = Cache.registerCacheClient('participants', generateFunc)
78
+ cacheClient = Cache.registerCacheClient({ id: 'participants', generate, preloadCache: getParticipantsCached })
79
79
  }
80
80
 
81
81
  exports.invalidateParticipantsCache = async () => {
@@ -63,7 +63,7 @@ const buildUnifiedParticipantsCurrencyData = (allCurrencyParticipants) => {
63
63
 
64
64
  const getParticipantCurrencyCached = () => cacheClient.get(participantCurrencyAllCacheKey)
65
65
 
66
- const generateFunc = async function (key) {
66
+ const generate = async function (key) {
67
67
  const allCurrencyParticipants = await ParticipantCurrencyModel.getAll()
68
68
  return buildUnifiedParticipantsCurrencyData(allCurrencyParticipants)
69
69
  }
@@ -73,7 +73,7 @@ const generateFunc = async function (key) {
73
73
  */
74
74
  exports.initialize = async () => {
75
75
  /* Register as cache client */
76
- cacheClient = Cache.registerCacheClient('participantCurrency', generateFunc)
76
+ cacheClient = Cache.registerCacheClient({ id: 'participantCurrency', generate, preloadCache: getParticipantCurrencyCached })
77
77
  }
78
78
 
79
79
  exports.invalidateParticipantCurrencyCache = async () => {
@@ -62,7 +62,7 @@ const buildUnifiedParticipantsLimitData = (allLimitParticipants) => {
62
62
 
63
63
  const getParticipantLimitCached = () => cacheClient.get(participantLimitAllCacheKey)
64
64
 
65
- const generateFunc = async function (key) {
65
+ const generate = async function (key) {
66
66
  const allLimitParticipants = await ParticipantLimitModel.getAll()
67
67
  return buildUnifiedParticipantsLimitData(allLimitParticipants)
68
68
  }
@@ -72,7 +72,7 @@ const generateFunc = async function (key) {
72
72
  */
73
73
  exports.initialize = async () => {
74
74
  /* Register as cache client */
75
- cacheClient = Cache.registerCacheClient('participantLimit', generateFunc)
75
+ cacheClient = Cache.registerCacheClient({ id: 'participantLimit', generate, preloadCache: getParticipantLimitCached })
76
76
  }
77
77
 
78
78
  exports.invalidateParticipantLimitCache = async () => {
@@ -71,7 +71,7 @@ const buildUnifiedParticipantCurrencyData = (allParticipantCurrency) => {
71
71
 
72
72
  const getParticipantCurrencyCached = (trx) => cacheClient.get({ id: participantCurrencyAllCacheKey, trx })
73
73
 
74
- const generateFunc = async function (key) {
74
+ const generate = async function (key) {
75
75
  const allParticipantCurrency = await BatchPositionModel.getAllParticipantCurrency(key.trx)
76
76
  return buildUnifiedParticipantCurrencyData(allParticipantCurrency)
77
77
  }
@@ -81,7 +81,7 @@ const generateFunc = async function (key) {
81
81
  */
82
82
  exports.initialize = async () => {
83
83
  /* Register as cache client */
84
- cacheClient = Cache.registerCacheClient('positionBatch', generateFunc)
84
+ cacheClient = Cache.registerCacheClient({ id: 'positionBatch', generate, preloadCache: getParticipantCurrencyCached })
85
85
  }
86
86
 
87
87
  exports.getParticipantCurrencyByIds = async (trx, participantCurrencyIds) => {
@@ -64,7 +64,7 @@ const buildUnifiedSettlementModelsData = (allSettlementModels) => {
64
64
 
65
65
  const getSettlementModelsCached = () => cacheClient.get(settlementModelsAllCacheKey)
66
66
 
67
- const generateFunc = async function (key) {
67
+ const generate = async function (key) {
68
68
  const allSettlementModels = await SettlementModel.getAll()
69
69
  return buildUnifiedSettlementModelsData(allSettlementModels)
70
70
  }
@@ -74,7 +74,7 @@ const generateFunc = async function (key) {
74
74
  */
75
75
  exports.initialize = async () => {
76
76
  /* Register as cache client */
77
- cacheClient = Cache.registerCacheClient('settlementModels', generateFunc)
77
+ cacheClient = Cache.registerCacheClient({ id: 'settlementModels', generate, preloadCache: getSettlementModelsCached })
78
78
  }
79
79
 
80
80
  exports.invalidateSettlementModelsCache = async () => {