-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): replaces old learning content cache by new one
- Loading branch information
Showing
14 changed files
with
120 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
api/src/shared/domain/usecases/init-learning-content-cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
const initLearningContentCache = async function ({ LearningContentCache }) { | ||
await LearningContentCache.instance.get(); | ||
}; | ||
export { initLearningContentCache }; | ||
export async function initLearningContentCache() { | ||
// FIXME | ||
} |
86 changes: 35 additions & 51 deletions
86
api/src/shared/infrastructure/caches/learning-content-cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,53 @@ | ||
import { config } from '../../config.js'; | ||
import { lcmsClient } from '../lcms-client.js'; | ||
import { DistributedCache } from './DistributedCache.js'; | ||
import { InMemoryCache } from './InMemoryCache.js'; | ||
import { LayeredCache } from './LayeredCache.js'; | ||
import { RedisCache } from './RedisCache.js'; | ||
|
||
const LEARNING_CONTENT_CHANNEL = 'Learning content'; | ||
const LEARNING_CONTENT_CACHE_KEY = 'LearningContent'; | ||
import * as learningContentPubSub from '../caches/learning-content-pubsub.js'; | ||
|
||
export class LearningContentCache { | ||
constructor(redisUrl) { | ||
if (redisUrl) { | ||
const distributedCache = new DistributedCache(new InMemoryCache(), redisUrl, LEARNING_CONTENT_CHANNEL); | ||
const redisCache = new RedisCache(redisUrl); | ||
|
||
this._underlyingCache = new LayeredCache(distributedCache, redisCache); | ||
} else { | ||
this._underlyingCache = new InMemoryCache(); | ||
} | ||
this.generator = () => lcmsClient.getLatestRelease(); | ||
} | ||
|
||
get() { | ||
return this._underlyingCache.get(LEARNING_CONTENT_CACHE_KEY, this.generator); | ||
} | ||
#map; | ||
#pubSub; | ||
#name; | ||
|
||
async reset() { | ||
const object = await this.generator(); | ||
return this._underlyingCache.set(LEARNING_CONTENT_CACHE_KEY, object); | ||
} | ||
/** | ||
* @param {{ | ||
* name: string | ||
* pubSub: import('../caches/learning-content-pubsub.js').LearningContentPubSub | ||
* map: Map | ||
* }} config | ||
* @returns | ||
*/ | ||
constructor({ name, pubSub = learningContentPubSub.getPubSub(), map = new Map() }) { | ||
this.#name = name; | ||
this.#pubSub = pubSub; | ||
this.#map = map; | ||
|
||
async update() { | ||
const newLearningContent = await lcmsClient.createRelease(); | ||
return this._underlyingCache.set(LEARNING_CONTENT_CACHE_KEY, newLearningContent); | ||
this.#subscribe(); | ||
} | ||
|
||
patch(patch) { | ||
return this._underlyingCache.patch(LEARNING_CONTENT_CACHE_KEY, patch); | ||
get(key) { | ||
return this.#map.get(key); | ||
} | ||
|
||
flushAll() { | ||
return this._underlyingCache.flushAll(); | ||
set(key, value) { | ||
return this.#map.set(key, value); | ||
} | ||
|
||
quit() { | ||
return this._underlyingCache.quit(); | ||
delete(key) { | ||
return this.#pubSub.publish(this.#name, { type: 'delete', key }); | ||
} | ||
|
||
/** @type {LearningContentCache} */ | ||
static _instance = null; | ||
|
||
static defaultInstance() { | ||
return new LearningContentCache(config.caching.redisUrl); | ||
clear() { | ||
return this.#pubSub.publish(this.#name, { type: 'clear' }); | ||
} | ||
|
||
static get instance() { | ||
if (!this._instance) { | ||
this._instance = this.defaultInstance(); | ||
async #subscribe() { | ||
for await (const message of this.#pubSub.subscribe(this.#name)) { | ||
if (message.type === 'clear') this.#map.clear(); | ||
if (message.type === 'delete') this.#map.delete(this.message.key); | ||
} | ||
return this._instance; | ||
} | ||
|
||
static set instance(_instance) { | ||
this._instance = _instance; | ||
console.log('POUET'); | ||
} | ||
} | ||
|
||
export const learningContentCache = LearningContentCache.instance; | ||
export const learningContentCache = { | ||
async quit() { | ||
return learningContentPubSub.quit(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.