Sunday, May 4, 2025

What is Caching Data

Caching data to reduce expensive or frequent database queries, it's common to cache the following data types or structures:


1. Lookup Tables / Reference Data

  • Country lists, currency codes, language options, etc.

  • Usually small, rarely change.

  • Cached as: Map / Object / Dictionary

2. User Sessions / Auth Tokens

  • User-specific session or authentication information.

  • Cached as: Key-Value Pairs, e.g., {userId: sessionInfo}

3. Frequently Accessed Business Data

  • Product catalogs, user profiles, configuration settings, etc.

  • Cached as: JSON Objects, Lists, or Serialized Records

4. Query Results

  • The result of complex or expensive SQL queries.

  • Cached as: Serialized Result Sets, e.g., JSON or binary formats

5. Computed/Derived Data

  • Aggregated metrics, scores, or statistics.

  • Cached as: Numbers, Objects, or Strings

6. HTML Fragments / Page Chunks

  • Rendered UI elements like menus, sidebars, etc.

  • Cached as: Strings

7. API Responses

  • Cached third-party API responses to avoid redundant calls.

  • Cached as: JSON/XML Strings or Deserialized Objects


Common Caching Tools

  • In-memory: Redis, Memcached, Ehcache

  • Client-side: Browser localStorage/sessionStorage

  • Application-level: Guava Cache (Java), @Cacheable (Spring), Angular/React state services


For a toy store website, caching can dramatically improve performance and reduce database load. Here's a breakdown of what you can cache and where it might be best cached:

1. Product Catalog

What to cache:

  • Toy listings, categories, filters

  • Product details (name, price, description, image URL)

Where to cache:

  • In-memory (e.g., Redis, in-app memory) for fast access

  • Browser cache for static content like images


๐Ÿงธ 2. Category and Filter Data

What to cache:

  • List of categories (e.g., Age 0-3, Educational, Dolls)

  • Available brands, age ranges, price brackets

Where to cache:

  • Application-level cache (e.g., in-memory or Redis)

  • CDN/static file for rarely changing filters


๐Ÿ‘ค 3. User Session and Cart

What to cache:

  • User login session, preferences

  • Shopping cart data

Where to cache:

  • Server-side session cache (e.g., Redis)

  • Local storage or cookies on the client for carts


๐Ÿ’ฐ 4. Promotions and Pricing Rules

What to cache:

  • Discount rules, active coupon codes, seasonal offers

Where to cache:

  • In-memory with short TTL (Time To Live)


๐ŸŒ 5. Frequently Accessed Pages

What to cache:

  • Homepage layout (banners, top products)

  • Bestsellers, new arrivals, etc.

Where to cache:

  • HTML fragment cache or full-page cache (e.g., Varnish, NGINX)

  • CDN for static parts


๐Ÿงพ 6. Order Status and History (per user)

What to cache:

  • Recently placed orders, shipping status

Where to cache:

  • Short-lived cache for recently viewed orders

  • Could be cached in Redis with keys like orders:userId


⚠️ Cache with care:

  • Inventory/stock levels can be cached, but with short TTL (or use a hybrid cache+DB approach) to avoid overselling.

  • Personalized recommendations can be cached per user or user segment.


No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

๐ŸŽฏ Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...