Code Explainers

Code explainers tagged #caching

php
<?php
 
namespace App\View\Composers;
 

How a Laravel view composer feeds the navigation

view-composer caching dependency-injection
Intermediate 5 steps
go
package middleware
 
import (
	"bytes"

Building a response cache middleware in Gin

middleware caching concurrency
Intermediate 8 steps
python
from flask import Blueprint, jsonify, request, abort
from flask_caching import Cache
 
cache = Cache(config={"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0"})

Custom cache keys for Flask endpoints

caching redis cache-invalidation
Intermediate 7 steps
go
package server
 
import (
	"net/http"

Serving cached static assets in Gin

caching static-files middleware
Intermediate 7 steps
java
@RefreshScope
@Component
public class FeatureSettings {
 

Refreshable feature settings in Spring

caching configuration dependency-injection
Intermediate 7 steps
php
<?php
 
namespace App\Resilience;
 

How a circuit breaker guards a flaky service

circuit-breaker resilience caching
Intermediate 9 steps
python
from collections import OrderedDict
from typing import Any, Hashable, Optional
 
 

Building an LRU cache with OrderedDict

caching eviction data-structures
Intermediate 6 steps
php
<?php
 
namespace App\Services;
 

Phone verification codes in Laravel

rate-limiting caching hashing
Intermediate 7 steps
javascript
function memoize(fn, resolver) {
  const cache = new Map();
 
  function memoized(...args) {

Building a memoize wrapper in JavaScript

memoization closures higher-order-functions
Intermediate 7 steps
python
from functools import wraps
 
from django.core.cache import cache
from django.http import JsonResponse

Building a rate-limit decorator in Django

decorators rate limiting caching
Intermediate 7 steps
python
import os
from dataclasses import dataclass
from functools import lru_cache
 

Loading typed config from environment variables

configuration environment-variables type-coercion
Intermediate 6 steps
java
@Aspect
@Component
public class IdempotencyAspect {
 

How an idempotency aspect works in Spring

aop idempotency caching
Advanced 9 steps
python
from django.core.cache import cache
from django.db.models import Count, Sum, Q
from django.utils import timezone
 

Caching a Django dashboard aggregate query

caching aggregation conditional-queries
Intermediate 7 steps
typescript
type Fetcher<T> = (key: string) => Promise<T>;
 
export class RequestDeduplicator<T> {
  private inFlight = new Map<string, Promise<T>>();

Deduplicating in-flight requests in TypeScript

deduplication promises caching
Intermediate 7 steps
go
package cache
 
import (
	"context"

Cache-aside reads with singleflight in Go

caching singleflight concurrency
Advanced 8 steps
javascript
import { NextResponse } from 'next/server'
 
const UPSTREAM = 'https://api.exchangerate.host/latest'
 

A cached exchange-rate proxy in Next.js

route-handler caching revalidation
Intermediate 7 steps
javascript
const formatters = new Map();
 
function getFormatter(locale, currency) {
  const key = `${locale}:${currency}`;

Caching Intl.NumberFormat for currency formatting

memoization internationalization caching
Intermediate 8 steps
go
package web
 
import (
	"embed"

Serving embedded static assets in Go

embed static-assets http-handler
Intermediate 8 steps