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

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.0",
3
+ "version": "19.11.4-cache.2",
4
4
  "description": "Central ledger hosted by a scheme to record and settle transfers",
5
5
  "license": "Apache-2.0",
6
6
  "author": "ModusBox",
@@ -44,7 +44,7 @@
44
44
  "lint": "npx standard",
45
45
  "lint:fix": "npx standard --fix",
46
46
  "test": "npm run test:unit:spec",
47
- "test:unit": "npx tape 'test/unit/**/*.test.js'",
47
+ "test:unit": "tape 'test/unit/**/*.test.js'",
48
48
  "test:unit:spec": "npm run test:unit | tap-spec",
49
49
  "test:unit:distLock": "npx tape 'test/unit/lib/distLock/**/*.test.js'",
50
50
  "test:unit:participants-routes": "npx tape test/unit/api/participants/routes.test.js",
@@ -85,6 +85,7 @@
85
85
  "dependencies": {
86
86
  "@hapi/basic": "7.0.2",
87
87
  "@hapi/catbox-memory": "6.0.2",
88
+ "@hapi/catbox": "12.1.1",
88
89
  "@hapi/good": "9.0.1",
89
90
  "@hapi/hapi": "21.4.4",
90
91
  "@hapi/inert": "7.1.0",
@@ -138,7 +139,7 @@
138
139
  "jsonpath": "1.1.1",
139
140
  "mock-knex": "0.4.13",
140
141
  "nodemon": "3.1.11",
141
- "npm-check-updates": "19.1.2",
142
+ "npm-check-updates": "19.2.0",
142
143
  "nyc": "17.1.0",
143
144
  "pre-commit": "1.2.2",
144
145
  "proxyquire": "2.1.3",
package/src/lib/cache.js CHANGED
@@ -1,41 +1,30 @@
1
1
  'use strict'
2
2
 
3
3
  const CatboxMemory = require('@hapi/catbox-memory')
4
+ const Catbox = require('@hapi/catbox')
4
5
  const Config = require('../lib/config')
5
6
 
6
- let enabled = true
7
- let ttl
8
- let catboxMemoryClient = null
7
+ const expiresIn = parseInt(Config.CACHE_CONFIG.EXPIRES_IN_MS)
8
+ // Init memory client
9
+ const catboxMemoryClient = new CatboxMemory.Engine({ maxByteSize: Config.CACHE_CONFIG.MAX_BYTE_SIZE })
10
+ catboxMemoryClient.start()
9
11
 
10
12
  class CacheClient {
11
- constructor (meta) {
12
- this.meta = meta
13
- }
14
-
15
- getMeta () {
16
- return this.meta
17
- }
18
-
19
- createKey (id) {
20
- return {
21
- segment: this.meta.id,
22
- id
23
- }
13
+ constructor (segment, generateFunc) {
14
+ this.generateFunc = generateFunc
15
+ if (Config.CACHE_CONFIG.CACHE_ENABLED) this.policy = new Catbox.Policy({ generateFunc, expiresIn, generateTimeout: false }, catboxMemoryClient, segment)
24
16
  }
25
17
 
26
18
  get (key) {
27
- if (enabled) {
28
- return catboxMemoryClient.get(key)
29
- }
30
- return null
19
+ return Config.CACHE_CONFIG.CACHE_ENABLED ? this.policy.get(key) : this.generateFunc(key)
31
20
  }
32
21
 
33
- set (key, value) {
34
- return catboxMemoryClient.set(key, value, ttl)
22
+ drop (key) {
23
+ return this.policy?.drop(key)
35
24
  }
36
25
 
37
- drop (key) {
38
- catboxMemoryClient.drop(key)
26
+ setGenerateFunc (generateFunc) {
27
+ this.policy?.options({ generateFunc, expiresIn, generateTimeout: false })
39
28
  }
40
29
  }
41
30
 
@@ -50,32 +39,22 @@ class CacheClient {
50
39
  */
51
40
  let cacheClients = {}
52
41
 
53
- const registerCacheClient = (clientMeta) => {
54
- const newClient = new CacheClient(clientMeta)
55
- cacheClients[clientMeta.id] = newClient
42
+ const registerCacheClient = (id, generateFunc) => {
43
+ const newClient = new CacheClient(id, generateFunc)
44
+ cacheClients[id] = newClient
56
45
  return newClient
57
46
  }
58
47
 
59
48
  const initCache = async function () {
60
49
  // Read config
61
- ttl = parseInt(Config.CACHE_CONFIG.EXPIRES_IN_MS)
62
- enabled = Config.CACHE_CONFIG.CACHE_ENABLED
63
-
64
- // Init catbox.
65
- catboxMemoryClient = new CatboxMemory.Engine({
66
- maxByteSize: Config.CACHE_CONFIG.MAX_BYTE_SIZE
67
- })
68
- catboxMemoryClient.start()
69
-
70
- for (const clientId in cacheClients) {
71
- const clientMeta = cacheClients[clientId].getMeta()
72
- await clientMeta.preloadCache()
50
+ for (const client of Object.values(cacheClients)) {
51
+ await client.get('all') // preload cache
73
52
  }
74
53
  }
75
54
 
76
55
  const destroyCache = async function () {
77
- catboxMemoryClient?.stop()
78
- catboxMemoryClient = null
56
+ catboxMemoryClient.stop()
57
+ catboxMemoryClient.start()
79
58
  }
80
59
 
81
60
  const dropClients = function () {
@@ -83,7 +62,7 @@ const dropClients = function () {
83
62
  }
84
63
 
85
64
  const isCacheEnabled = function () {
86
- return enabled
65
+ return Config.CACHE_CONFIG.CACHE_ENABLED
87
66
  }
88
67
 
89
68
  module.exports = {
@@ -96,6 +75,6 @@ module.exports = {
96
75
  isCacheEnabled,
97
76
 
98
77
  // exposed for tests
99
- CatboxMemory,
78
+ Catbox,
100
79
  dropClients
101
80
  }
@@ -31,23 +31,17 @@ const Cache = require('../lib/cache')
31
31
  const Enums = require('./enum')
32
32
 
33
33
  let cacheClient
34
- let enumAllCacheKey
34
+ const enumAllCacheKey = 'all'
35
35
 
36
36
  /*
37
37
  Private API
38
38
  */
39
- const _getAllEnums = async function () {
40
- let allEnums
41
- const allEnumsFromCache = await cacheClient.get(enumAllCacheKey)
42
- if (allEnumsFromCache === null) {
43
- allEnums = {}
44
- for (const enumId of Enums.enumsIds) {
45
- allEnums[enumId] = await Enums[enumId]()
46
- }
47
- await cacheClient.set(enumAllCacheKey, allEnums)
48
- } else {
49
- // unwrap from catbox structure
50
- allEnums = allEnumsFromCache.item
39
+ const _getAllEnums = () => cacheClient.get(enumAllCacheKey)
40
+
41
+ const generateFunc = async function (key) {
42
+ const allEnums = {}
43
+ for (const enumId of Enums.enumsIds) {
44
+ allEnums[enumId] = await Enums[enumId]()
51
45
  }
52
46
  return allEnums
53
47
  }
@@ -65,13 +59,7 @@ exports.getEnums = async (id) => {
65
59
 
66
60
  exports.initialize = async () => {
67
61
  /* Register as cache client */
68
- const enumCacheClientMeta = {
69
- id: 'enum',
70
- preloadCache: _getAllEnums
71
- }
72
-
73
- cacheClient = Cache.registerCacheClient(enumCacheClientMeta)
74
- enumAllCacheKey = cacheClient.createKey('all')
62
+ cacheClient = Cache.registerCacheClient('enum', generateFunc)
75
63
  }
76
64
 
77
65
  exports.invalidateEnumCache = async () => {
@@ -26,13 +26,12 @@
26
26
  --------------
27
27
  **********/
28
28
 
29
- const Metrics = require('@mojaloop/central-services-metrics')
30
29
  const cache = require('../../lib/cache')
31
30
  const externalParticipantModel = require('./externalParticipant')
32
31
  const rethrow = require('../../shared/rethrow')
33
32
 
34
33
  let cacheClient
35
- let epAllCacheKey
34
+ const epAllCacheKey = 'all'
36
35
 
37
36
  const buildUnifiedCachedData = (allExternalParticipants) => {
38
37
  // build indexes - optimization for byId and byName access
@@ -52,28 +51,11 @@ const buildUnifiedCachedData = (allExternalParticipants) => {
52
51
  }
53
52
  }
54
53
 
55
- const getExternalParticipantsCached = async () => {
56
- const queryName = 'model_getExternalParticipantsCached'
57
- const histTimer = Metrics.getHistogram(
58
- 'model_externalParticipant',
59
- `${queryName} - Metrics for externalParticipant model`,
60
- ['success', 'queryName', 'hit']
61
- ).startTimer()
62
-
63
- let cachedParticipants = await cacheClient.get(epAllCacheKey)
64
- const hit = !!cachedParticipants
65
-
66
- if (!cachedParticipants) {
67
- const allParticipants = await externalParticipantModel.getAll()
68
- cachedParticipants = buildUnifiedCachedData(allParticipants)
69
- await cacheClient.set(epAllCacheKey, cachedParticipants)
70
- } else {
71
- // unwrap participants list from catbox structure
72
- cachedParticipants = cachedParticipants.item
73
- }
74
- histTimer({ success: true, queryName, hit })
54
+ const getExternalParticipantsCached = () => cacheClient.get(epAllCacheKey)
75
55
 
76
- return cachedParticipants
56
+ const generateFunc = async function (key) {
57
+ const allParticipants = await externalParticipantModel.getAll()
58
+ return buildUnifiedCachedData(allParticipants)
77
59
  }
78
60
 
79
61
  /*
@@ -81,13 +63,7 @@ const getExternalParticipantsCached = async () => {
81
63
  */
82
64
  const initialize = () => {
83
65
  /* Register as cache client */
84
- const cacheClientMeta = {
85
- id: 'externalParticipants',
86
- preloadCache: getExternalParticipantsCached
87
- }
88
-
89
- cacheClient = cache.registerCacheClient(cacheClientMeta)
90
- epAllCacheKey = cacheClient.createKey('all')
66
+ cacheClient = cache.registerCacheClient('externalParticipants', generateFunc)
91
67
  }
92
68
 
93
69
  const invalidateCache = async () => {
@@ -29,11 +29,10 @@
29
29
 
30
30
  const Cache = require('../../lib/cache')
31
31
  const ParticipantModel = require('../../models/participant/participant')
32
- const Metrics = require('@mojaloop/central-services-metrics')
33
32
  const rethrow = require('../../shared/rethrow')
34
33
 
35
34
  let cacheClient
36
- let participantsAllCacheKey
35
+ const participantsAllCacheKey = 'all'
37
36
 
38
37
  /*
39
38
  Private API
@@ -65,42 +64,18 @@ const buildUnifiedParticipantsData = (allParticipants) => {
65
64
  return unifiedParticipants
66
65
  }
67
66
 
68
- const getParticipantsCached = async () => {
69
- const histTimer = Metrics.getHistogram(
70
- 'model_participant',
71
- 'model_getParticipantsCached - Metrics for participant model',
72
- ['success', 'queryName', 'hit']
73
- ).startTimer()
74
- // Do we have valid participants list in the cache ?
75
- let cachedParticipants = await cacheClient.get(participantsAllCacheKey)
76
- if (!cachedParticipants) {
77
- // No participants in the cache, so fetch from participant API
78
- const allParticipants = await ParticipantModel.getAll()
79
- cachedParticipants = buildUnifiedParticipantsData(allParticipants)
80
-
81
- // store in cache
82
- await cacheClient.set(participantsAllCacheKey, cachedParticipants)
83
- histTimer({ success: true, queryName: 'model_getParticipantsCached', hit: false })
84
- } else {
85
- // unwrap participants list from catbox structure
86
- cachedParticipants = cachedParticipants.item
87
- histTimer({ success: true, queryName: 'model_getParticipantsCached', hit: true })
88
- }
89
- return cachedParticipants
67
+ const getParticipantsCached = () => cacheClient.get(participantsAllCacheKey)
68
+
69
+ const generateFunc = async function (key) {
70
+ const allParticipants = await ParticipantModel.getAll()
71
+ return buildUnifiedParticipantsData(allParticipants)
90
72
  }
91
73
 
92
74
  /*
93
75
  Public API
94
76
  */
95
77
  exports.initialize = async () => {
96
- /* Register as cache client */
97
- const participantCacheClientMeta = {
98
- id: 'participants',
99
- preloadCache: getParticipantsCached
100
- }
101
-
102
- cacheClient = Cache.registerCacheClient(participantCacheClientMeta)
103
- participantsAllCacheKey = cacheClient.createKey('all')
78
+ cacheClient = Cache.registerCacheClient('participants', generateFunc)
104
79
  }
105
80
 
106
81
  exports.invalidateParticipantsCache = async () => {
@@ -150,3 +125,5 @@ exports.create = withInvalidate('create')
150
125
  exports.update = withInvalidate('update')
151
126
  exports.destroyByName = withInvalidate('destroyByName')
152
127
  exports.destroyParticipantEndpointByParticipantId = withInvalidate('destroyParticipantEndpointByParticipantId')
128
+
129
+ exports.build = buildUnifiedParticipantsData
@@ -30,11 +30,10 @@
30
30
  const Cache = require('../../lib/cache')
31
31
  const Config = require('../../../src/lib/config')
32
32
  const ParticipantCurrencyModel = require('../../models/participant/participantCurrency')
33
- const Metrics = require('@mojaloop/central-services-metrics')
34
33
  const rethrow = require('../../shared/rethrow')
35
34
 
36
35
  let cacheClient
37
- let participantCurrencyAllCacheKey
36
+ const participantCurrencyAllCacheKey = 'all'
38
37
 
39
38
  /*
40
39
  Private API
@@ -62,28 +61,11 @@ const buildUnifiedParticipantsCurrencyData = (allCurrencyParticipants) => {
62
61
  return unifiedCurrencyParticipants
63
62
  }
64
63
 
65
- const getParticipantCurrencyCached = async () => {
66
- const histTimer = Metrics.getHistogram(
67
- 'model_participant',
68
- 'model_getParticipantCurrencyCached - Metrics for participant model',
69
- ['success', 'queryName', 'hit']
70
- ).startTimer()
71
- // Do we have valid participantsCurrency list in the cache ?
72
- let cachedCurrencyParticipants = await cacheClient.get(participantCurrencyAllCacheKey)
73
- if (!cachedCurrencyParticipants) {
74
- // No participantsCurrency in the cache, so fetch from participantsCurrency API
75
- const allCurrencyParticipants = await ParticipantCurrencyModel.getAll()
76
- cachedCurrencyParticipants = buildUnifiedParticipantsCurrencyData(allCurrencyParticipants)
77
-
78
- // store in cache
79
- await cacheClient.set(participantCurrencyAllCacheKey, cachedCurrencyParticipants)
80
- histTimer({ success: true, queryName: 'model_getParticipantCurrencyCached', hit: false })
81
- } else {
82
- // unwrap participants list from catbox structure
83
- cachedCurrencyParticipants = cachedCurrencyParticipants.item
84
- histTimer({ success: true, queryName: 'model_getParticipantCurrencyCached', hit: true })
85
- }
86
- return cachedCurrencyParticipants
64
+ const getParticipantCurrencyCached = () => cacheClient.get(participantCurrencyAllCacheKey)
65
+
66
+ const generateFunc = async function (key) {
67
+ const allCurrencyParticipants = await ParticipantCurrencyModel.getAll()
68
+ return buildUnifiedParticipantsCurrencyData(allCurrencyParticipants)
87
69
  }
88
70
 
89
71
  /*
@@ -91,13 +73,7 @@ const getParticipantCurrencyCached = async () => {
91
73
  */
92
74
  exports.initialize = async () => {
93
75
  /* Register as cache client */
94
- const participantCurrencyCacheClientMeta = {
95
- id: 'participantCurrency',
96
- preloadCache: getParticipantCurrencyCached
97
- }
98
-
99
- cacheClient = Cache.registerCacheClient(participantCurrencyCacheClientMeta)
100
- participantCurrencyAllCacheKey = cacheClient.createKey('all')
76
+ cacheClient = Cache.registerCacheClient('participantCurrency', generateFunc)
101
77
  }
102
78
 
103
79
  exports.invalidateParticipantCurrencyCache = async () => {
@@ -170,3 +146,4 @@ const withInvalidate = (theFunctionName) => {
170
146
  exports.create = withInvalidate('create')
171
147
  exports.update = withInvalidate('update')
172
148
  exports.destroyByParticipantId = withInvalidate('destroyByParticipantId')
149
+ exports.build = buildUnifiedParticipantsCurrencyData
@@ -29,11 +29,10 @@
29
29
 
30
30
  const Cache = require('../../lib/cache')
31
31
  const ParticipantLimitModel = require('../../models/participant/participantLimit')
32
- const Metrics = require('@mojaloop/central-services-metrics')
33
32
  const rethrow = require('../../shared/rethrow')
34
33
 
35
34
  let cacheClient
36
- let participantLimitAllCacheKey
35
+ const participantLimitAllCacheKey = 'all'
37
36
 
38
37
  /*
39
38
  Private API
@@ -61,28 +60,11 @@ const buildUnifiedParticipantsLimitData = (allLimitParticipants) => {
61
60
  return unifiedLimitParticipants
62
61
  }
63
62
 
64
- const getParticipantLimitCached = async () => {
65
- const histTimer = Metrics.getHistogram(
66
- 'model_participant',
67
- 'model_getParticipantLimitCached - Metrics for participant model',
68
- ['success', 'queryName', 'hit']
69
- ).startTimer()
70
- // Do we have valid participantsLimit list in the cache ?
71
- let cachedLimitParticipants = await cacheClient.get(participantLimitAllCacheKey)
72
- if (!cachedLimitParticipants) {
73
- // No participantsLimit in the cache, so fetch from participantsLimit API
74
- const allLimitParticipants = await ParticipantLimitModel.getAll()
75
- cachedLimitParticipants = buildUnifiedParticipantsLimitData(allLimitParticipants)
76
-
77
- // store in cache
78
- await cacheClient.set(participantLimitAllCacheKey, cachedLimitParticipants)
79
- histTimer({ success: true, queryName: 'model_getParticipantLimitCached', hit: false })
80
- } else {
81
- // unwrap participants list from catbox structure
82
- cachedLimitParticipants = cachedLimitParticipants.item
83
- histTimer({ success: true, queryName: 'model_getParticipantLimitCached', hit: true })
84
- }
85
- return cachedLimitParticipants
63
+ const getParticipantLimitCached = () => cacheClient.get(participantLimitAllCacheKey)
64
+
65
+ const generateFunc = async function (key) {
66
+ const allLimitParticipants = await ParticipantLimitModel.getAll()
67
+ return buildUnifiedParticipantsLimitData(allLimitParticipants)
86
68
  }
87
69
 
88
70
  /*
@@ -90,13 +72,7 @@ const getParticipantLimitCached = async () => {
90
72
  */
91
73
  exports.initialize = async () => {
92
74
  /* Register as cache client */
93
- const participantLimitCacheClientMeta = {
94
- id: 'participantLimit',
95
- preloadCache: getParticipantLimitCached
96
- }
97
-
98
- cacheClient = Cache.registerCacheClient(participantLimitCacheClientMeta)
99
- participantLimitAllCacheKey = cacheClient.createKey('all')
75
+ cacheClient = Cache.registerCacheClient('participantLimit', generateFunc)
100
76
  }
101
77
 
102
78
  exports.invalidateParticipantLimitCache = async () => {
@@ -125,3 +101,4 @@ exports.insert = withInvalidate('insert')
125
101
  exports.update = withInvalidate('update')
126
102
  exports.destroyByParticipantCurrencyId = withInvalidate('destroyByParticipantCurrencyId')
127
103
  exports.destroyByParticipantId = withInvalidate('destroyByParticipantId')
104
+ exports.build = buildUnifiedParticipantsLimitData
@@ -51,7 +51,7 @@ const getByParticipantPositionId = async (participantPositionId) => {
51
51
  const histTimer = Metrics.getHistogram(
52
52
  'model_participant',
53
53
  'model_getByParticipantPositionId - Metrics for participant model',
54
- ['success', 'queryName', 'hit']
54
+ ['success', 'queryName']
55
55
  ).startTimer()
56
56
  try {
57
57
  return await Db.from('participantPositionChange').query(async (builder) => {
@@ -60,11 +60,11 @@ const getByParticipantPositionId = async (participantPositionId) => {
60
60
  .select('participantPositionChange.*')
61
61
  .orderBy('participantPositionChangeId', 'desc')
62
62
  .first()
63
- histTimer({ success: true, queryName: 'model_getByParticipantPositionId', hit: false })
63
+ histTimer({ success: true, queryName: 'model_getByParticipantPositionId' })
64
64
  return result
65
65
  })
66
66
  } catch (err) {
67
- histTimer({ success: false, queryName: 'model_getByParticipantPositionId', hit: false })
67
+ histTimer({ success: false, queryName: 'model_getByParticipantPositionId' })
68
68
  rethrow.rethrowDatabaseError(err)
69
69
  }
70
70
  }
@@ -31,7 +31,6 @@
31
31
  'use strict'
32
32
 
33
33
  const Cache = require('../../lib/cache')
34
- const Metrics = require('@mojaloop/central-services-metrics')
35
34
  const BatchPositionModel = require('./batch')
36
35
  const rethrow = require('../../shared/rethrow')
37
36
 
@@ -70,27 +69,11 @@ const buildUnifiedParticipantCurrencyData = (allParticipantCurrency) => {
70
69
  return unifiedParticipantsCurrency
71
70
  }
72
71
 
73
- const getParticipantCurrencyCached = async (trx) => {
74
- const histTimer = Metrics.getHistogram(
75
- 'model_participant_batch',
76
- 'model_getParticipantsCached - Metrics for participant model',
77
- ['success', 'queryName', 'hit']
78
- ).startTimer()
79
- // Do we have valid participants list in the cache ?
80
- let cachedParticipantCurrency = await cacheClient.get(participantCurrencyAllCacheKey)
81
- if (!cachedParticipantCurrency) {
82
- const allParticipantCurrency = await BatchPositionModel.getAllParticipantCurrency(trx)
83
- cachedParticipantCurrency = buildUnifiedParticipantCurrencyData(allParticipantCurrency)
84
-
85
- // store in cache
86
- await cacheClient.set(participantCurrencyAllCacheKey, cachedParticipantCurrency)
87
- histTimer({ success: true, queryName: 'model_getParticipantCurrencyBatchCached', hit: false })
88
- } else {
89
- // unwrap participants list from catbox structure
90
- cachedParticipantCurrency = cachedParticipantCurrency.item
91
- histTimer({ success: true, queryName: 'model_getParticipantCurrencyBatchCached', hit: true })
92
- }
93
- return cachedParticipantCurrency
72
+ const getParticipantCurrencyCached = (trx) => cacheClient.get({ id: participantCurrencyAllCacheKey, trx })
73
+
74
+ const generateFunc = async function (key) {
75
+ const allParticipantCurrency = await BatchPositionModel.getAllParticipantCurrency(key.trx)
76
+ return buildUnifiedParticipantCurrencyData(allParticipantCurrency)
94
77
  }
95
78
 
96
79
  /*
@@ -98,13 +81,7 @@ const getParticipantCurrencyCached = async (trx) => {
98
81
  */
99
82
  exports.initialize = async () => {
100
83
  /* Register as cache client */
101
- const participantCurrencyCacheClientMeta = {
102
- id: 'participantCurrency',
103
- preloadCache: getParticipantCurrencyCached
104
- }
105
-
106
- cacheClient = Cache.registerCacheClient(participantCurrencyCacheClientMeta)
107
- participantCurrencyAllCacheKey = cacheClient.createKey('participantCurrency')
84
+ cacheClient = Cache.registerCacheClient('positionBatch', generateFunc)
108
85
  }
109
86
 
110
87
  exports.getParticipantCurrencyByIds = async (trx, participantCurrencyIds) => {
@@ -132,3 +109,5 @@ exports.getParticipantCurrencyByParticipantIds = async (trx, participantIds) =>
132
109
  rethrow.rethrowCachedDatabaseError(err)
133
110
  }
134
111
  }
112
+
113
+ exports.build = buildUnifiedParticipantCurrencyData
@@ -29,11 +29,10 @@
29
29
 
30
30
  const Cache = require('../../lib/cache')
31
31
  const SettlementModel = require('../../models/settlement/settlementModel')
32
- const Metrics = require('@mojaloop/central-services-metrics')
33
32
  const rethrow = require('../../shared/rethrow')
34
33
 
35
34
  let cacheClient
36
- let settlementModelsAllCacheKey
35
+ const settlementModelsAllCacheKey = 'all'
37
36
 
38
37
  /*
39
38
  Private API
@@ -63,28 +62,11 @@ const buildUnifiedSettlementModelsData = (allSettlementModels) => {
63
62
  return unifiedSettlementModels
64
63
  }
65
64
 
66
- const getSettlementModelsCached = async () => {
67
- const histTimer = Metrics.getHistogram(
68
- 'model_settlementModel',
69
- 'model_getSettlementModelsCached - Metrics for settlementModel model',
70
- ['success', 'queryName', 'hit']
71
- ).startTimer()
72
- // Do we have valid settlement models list in the cache ?
73
- let cachedSettlementModels = await cacheClient.get(settlementModelsAllCacheKey)
74
- if (!cachedSettlementModels) {
75
- // No settlement models in the cache, so fetch from participant API
76
- const allSettlementModels = await SettlementModel.getAll()
77
- cachedSettlementModels = buildUnifiedSettlementModelsData(allSettlementModels)
78
-
79
- // store in cache
80
- await cacheClient.set(settlementModelsAllCacheKey, cachedSettlementModels)
81
- histTimer({ success: true, queryName: 'model_getSettlementModelsCached', hit: false })
82
- } else {
83
- // unwrap settlement models list from catbox structure
84
- cachedSettlementModels = cachedSettlementModels.item
85
- histTimer({ success: true, queryName: 'model_getSettlementModelsCached', hit: true })
86
- }
87
- return cachedSettlementModels
65
+ const getSettlementModelsCached = () => cacheClient.get(settlementModelsAllCacheKey)
66
+
67
+ const generateFunc = async function (key) {
68
+ const allSettlementModels = await SettlementModel.getAll()
69
+ return buildUnifiedSettlementModelsData(allSettlementModels)
88
70
  }
89
71
 
90
72
  /*
@@ -92,13 +74,7 @@ const getSettlementModelsCached = async () => {
92
74
  */
93
75
  exports.initialize = async () => {
94
76
  /* Register as cache client */
95
- const settlementModelCacheClientMeta = {
96
- id: 'settlementModels',
97
- preloadCache: getSettlementModelsCached
98
- }
99
-
100
- cacheClient = Cache.registerCacheClient(settlementModelCacheClientMeta)
101
- settlementModelsAllCacheKey = cacheClient.createKey('all')
77
+ cacheClient = Cache.registerCacheClient('settlementModels', generateFunc)
102
78
  }
103
79
 
104
80
  exports.invalidateSettlementModelsCache = async () => {
@@ -140,3 +116,5 @@ exports.getAll = async () => {
140
116
  rethrow.rethrowCachedDatabaseError(err)
141
117
  }
142
118
  }
119
+
120
+ exports.build = buildUnifiedSettlementModelsData