Code Explainers

Code explainers tagged #caching

javascript
import { useState, useEffect, useCallback, useRef } from 'react';
 
const cache = new Map();
const inflight = new Map();

Building a stale-while-revalidate hook in React

caching request-deduplication custom-hooks
Advanced 10 steps
php
<?php
 
namespace App\Services;
 

Building a cached daily leaderboard in Laravel

caching aggregation eager-loading
Intermediate 9 steps
python
import secrets
 
from django.contrib.auth import authenticate, login
from django.core.cache import cache

Two-factor login with OTP in Django

two-factor-auth one-time-passwords caching
Intermediate 9 steps
typescript
import { Pipe, PipeTransform } from '@angular/core';
 
@Pipe({
  name: 'chunk',

A memoized chunk pipe in Angular

memoization weakmap pure-pipes
Intermediate 7 steps
python
from django.db import connections
from django.db.utils import OperationalError
from django.core.cache import caches
from django.http import JsonResponse

Building a health check endpoint in Django

health check monitoring database
Intermediate 7 steps
php
<?php
 
namespace App\Services;
 

A cached settings service in Laravel

caching memoization cache-invalidation
Intermediate 8 steps
javascript
const crypto = require('crypto');
 
function idempotency({ store, ttlMs = 24 * 60 * 60 * 1000 }) {
  return async function idempotencyMiddleware(req, res, next) {

Idempotent POST requests in Express

idempotency middleware caching
Advanced 8 steps
go
package routing
 
import (
	"net/http"

Region-aware upstream routing in Gin

middleware concurrency load-balancing
Advanced 8 steps
java
@Service
public class ProductLookupService {
 
    private final ProductRepository productRepository;

How Spring caching layers a product lookup

caching cache-eviction fallback
Intermediate 8 steps
python
import hashlib
import json
from functools import wraps
from flask import g, request, jsonify, current_app

Building an idempotency decorator in Flask

idempotency decorators redis
Advanced 7 steps
typescript
import {
  CallHandler,
  ConflictException,
  ExecutionContext,

Idempotent POST requests in NestJS with Redis

idempotency interceptor redis
Advanced 7 steps
typescript
type CacheEntry<V> = {
  value: V;
  computedAt: number;
};

A TTL cache keyed by object identity

caching weakmap generics
Intermediate 8 steps
typescript
import { Injectable, Scope } from '@nestjs/common';
import DataLoader from 'dataloader';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';

Batching queries with DataLoader in NestJS

dataloader batching n+1
Intermediate 7 steps
javascript
class RequestDeduplicator {
  constructor(fetcher) {
    this.fetcher = fetcher;
    this.inFlight = new Map();

Deduplicating in-flight requests in JavaScript

promises caching async
Intermediate 6 steps
typescript
type CompactOptions = {
  locale?: string;
  maximumFractionDigits?: number;
};

Caching Intl.NumberFormat for compact numbers

memoization internationalization caching
Intermediate 7 steps
java
@Component
public class ApiKeyRotationFilter extends OncePerRequestFilter {
 
    private static final String API_KEY_HEADER = "X-Api-Key";

Building an API key rotation filter in Spring

servlet-filter authentication caching
Intermediate 10 steps
typescript
import {
  CallHandler,
  ExecutionContext,
  Injectable,

Deduping in-flight requests in NestJS

interceptor request-deduplication rxjs
Advanced 6 steps
php
namespace App\Console\Commands;
 
use App\Models\Region;
use App\Services\ReportBuilder;

How a Laravel command warms report caches

caching cache-warming artisan-command
Intermediate 5 steps