generate functionMon, 05 Dec 2022

You have a client service which loads resources from a remote place, but you want to optimize it and decided to cache the results. Your task is to implement a class for caching the results of the loaded resources. You are given the ResourceManager class with load() - an asynchronous method for loading data (it returns a Promise). Your task is to implement the CachingResourceManager class. It takes a ResourceManager instance in its constructor as a dependency and should implement two methods: - get(id) - an async method that takes an id and returns a Promise with the resource. The resource must be returned directly from cache or loaded using ResourceManager (first trying to check in cache, then calling resource manager if not found in cache). - invalidateCache() - a synchronous method that should immediately invalidate all cache values. If some load functions are being processed, their results shouldn't be placed in cache (for more information, check the examples in the file asyncTests.js).

class ResourceManager { constructor() {} load(id) { return new Promise((res, rej) => { res(id); }); } } class CachingResourceManager { constructor(resourceManager) { // write your code here } async get(id) { // write your code here } invalidateCache() { // write your code here } }

Generate More

Questions about programming?Chat with your personal AI assistant