Hone logo
Hone
Problems

Angular Build Cache Implementation

Building Angular applications can be time-consuming, especially for large projects. Implementing a build cache allows you to store the results of previous builds and reuse them when the inputs haven't changed, significantly reducing build times. This challenge asks you to create a simple build cache service that can store and retrieve build outputs based on a unique build key.

Problem Description

You need to implement an AngularBuildCacheService in TypeScript that provides functionality to store and retrieve build outputs. The service should accept a buildKey (a string representing the build configuration) and a buildOutput (any data type representing the build result). It should then allow retrieval of the buildOutput using the same buildKey. The cache should be in-memory for simplicity.

Key Requirements:

  • setCache(buildKey: string, buildOutput: any): Stores the buildOutput associated with the given buildKey.
  • getCache(buildKey: string): any: Retrieves the buildOutput associated with the given buildKey. Returns undefined if the key is not found in the cache.
  • clearCache(buildKey: string): void: Removes the entry associated with the given buildKey.
  • clearAllCache(): void: Removes all entries from the cache.

Expected Behavior:

  • The service should handle cases where a buildKey is already present in the cache (overwrite the existing value).
  • The service should gracefully handle cases where a buildKey is not found in the cache (return undefined from getCache).
  • The cache should be in-memory and not persistent across application restarts.

Edge Cases to Consider:

  • Empty buildKey strings. Should be handled gracefully (e.g., treated as a valid key, or throw an error - your choice, document your decision).
  • buildOutput of any data type.
  • Multiple calls to setCache with the same buildKey (should overwrite).
  • Calling getCache with a non-existent buildKey.
  • Calling clearCache with a non-existent buildKey.

Examples

Example 1:

Input:
  cacheService.setCache("production", { success: true, buildTime: 123 });
  cacheService.getCache("production");
Output: { success: true, buildTime: 123 }
Explanation: The build output for the "production" key is successfully retrieved.

Example 2:

Input:
  cacheService.setCache("development", { success: false, errors: ["error1", "error2"] });
  cacheService.getCache("staging");
Output: undefined
Explanation: The "staging" key does not exist in the cache, so undefined is returned.

Example 3:

Input:
  cacheService.setCache("production", { success: true, buildTime: 123 });
  cacheService.setCache("production", { success: true, buildTime: 456 });
  cacheService.getCache("production");
Output: { success: true, buildTime: 456 }
Explanation: The second call to setCache overwrites the previous value for the "production" key.

Constraints

  • The cache should be implemented using a simple in-memory data structure (e.g., a JavaScript object or Map).
  • The buildKey must be a string.
  • The buildOutput can be of any data type.
  • The service should be reasonably performant for typical Angular build scenarios (e.g., retrieval and storage should be fast). No need for complex optimizations, but avoid obviously inefficient operations.
  • The service should be stateless.

Notes

  • Consider using a Map for the cache to allow for more efficient key lookups.
  • Think about how you want to handle invalid or empty buildKey values. Document your approach.
  • This is a simplified build cache implementation. Real-world build caches often involve persistent storage (e.g., a file system or database) and more sophisticated caching strategies.
  • Focus on clarity, readability, and correctness of your code. Good error handling and documentation are also important.
  • You are not required to integrate this service into an Angular application; just implement the service itself.
Loading editor...
typescript