Cache Key Generation (Hash)
📝 Note: This guide uses generic placeholder names to be reusable across any Spring Boot microservice.
Replace with your actual implementation:
{YourService} → Your service name (e.g., OrderService, PaymentService)
BusinessService → Your core service (e.g., OrderService, UserService)
DataService → Your data processing service (e.g., PaymentService, InventoryService)
IntegrationService → Your external integration (e.g., PaymentGatewayService)
{RequestType} → Your request DTO (e.g., CreateOrderRequest)
{ResponseType} → Your response DTO (e.g., OrderResponse)
Overview
The hash package generates consistent, deterministic cache keys using Base64 encoding for authentication tokens, resource accesss, and user-specific data. Cache hashes enable efficient invalidation and client-side caching strategies.
Package
Base: com.example.microserviceorch.hash.example
Core Components
- HashGenerator - Interface defining hash generation contract
- HashGeneratorImpl - Base64-based implementation with delimiter composition
Key Capabilities
- Generate cache hashes for authenticated users with resource accesss
- Generate user-specific cache hashes (AMC account or Adobe ID)
- Generate cache hashes for unauthenticated users
- Deterministic, consistent hash generation with case normalization
- Compact representation using abbreviated keys
- Base64 encoding for URL-safe cache keys
Quick Reference
Hash Types
// Cache hash for authenticated users
String cacheHash = hashGenerator.generateCacheHash(wrapper, resource accesss);
// User-specific cache hash
String userCacheHash = hashGenerator.generateUserCacheHash(accountId, wrapper);
// Unauthenticated user cache hash
String unauthCacheHash = hashGenerator.generateUnauthUserCacheHash(wrapper);
Cache Key Structure
t=amcn;n=amc;p=web;e=amcn_auth,ob-sub-amcplus;c=us;l=en-us;av=1.0.0
↓ Base64 Encoding ↓
dD1hbWNuO249YW1jO3A9d2ViO2U9YW1jbl9hdXRoLG9iLXN1Yi1hbWNwbHVzO2M9dXM7bD1lbi11czthdj0xLjAuMA==
Design Patterns
- Strategy Pattern: Different strategies for auth, unauth, and user-specific hashes
- Template Method: Common hash generation logic (
generateCacheHashCommon)
- Builder Pattern: Fluent RequestParametersWrapper construction
- Dependency Injection: Component injected via constructor
Best Practices
✅ Use abbreviated keys (t, n, p, etc.) for compact cache keys
✅ Normalize case for country and language codes
✅ Maintain consistent parameter ordering for deterministic hashes
✅ Handle optional fields gracefully (only add if present)
✅ Use null-safe checks with default values
✅ Log warnings for invalid inputs
❌ Don't use long key names (increases cache key size)
❌ Don't skip case normalization (causes cache misses)
❌ Don't randomize parameter ordering (breaks determinism)
❌ Don't silently swallow errors
Guides
- Hash Generation Architecture - Patterns, components, and structure
- Implementation Guide - Delimiter strategy, encoding, and common logic
- Testing Strategies - Unit tests, determinism, and validation
Examples
- Basic Hash Generation - Standard cache hash creation
- Service Integration - Using HashGenerator in services
- Testing Examples - Comprehensive test cases
Cache Key Reference
| Key |
Full Name |
Description |
Example |
t |
Tenant |
Multi-tenant identifier |
"amcn" |
n |
Network |
Network/brand identifier |
"amc", "sundancenow" |
p |
Platform |
Client platform |
"web", "ios", "android" |
e |
Entitlements |
Comma-separated resource accesss |
"amcn_auth,ob-sub" |
c |
Country Code |
ISO country code (lowercase) |
"us", "ca", "uk" |
l |
Language |
Language code (lowercase) |
"en-us", "fr", "es" |
a |
Account ID |
User account identifier |
"550e8400-e29b..." |
d |
Device ID |
Device identifier |
"device-uuid-123" |
tc |
Test Context |
QA test environment |
"qa-env-1" |
iaps |
IAP Supported |
In-app purchase support |
1 or 0 |
av |
App Version |
Application version |
"1.0.0", "2.5.3" |
ff |
Feature Flag |
Feature flag identifier |
"new-ui-v2" |
Code Formatting
Use Spotless with Google Java Format (AOSP style):
./gradlew spotlessApply
Performance
- Sub-millisecond hash generation
- No cryptographic overhead (Base64 only)
- Thread-safe stateless operations
- Average hash size: 80-120 characters
1---2name: hash3description: Cache Key Generation (Hash)4---5# Cache Key Generation (Hash)67> **📝 Note:** This guide uses generic placeholder names to be reusable across any Spring Boot microservice.8> Replace with your actual implementation:9> - `{YourService}` → Your service name (e.g., `OrderService`, `PaymentService`)10> - `BusinessService` → Your core service (e.g., `OrderService`, `UserService`)11> - `DataService` → Your data processing service (e.g., `PaymentService`, `InventoryService`)12> - `IntegrationService` → Your external integration (e.g., `PaymentGatewayService`)13> - `{RequestType}` → Your request DTO (e.g., `CreateOrderRequest`)14> - `{ResponseType}` → Your response DTO (e.g., `OrderResponse`)151617## Overview18The hash package generates consistent, deterministic cache keys using Base64 encoding for authentication tokens, resource accesss, and user-specific data. Cache hashes enable efficient invalidation and client-side caching strategies.1920## Package21**Base**: `com.example.microserviceorch.hash.example`2223## Core Components24- **HashGenerator** - Interface defining hash generation contract25- **HashGeneratorImpl** - Base64-based implementation with delimiter composition2627## Key Capabilities28- Generate cache hashes for authenticated users with resource accesss29- Generate user-specific cache hashes (AMC account or Adobe ID)30- Generate cache hashes for unauthenticated users31- Deterministic, consistent hash generation with case normalization32- Compact representation using abbreviated keys33- Base64 encoding for URL-safe cache keys3435## Quick Reference3637### Hash Types38```java39// Cache hash for authenticated users40String cacheHash = hashGenerator.generateCacheHash(wrapper, resource accesss);4142// User-specific cache hash43String userCacheHash = hashGenerator.generateUserCacheHash(accountId, wrapper);4445// Unauthenticated user cache hash46String unauthCacheHash = hashGenerator.generateUnauthUserCacheHash(wrapper);47```4849### Cache Key Structure50```51t=amcn;n=amc;p=web;e=amcn_auth,ob-sub-amcplus;c=us;l=en-us;av=1.0.052 ↓ Base64 Encoding ↓53dD1hbWNuO249YW1jO3A9d2ViO2U9YW1jbl9hdXRoLG9iLXN1Yi1hbWNwbHVzO2M9dXM7bD1lbi11czthdj0xLjAuMA==54```5556## Design Patterns57- **Strategy Pattern**: Different strategies for auth, unauth, and user-specific hashes58- **Template Method**: Common hash generation logic (`generateCacheHashCommon`)59- **Builder Pattern**: Fluent RequestParametersWrapper construction60- **Dependency Injection**: Component injected via constructor6162## Best Practices63✅ Use abbreviated keys (`t`, `n`, `p`, etc.) for compact cache keys 64✅ Normalize case for country and language codes 65✅ Maintain consistent parameter ordering for deterministic hashes 66✅ Handle optional fields gracefully (only add if present) 67✅ Use null-safe checks with default values 68✅ Log warnings for invalid inputs 6970❌ Don't use long key names (increases cache key size) 71❌ Don't skip case normalization (causes cache misses) 72❌ Don't randomize parameter ordering (breaks determinism) 73❌ Don't silently swallow errors 7475## Guides76- [Hash Generation Architecture](guides/architecture.md) - Patterns, components, and structure77- [Implementation Guide](guides/implementation.md) - Delimiter strategy, encoding, and common logic78- [Testing Strategies](guides/testing.md) - Unit tests, determinism, and validation7980## Examples81- [Basic Hash Generation](examples/basic-usage.md) - Standard cache hash creation82- [Service Integration](examples/service-integration.md) - Using HashGenerator in services83- [Testing Examples](examples/testing-examples.md) - Comprehensive test cases8485## Cache Key Reference8687| Key | Full Name | Description | Example |88|-----|-----------|-------------|---------|89| `t` | Tenant | Multi-tenant identifier | `"amcn"` |90| `n` | Network | Network/brand identifier | `"amc"`, `"sundancenow"` |91| `p` | Platform | Client platform | `"web"`, `"ios"`, `"android"` |92| `e` | Entitlements | Comma-separated resource accesss | `"amcn_auth,ob-sub"` |93| `c` | Country Code | ISO country code (lowercase) | `"us"`, `"ca"`, `"uk"` |94| `l` | Language | Language code (lowercase) | `"en-us"`, `"fr"`, `"es"` |95| `a` | Account ID | User account identifier | `"550e8400-e29b..."` |96| `d` | Device ID | Device identifier | `"device-uuid-123"` |97| `tc` | Test Context | QA test environment | `"qa-env-1"` |98| `iaps` | IAP Supported | In-app purchase support | `1` or `0` |99| `av` | App Version | Application version | `"1.0.0"`, `"2.5.3"` |100| `ff` | Feature Flag | Feature flag identifier | `"new-ui-v2"` |101102## Code Formatting103Use **Spotless** with Google Java Format (AOSP style):104```bash105./gradlew spotlessApply106```107108## Performance109- Sub-millisecond hash generation110- No cryptographic overhead (Base64 only)111- Thread-safe stateless operations112- Average hash size: 80-120 characters