# Hash

> Cache Key Generation (Hash)

- Skill: `harshamendu/hash` (Agent Skill, multi-file: 8 files)
- Install (CLI): `npx skillmds@latest add harshamendu/hash`
- Raw SKILL.md: https://api.skillmd.com/api/skills/harshamendu/hash/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Harshamendu (https://skillmd.com/u/harshamendu)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/harshamendu/hash

---

# 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
```java
// 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](guides/architecture.md) - Patterns, components, and structure
- [Implementation Guide](guides/implementation.md) - Delimiter strategy, encoding, and common logic
- [Testing Strategies](guides/testing.md) - Unit tests, determinism, and validation

## Examples
- [Basic Hash Generation](examples/basic-usage.md) - Standard cache hash creation
- [Service Integration](examples/service-integration.md) - Using HashGenerator in services
- [Testing Examples](examples/testing-examples.md) - 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):
```bash
./gradlew spotlessApply
```

## Performance
- Sub-millisecond hash generation
- No cryptographic overhead (Base64 only)
- Thread-safe stateless operations
- Average hash size: 80-120 characters

