Skip to content

๐Ÿš€ Spring Boot application demonstrating high-performance caching with Caffeine, including configuration, monitoring, and practical examples โšก

Notifications You must be signed in to change notification settings

sandeepkv93/spring-caffeine-cache-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Spring Boot Caffeine Cache Demo

A lightning-fast โšก caching implementation demo using Spring Boot and Caffeine Cache to supercharge your application performance! ๐ŸŽฏ

๐ŸŽฏ Overview

This project demonstrates a powerful caching system with a product management example, featuring:

  • ๐Ÿ’พ In-memory product storage with simulated slow service
  • โ˜• Caffeine cache integration with Spring Boot
  • ๐Ÿ“Š Real-time cache statistics monitoring
  • ๐Ÿ”„ RESTful API endpoints for product operations
  • โœ… 100% test coverage
  • ๐Ÿš Interactive shell script for cache behavior demonstration

๐Ÿ› ๏ธ Technical Stack

  • โ˜• Java 21
  • ๐Ÿƒ Spring Boot 3.2.0
  • โšก Caffeine Cache 3.1.8
  • ๐Ÿ—๏ธ Lombok
  • ๐Ÿ”จ Maven
  • ๐Ÿงช JUnit 5 with AssertJ

โš™๏ธ Cache Configuration

Supercharge your application with these cache settings! ๐Ÿ”ฅ

Caffeine.newBuilder()
    .initialCapacity(100)    // ๐ŸŒฑ Initial cache size
    .maximumSize(500)        // ๐Ÿ“ˆ Maximum cache entries
    .expireAfterWrite(5, TimeUnit.MINUTES)  // โฐ Entry expiration time
    .recordStats()           // ๐Ÿ“Š Enable statistics

๐ŸŽฎ API Endpoints

Method Endpoint Description
GET /api/products/{id} ๐Ÿ” Retrieve a product
PUT /api/products ๐Ÿ“ Update a product
DELETE /api/products/{id} ๐Ÿ—‘๏ธ Delete a product
DELETE /api/products/cache ๐Ÿงน Clear the cache
GET /api/products/cache/stats ๐Ÿ“Š Get cache statistics

๐ŸŒŸ Key Features

๐Ÿท๏ธ Caching Annotations

Smart caching with Spring annotations:

  • ๐Ÿ“ฅ @Cacheable: Caches method results
  • ๐Ÿ“ค @CachePut: Updates cache entries
  • ๐Ÿ—‘๏ธ @CacheEvict: Removes entries from cache
  • โœจ @EnableCaching: Enables Spring's caching magic

๐Ÿ“Š Cache Statistics

Monitor your cache like a pro:

  • ๐ŸŽฏ Hit/miss counts
  • ๐Ÿ“ˆ Hit rate
  • ๐Ÿš€ Eviction count
  • ๐Ÿ“ฆ Cache size

โฐ Simulated Latency

Watch the caching magic happen with a simulated delay:

private void simulateSlowService() {
    Thread.sleep(3000L); // โณ 3-second delay
}

๐Ÿงช Testing

We take testing seriously! ๐ŸŽฏ

  • ๐Ÿ”ฌ Unit tests for all components
  • ๐ŸŽฏ Cache behavior verification
  • ๐Ÿ›ก๏ธ Edge case handling
  • ๐Ÿ“Š JaCoCo test coverage reporting (80%+ coverage)

๐Ÿ”„ Cache Testing Script

Test drive the cache with our shell script! ๐Ÿš—

./test-cache.sh [iterations]

Watch it perform: ๐ŸŽฌ

  1. ๐Ÿ“Š Initial cache stats check
  2. ๐Ÿ†• First product retrieval (uncached)
  3. โšก Second product retrieval (cached)
  4. ๐Ÿ“ Product update
  5. ๐Ÿงน Cache clearing
  6. ๐Ÿ”„ Post-clear retrieval

๐Ÿš€ Building and Running

๐Ÿ“‹ Prerequisites

  • โ˜• Java 21 or higher
  • ๐Ÿ”จ Maven 3.8.0 or higher

๐Ÿ—๏ธ Build

mvn clean install

๐ŸŽฌ Run

mvn spring-boot:run

Application launches on port 8989 ๐Ÿš€

๐Ÿ’Ž Code Quality

Quality is our priority! ๐Ÿ’ช

  • ๐Ÿ“ Google Java Format enforcement
  • ๐Ÿ” Maven Enforcer Plugin
  • ๐Ÿ“š JavaDoc documentation
  • ๐Ÿ“Š JaCoCo code coverage
  • โœจ Consistent formatting

๐Ÿš€ Performance Benefits

Experience these amazing performance gains:

  • ๐Ÿ“‰ Reduced database load
  • โšก Lightning-fast response times
  • ๐Ÿ”„ Smart cache eviction
  • โš™๏ธ Flexible configuration

๐ŸŒŸ Best Practices

  1. ๐ŸŽฏ Cache Configuration

    • ๐Ÿ“ฆ Optimal cache sizes
    • โฐ Smart expiration policies
    • ๐Ÿ“Š Performance monitoring
  2. ๐Ÿ›ก๏ธ Error Handling

    • ๐Ÿ” Exception management
    • ๐Ÿงต Thread handling
    • โœ… Null safety
  3. ๐Ÿงช Testing

    • ๐ŸŽฏ Complete coverage
    • ๐Ÿ”ฌ Edge cases
    • ๐Ÿ“Š Performance metrics
  4. ๐Ÿ“ Code Organization

    • ๐Ÿ—๏ธ Clean architecture
    • ๐Ÿ”„ Separation of concerns
    • ๐Ÿ’‰ Dependency injection

About

๐Ÿš€ Spring Boot application demonstrating high-performance caching with Caffeine, including configuration, monitoring, and practical examples โšก

Topics

Resources

Stars

Watchers

Forks