Hone logo
Hone
Problems

Implementing a Build Cache for Faster Vue Development

Build caches significantly reduce development time by storing the results of previous builds. This challenge asks you to implement a simple build cache mechanism within a Vue component, allowing it to reuse previously computed data instead of re-running expensive calculations. This is particularly useful for components with complex data transformations or API calls.

Problem Description

You need to create a Vue component that utilizes a build cache to optimize data fetching and processing. The component should:

  1. Fetch Data: Simulate fetching data from an API (using setTimeout to mimic network latency). The data should be an array of objects, each with a unique id and a name property.
  2. Process Data: Transform the fetched data by adding a new property called processedName to each object. This processing should be computationally expensive (simulated by a simple string manipulation).
  3. Cache Results: Implement a cache that stores the processed data based on a unique key derived from the fetched data. Before processing the data, check the cache. If the data is already cached, return the cached result. Otherwise, fetch, process, store in the cache, and return.
  4. Re-fetch on Change: If the fetched data changes (e.g., a different API endpoint is used), the cache should be invalidated, and the data should be re-fetched and re-processed. For simplicity, we'll simulate a change by passing a cacheKey prop to the component. Different cacheKey values will force a cache miss.
  5. Display Data: Display the processed data in a simple list.

Expected Behavior:

  • The first time the component is rendered (or when cacheKey changes), the data should be fetched, processed, and displayed.
  • Subsequent renders with the same cacheKey should return the cached processed data almost instantly, without re-fetching or re-processing.
  • The component should handle potential errors during data fetching gracefully (though error handling is not the primary focus of this challenge).

Examples

Example 1:

Input: cacheKey = "initialCache"
Output: A list of processed data items (e.g., [ { id: 1, name: "Alice", processedName: "ALICE-processed" }, { id: 2, name: "Bob", processedName: "BOB-processed" } ])
Explanation: Data is fetched, processed, and cached.

Example 2:

Input: cacheKey = "initialCache" (rendered immediately after Example 1)
Output: The same list of processed data items as in Example 1.
Explanation: Data is retrieved from the cache, bypassing the fetch and processing steps.

Example 3:

Input: cacheKey = "newCache"
Output: A list of processed data items (potentially different from Example 1).
Explanation: The cache is invalidated due to the new cacheKey, so data is fetched, processed, and cached again.

Constraints

  • Data Size: The fetched data array will contain a maximum of 10 items.
  • Processing Time: The processedName calculation should take a noticeable amount of time (e.g., 50-100ms) to simulate a computationally expensive operation.
  • Cache Storage: The cache should be a simple JavaScript object.
  • TypeScript: The solution must be written in TypeScript.
  • Vue 3: The solution must use Vue 3.

Notes

  • Consider using ref from Vue to manage reactive data.
  • The cacheKey prop is crucial for controlling cache invalidation.
  • The simulated API call uses setTimeout. Be mindful of asynchronous operations and how they interact with the cache.
  • Focus on the caching logic and its impact on performance. Styling and complex UI are not required.
  • The "expensive" processing can be a simple string manipulation like converting to uppercase and adding "-processed".
  • Think about how to generate a unique cache key based on the fetched data. A simple string representation might suffice for this challenge.
Loading editor...
typescript