ruby
class RegistrationsController < ApplicationController
  def new
    @user = User.new
  end

  def create

Send a Welcome Email After Signup Using a Sidekiq Background Job in Rails

ruby rails sidekiq
by codesnips 4 tabs
go
package pricecache

import (
	"context"
	"sync"

Collapse Concurrent Identical Requests in Go with singleflight

go singleflight concurrency
by codesnips 3 tabs
python
import functools
import threading
import time
from collections import OrderedDict

Custom TTL + LRU Cache Decorator for Expensive Python Computations

python caching lru
by codesnips 2 tabs
php
<?php

namespace App\Exports;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;

Streaming a Filtered Orders CSV Export in Laravel with StreamedResponse

php laravel csv
by codesnips 3 tabs
ruby
module KeysetPaginatable
  extend ActiveSupport::Concern

  class_methods do
    def keyset_page(cursor: nil, limit: 20, direction: :desc)
      limit = limit.to_i.clamp(1, 100)

Keyset Cursor Pagination for ActiveRecord Instead of OFFSET

ruby rails activerecord
by codesnips 4 tabs
rust
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::{Arc, RwLock};

pub struct Memoizer<K, V> {
    store: RwLock<HashMap<K, Arc<V>>>,

Thread-Safe Memoization in Rust with RwLock and OnceCell Sharding

rust concurrency memoization
by codesnips 3 tabs
javascript
import { useState, useEffect, useCallback } from 'react';

export function useLocalStorage(key, initialValue) {
  const readValue = useCallback(() => {
    if (typeof window === 'undefined') return initialValue;
    try {

Persist a React Shopping Cart to localStorage with a Context Provider

javascript react context
by codesnips 3 tabs
java
package com.example.orders;

public final class Order {

    public static final Order POISON_PILL = new Order(-1L, 0.0);

Producer/Consumer Order Processing With a Bounded BlockingQueue in Java

java concurrency blockingqueue
by codesnips 3 tabs
typescript
export interface Cursor {
  createdAt: string;
  id: string;
}

export function encodeCursor(c: Cursor): string {

Cursor Pagination for a REST List Endpoint with a Typed Fetch Client

typescript express rest
by codesnips 3 tabs
go
package ratelimit

import (
	"sync"
	"time"
)

Token Bucket Rate-Limiting Middleware for a Go net/http API

go net-http middleware
by codesnips 4 tabs
python
import asyncio
import time
from dataclasses import dataclass, field


@dataclass

Per-Client Rate Limiting in FastAPI with a Token Bucket Dependency

python fastapi rate-limiting
by codesnips 3 tabs
python
import abc
from typing import IO


class Exporter(abc.ABC):
    name: str = ""

Plugin Registry with Class Decorators and setuptools Entry-Point Discovery

python plugins decorators
by codesnips 4 tabs