1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class HeaderCache {
  4. private static final Map<String, Map<String, String>> cache = new HashMap<>(); // Cache for each staging environment
  5. public static Map<String, String> getHeaders(String stagingEnvironment) {
  6. // Check if headers are already cached for this staging environment
  7. Map<String, String> cachedHeaders = cache.get(stagingEnvironment);
  8. if (cachedHeaders != null) {
  9. return cachedHeaders; // Return cached headers
  10. }
  11. // If not cached, simulate a header request (replace with actual request logic)
  12. Map<String, String> headers = simulateHeaderRequest(stagingEnvironment);
  13. // Cache the headers for this staging environment
  14. cache.put(stagingEnvironment, headers);
  15. return headers;
  16. }
  17. private static Map<String, String> simulateHeaderRequest(String stagingEnvironment) {
  18. // Simulate fetching headers from a staging environment (replace with actual logic)
  19. Map<String, String> headers = new HashMap<>();
  20. headers.put("X-Staging-Id", stagingEnvironment);
  21. headers.put("Content-Type", "application/json");
  22. return headers;
  23. }
  24. public static void main(String[] args) {
  25. // Example usage:
  26. Map<String, String> staging1Headers = getHeaders("staging-env-1");
  27. System.out.println("Staging 1 Headers: " + staging1Headers);
  28. Map<String, String> staging1HeadersAgain = getHeaders("staging-env-1"); // Should be from cache
  29. System.out.println("Staging 1 Headers (from cache): " + staging1HeadersAgain);
  30. Map<String, String> staging2Headers = getHeaders("staging-env-2");
  31. System.out.println("Staging 2 Headers: " + staging2Headers);
  32. }
  33. }

Add your comment