Hone logo
Hone
Problems

Cache Validation with Jest

Caching is a common optimization technique to reduce latency and improve application performance. However, cached data can become stale, leading to incorrect results. This challenge focuses on creating a utility function to validate cached data and writing Jest tests to ensure its correct behavior, particularly when dealing with expiration times.

Problem Description

You need to implement a validateCache function that checks if a cached value is still valid based on its creation timestamp and an expiration time. The function should accept the cached value, its timestamp (in milliseconds), and the expiration time (in milliseconds) as input. It should return true if the cache is valid (i.e., the current time is within the expiration window) and false otherwise.

Key Requirements:

  • The validateCache function must accurately determine cache validity based on the provided timestamp and expiration time.
  • The function should use Date.now() to get the current time.
  • The function should handle cases where the expiration time is zero (cache is immediately invalid).
  • The function should handle cases where the timestamp is in the future (cache is invalid).

Expected Behavior:

  • If the difference between Date.now() and the timestamp is less than or equal to the expiration time, the function should return true.
  • If the difference between Date.now() and the timestamp is greater than the expiration time, the function should return false.
  • If the expiration time is 0, the function should always return false.

Edge Cases to Consider:

  • Expiration time is zero.
  • Timestamp is in the future (e.g., due to clock skew).
  • Timestamp is very close to the current time (potential floating-point precision issues, though not a primary concern).
  • Large expiration times.

Examples

Example 1:

Input: { data: 'some data', timestamp: 1678886400000, expiration: 86400000 } // 1 day
Output: true
Explanation: Assuming current time is within 1 day of 1678886400000, the cache is valid.

Example 2:

Input: { data: 'some data', timestamp: 1678886400000, expiration: 0 }
Output: false
Explanation: The expiration time is 0, so the cache is immediately invalid.

Example 3:

Input: { data: 'some data', timestamp: 1700000000000, expiration: 86400000 } // Future timestamp
Output: false
Explanation: The timestamp is in the future, so the cache is invalid.

Constraints

  • The expiration time and timestamp will be non-negative numbers (in milliseconds).
  • The validateCache function should be performant; avoid unnecessary computations.
  • The function should be written in TypeScript.

Notes

Consider using Date.now() for obtaining the current time. Think about how to calculate the time difference between the current time and the timestamp. Focus on writing clear and concise code that is easy to understand and test. Your Jest tests should cover the examples provided and any other relevant edge cases you identify. The data property of the input object is irrelevant to the validation logic and can be ignored.

Loading editor...
typescript