performance

python
from django.db.models import Prefetch
from django.views.generic import ListView
from .models import Post, Comment


class PostListView(ListView):

Django select_related and prefetch_related for N+1 query optimization

django python performance
by Priya Sharma 1 tab
ruby
class AddCommentsCountToPosts < ActiveRecord::Migration[6.1]
  def up
    add_column :posts, :comments_count, :integer, default: 0, null: false

    # Populate existing counts
    Post.find_each do |post|

Counter cache for association counts

rails performance activerecord
by Alex Kumar 2 tabs
erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs
ruby
class CreateCustomerRevenueSummaries < ActiveRecord::Migration[7.0]
  def change
    create_view :customer_revenue_summaries, materialized: true, version: 1

    add_index :customer_revenue_summaries,
              :customer_id,

Database Views for Read Models

rails postgres performance
by codesnips 4 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Throttled Last-Seen Tracking with Laravel Middleware and Redis Cache

laravel middleware redis
by codesnips 4 tabs
sql
-- Create basic index
CREATE INDEX idx_users_email ON users(email);

-- Unique index (enforces uniqueness)
CREATE UNIQUE INDEX idx_users_username ON users(username);

Database indexing strategies for performance

database indexing performance
by Maria Garcia 2 tabs
sql
CREATE TABLE subscriptions (
    id                  BIGGENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_id         BIGINT       NOT NULL,
    plan_code           TEXT         NOT NULL,
    status              TEXT         NOT NULL DEFAULT 'active',
    current_period_end  TIMESTAMPTZ  NOT NULL,

Partial index for “active” rows in Postgres

postgres performance sql
by codesnips 3 tabs
typescript
import { SetMetadata } from '@nestjs/common';

export const CACHEABLE_KEY = 'cacheable:options';

export type VaryDimension = 'user' | 'tenant' | 'query';

Per-Route Response Caching in NestJS with a Custom CacheInterceptor and Cache Key Builder

nestjs caching interceptors
by codesnips 4 tabs
java
@RestController
@RequestMapping("/api/customers")
public class CsvImportController {

    private final CsvImportService importService;

Streaming CSV Import in Spring Boot with MultipartFile and JDBC Batch Inserts

spring-boot csv jdbc
by codesnips 4 tabs
python
import hashlib
from functools import wraps

from flask import request, make_response
from redis import Redis

Flask ETag Caching for an Expensive Endpoint with Conditional 304 Handling

flask http-caching etag
by codesnips 2 tabs
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :user, :request_id

  def permissions
    @permissions ||= PermissionSet.new(user)
  end

Memoizing Computed User Permissions Per Request with Current Attributes in Rails

rails authorization caching
by codesnips 4 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["sentinel"]
  static values = {
    url: String

Infinite scroll with Turbo Frames and Intersection Observer

hotwire turbo stimulus
by Jordan Lee 3 tabs