Как вы внедряли кэширование Redis: что кэшировали, как работала схема и как выполнялась инвалидация?
Asked at
AI answer
A typical approach is cache-aside: the application first reads a value from Redis; on a cache miss, it fetches the value from the primary data store, saves it in the cache with a TTL, and returns it. Keys are built from the entity type and query parameters, for example user:42 or products:category:7:page:1; values are serialized. When data changes, the cache entries for the corresponding keys are deleted or updated after a successful write to the primary data store. A TTL limits how long stale entries remain and provides a safeguard against missed invalidation. A practical design also accounts for race conditions and cache-miss spikes, cache size, and hit/miss metrics. The specific choice—whether to cache a fully assembled response, an object, or the result of an expensive computation—depends on the workload profile.
Also asked at GigaChat / Сбер
- app = FastAPI() def get_db(): db = SessionLocal() try: yield db finally: db.close() def heavy_text_parser(file_id: int, db: Session): # Parsing a file that takes 5 minutes parse_and_save(file_id, db) @app.post("/upload") async def upload_file( background_tasks: BackgroundTasks, db: Session = Depends(get_db) ): file_id = save_file_to_db(db) db.commit() background_tasks.add_task(heavy_text_parser, file_id, db) return {"status": "parsing started"}
- def extract_emails_from_huge_file(file_path: str) -> list[str]: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # Заменяем переносы строк, чтобы текст был "сплошным" content = content.replace('\r\n', ' ').replace('\n', ' ') lines = content.split(' ') emails = [word for word in lines if '@' in word] return emails
Similar questions
- How did you invalidate the cache?
- How did you use Redis at CVM—as a cache, a key-value store, or a message broker?
- How was Redis set up in your project (one instance or several)?
- Could you briefly explain the caching in this case: what was cached, and how did it help?
- What is a Cache Stampede (the problem of simultaneous TTL expiration), and how can you protect a system from degradation when hot keys are invalidated?