Hone logo
Hone
Problems

Implementing Remote Caching in an Angular Application

Remote caching is a powerful technique for improving application performance and reducing server load by storing frequently accessed data on a remote server. This challenge asks you to implement a basic remote caching mechanism in an Angular application, allowing data to be fetched from a remote source and cached locally, with subsequent requests served from the cache. This is particularly useful for data that doesn't change frequently and is accessed often.

Problem Description

You need to create an Angular service called RemoteCacheService that handles fetching data from a remote API and caching it. The service should provide a method getData(url: string) that accepts a URL as input. This method should:

  1. Check the Cache: First, check if the data for the given URL is already present in the cache (a simple in-memory object).
  2. Fetch from API (if not cached): If the data is not in the cache, fetch it from the provided URL using the HttpClient.
  3. Store in Cache: After successfully fetching the data, store it in the cache using the URL as the key.
  4. Return Data: Return the fetched data (or the cached data if it was already present).
  5. Handle Errors: Implement error handling for API requests. If an error occurs during the API call, return an error object (e.g., { error: 'Failed to fetch data' }).

The cache should be cleared when the application is destroyed (e.g., by implementing OnDestroy in the service).

Key Requirements:

  • Use Angular's HttpClient for making API requests.
  • Implement a simple in-memory cache.
  • Handle API errors gracefully.
  • Clear the cache on service destruction.
  • The service should be injectable into other Angular components.

Expected Behavior:

  • The first time getData(url) is called for a specific URL, it should fetch data from the API and store it in the cache.
  • Subsequent calls to getData(url) with the same URL should return the cached data without making another API request.
  • Error handling should prevent the application from crashing if the API request fails.
  • The cache should be cleared when the RemoteCacheService instance is destroyed.

Edge Cases to Consider:

  • What happens if the API returns an error?
  • How to handle different data types returned by the API (e.g., JSON, text)? Assume JSON for this challenge.
  • How to manage the cache size (for simplicity, no size limit is required in this challenge, but consider it for future improvements).

Examples

Example 1:

Input: url = 'https://jsonplaceholder.typicode.com/todos/1'
Output: { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
Explanation: The API is called, data is fetched, and stored in the cache.

Example 2:

Input: url = 'https://jsonplaceholder.typicode.com/todos/1' (called again after the first call)
Output: { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
Explanation: The data is retrieved from the cache, and the API is not called again.

Example 3: (Error Handling)

Input: url = 'https://jsonplaceholder.typicode.com/todos/invalid-url'
Output: { error: 'Failed to fetch data' }
Explanation: The API call fails, and the error handling mechanism returns an error object.

Constraints

  • API Response: Assume the API always returns JSON data.
  • Cache Size: No limit on the cache size for this challenge.
  • Performance: Focus on correctness and clarity over extreme performance optimization. The in-memory cache is sufficient for this exercise.
  • Angular Version: Assume Angular 14 or later.

Notes

  • Consider using RxJS operators like first or take to ensure that only the first successful response is cached.
  • You can use a simple JavaScript object as the in-memory cache.
  • Think about how to handle potential race conditions if multiple components are trying to access the cache simultaneously (this is a more advanced consideration and not required for this challenge).
  • The focus is on demonstrating the core concepts of remote caching: fetching, caching, and retrieving data.
Loading editor...
typescript