Code Explainers

Code explainers tagged #caching

java
@Component
public class IdempotencyFilter extends OncePerRequestFilter {
 
    private final StringRedisTemplate redis;

Building an idempotency filter with Redis in Spring

idempotency redis distributed-locking
Advanced 10 steps
php
<?php
 
namespace App\Http\Middleware;
 

A scheduled maintenance-window middleware in Laravel

middleware caching http-responses
Intermediate 7 steps
typescript
import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  DetachedRouteHandle,

A scroll-preserving RouteReuseStrategy in Angular

route-reuse scroll-restoration caching
Advanced 10 steps
python
from flask import g, session
 
from .extensions import cache
from .models import User

Injecting the current user in Flask templates

context-processor caching authentication
Intermediate 6 steps
java
@Service
@RequiredArgsConstructor
public class ExchangeRateService {
 

Request-scoped rate caching in Spring

request-scope caching memoization
Advanced 6 steps
ruby
module Rack
  class MaintenanceMode
    RETRY_AFTER = 3600
 

A Rack maintenance-mode middleware in Rails

middleware rack http-status
Intermediate 8 steps
php
<?php
 
namespace App\Http\Middleware;
 

Caching HTTP responses with Laravel middleware

middleware caching http
Intermediate 8 steps
python
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

Busting template fragment caches in Django

caching signals cache-invalidation
Intermediate 4 steps
php
<?php
 
namespace App\Http\Controllers;
 

A cached autocomplete endpoint in Laravel

caching validation query-ranking
Intermediate 8 steps
go
package handler
 
type flightResult struct {
	status int

Deduping in-flight requests in Gin

middleware concurrency deduplication
Advanced 9 steps
java
@Component
public class RegionCacheWarmer implements SmartInitializingSingleton {
 
    private static final Logger log = LoggerFactory.getLogger(RegionCacheWarmer.class);

Warming a Spring cache at startup

caching startup-hook dependency-injection
Intermediate 7 steps
ruby
module UniqueJob
  extend ActiveSupport::Concern
 
  class_methods do

Deduplicating Active Job enqueues in Rails

concurrency idempotency caching
Advanced 9 steps
go
func UserDashboardCache(rdb *redis.Client, ttl time.Duration) gin.HandlerFunc {
	return func(c *gin.Context) {
		claims, ok := c.Get("claims")
		if !ok {

Per-user response caching in Gin with Redis

middleware caching redis
Advanced 9 steps
javascript
import { unstable_cache, revalidateTag } from 'next/cache'
import { db } from '@/lib/db'
 
export const getDashboardStats = unstable_cache(

Caching dashboard stats in Next.js

caching cache-invalidation tag-based-revalidation
Intermediate 8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json};
use dashmap::DashMap;
use serde::Serialize;
use std::sync::Arc;

Request coalescing in an Axum handler

caching concurrency request-coalescing
Advanced 8 steps
go
package cache
 
import (
	"sync"

A thread-safe TTL cache in Go

concurrency mutex caching
Intermediate 9 steps
go
package middleware
 
import (
	"bytes"

Idempotent requests in Gin with Redis

idempotency caching middleware
Advanced 10 steps
java
public class RequestCoalescer<K, V> {
 
    private final ConcurrentHashMap<K, CompletableFuture<V>> inFlight = new ConcurrentHashMap<>();
    private final Function<K, V> loader;

Coalescing duplicate requests in Java

concurrency caching completablefuture
Advanced 6 steps