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 thebuildOutputassociated with the givenbuildKey.getCache(buildKey: string): any: Retrieves thebuildOutputassociated with the givenbuildKey. Returnsundefinedif the key is not found in the cache.clearCache(buildKey: string): void: Removes the entry associated with the givenbuildKey.clearAllCache(): void: Removes all entries from the cache.
Expected Behavior:
- The service should handle cases where a
buildKeyis already present in the cache (overwrite the existing value). - The service should gracefully handle cases where a
buildKeyis not found in the cache (returnundefinedfromgetCache). - The cache should be in-memory and not persistent across application restarts.
Edge Cases to Consider:
- Empty
buildKeystrings. Should be handled gracefully (e.g., treated as a valid key, or throw an error - your choice, document your decision). buildOutputof any data type.- Multiple calls to
setCachewith the samebuildKey(should overwrite). - Calling
getCachewith a non-existentbuildKey. - Calling
clearCachewith a non-existentbuildKey.
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
buildKeymust be a string. - The
buildOutputcan 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
Mapfor the cache to allow for more efficient key lookups. - Think about how you want to handle invalid or empty
buildKeyvalues. 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.