Merged
Conversation
The cache interface is composed of a class that forces implementations to provide save, get, and delete methods. Currently, the only implementation is valkey which has a docker service limiting its memory. Application connects to the cache instance via CACHE_HOST and CACHE_PORT variables with CACHE_TTL_SECONDS set to 1h by default
Now translate and detect routes will save the response to cache and retrieve to reply faster to users. The cache key is: - translate:src_lang:tgt_lang:text - detect:text Where text is a sanitized version of the input text. By sanitizations the app will do: - Normalize Unicode characters (NFC form) - Trim excess whitespaces - Covert to lowercase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Feature: Caching System for Translation and Detection Services
Overview
This PR introduces a robust caching system for Babeltron's translation and language detection services. The implementation significantly improves response times for repeated requests by storing previous results in a Valkey (Redis-compatible) cache with configurable TTL.
Key Features
1. Cache Infrastructure
CacheInterface) that defines the contract for all cache implementations withsave,get, anddeletemethods.2. Cache Service Layer
3. API Integration
cached: booleanfield to responses to indicate cache hits4. Configuration
CACHE_HOST: Hostname for the cache serverCACHE_PORT: Port for the cache serverCACHE_TTL_SECONDS: Time-to-live for cached items (default: 3600s/1h)5. Infrastructure Updates
6. Testing
Technical Details
Cache Key Format
translate:{source_lang}:{target_lang}:{normalized_text}detect:{normalized_text}Text normalization includes:
Performance Considerations
Breaking Changes
None. This is a non-breaking enhancement that maintains backward compatibility with existing API contracts.
Future Improvements
Dependencies
valkeypackage (v6.1.0) - Redis-compatible Python clientTesting Instructions
Start the application with Docker Compose:
Make a translation request:
Make the same request again and observe the
cached: trueflag in the response and faster response time.Similarly, test the detection endpoint: