banatie-service/apps/api-service/src
Oleg Proskurin ca112886e5 feat: implement Phase 5 live generation with prompt caching
Implement cached generation endpoint that streams image bytes directly with
intelligent caching based on prompt hashing for optimal performance.

**Core Service:**
- **PromptCacheService**: Prompt-based cache management
  - SHA-256 hashing of prompts for cache lookup
  - Cache hit/miss tracking with statistics
  - Support for cache entry creation and retrieval
  - Hit count and last accessed timestamp tracking
  - Cache statistics per project

**v1 API Routes:**
- `GET /api/v1/live/generate` - Generate with caching, stream image bytes

**Endpoint Features:**
- Prompt-based caching with SHA-256 hashing
- Cache HIT: Streams existing image with X-Cache-Status: HIT header
- Cache MISS: Generates new image, caches it, streams with X-Cache-Status: MISS
- Direct image byte streaming (not JSON response)
- Cache-Control headers for browser caching (1 year max-age)
- Hit count tracking for cache analytics
- Integration with promptUrlCache database table

**Caching Logic:**
- Compute SHA-256 hash of prompt for cache key
- Check cache by promptHash and projectId
- On HIT: Fetch image from database, download from storage, stream bytes
- On MISS: Generate new image, create cache entry, stream bytes
- Record cache hits with incremented hit count

**Response Headers:**
- Content-Type: image/jpeg (or appropriate MIME type)
- Content-Length: Actual byte length
- Cache-Control: public, max-age=31536000 (1 year)
- X-Cache-Status: HIT | MISS
- X-Cache-Hit-Count: Number of cache hits (on HIT)
- X-Generation-Id: UUID (on MISS)
- X-Image-Id: UUID (always)

**Technical Notes:**
- Uses ImageService to fetch cached images by ID
- Uses StorageFactory to download image buffers from MinIO
- Parses storage keys to extract org/project/category/filename
- Validates storage key format before download
- All Phase 5 code is fully type-safe with zero TypeScript errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 23:00:10 +07:00
..
middleware fix: return correct codes 2025-10-12 22:13:34 +07:00
routes feat: implement Phase 5 live generation with prompt caching 2025-11-09 23:00:10 +07:00
services feat: implement Phase 5 live generation with prompt caching 2025-11-09 23:00:10 +07:00
types feat: implement Phase 4 image management with upload and CRUD endpoints 2025-11-09 22:41:59 +07:00
utils feat: add Phase 1 foundation for API v2.0 2025-11-09 21:53:50 +07:00
app.ts feat: implement Phase 2 core generation flow with services and endpoints 2025-11-09 22:14:49 +07:00
db.ts feat: update setup 2025-10-12 21:12:58 +07:00
server.ts feat: add gallery 2025-10-12 00:30:16 +07:00