Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 9 of 10 · Caching, Async & Scheduling
Interview question

What are the common cache eviction policies (LRU, LFU, TTL) and how do you configure them in Spring Boot (Caffeine)? सामान्य कैश इविक्शन पॉलिसीज़ (LRU, LFU, TTL) क्या हैं, और स्प्रिंग बूट (Caffeine) में इन्हें कैसे कॉन्फिगर करें?

Answer

An unbounded cache eventually exhausts memory, so eviction policies decide which entries to remove once a size or time limit is reached. LRU (Least Recently Used) evicts the entry that hasn't been accessed for the longest time; LFU (Least Frequently Used) evicts the entry accessed the fewest times; TTL (Time To Live) evicts entries a fixed duration after they were written or last accessed, regardless of access frequency.

Caffeine is the recommended high-performance local cache library for Spring Boot (replacing the older Guava cache), configured with maximumSize for an LRU-like bounded cache and expireAfterWrite/expireAfterAccess for TTL-based expiration, auto-detected by Spring Boot's cache abstraction when it's on the classpath.

spring:
  cache:
    type: caffeine
    caffeine:
      spec: maximumSize=1000,expireAfterWrite=10m

एक असीमित कैश अंततः मेमोरी समाप्त कर देता है, इसलिए इविक्शन पॉलिसीज़ यह तय करती हैं कि आकार या समय सीमा पहुँचने पर किन एंट्रीज़ को हटाया जाए। LRU (लीस्ट रीसेंटली यूज़्ड) उस एंट्री को हटाता है जिसे सबसे लंबे समय से एक्सेस नहीं किया गया। LFU (लीस्ट फ्रीक्वेंटली यूज़्ड) उस एंट्री को हटाता है जिसे सबसे कम बार एक्सेस किया गया। TTL (टाइम टू लिव) एक निश्चित अवधि के बाद एंट्रीज़ को हटाता है।

Caffeine स्प्रिंग बूट के लिए अनुशंसित उच्च-प्रदर्शन लोकल कैश लाइब्रेरी है, जिसे maximumSize और expireAfterWrite से कॉन्फिगर किया जाता है।

spring:
  cache:
    type: caffeine
    caffeine:
      spec: maximumSize=1000,expireAfterWrite=10m

Was this answer clear?