How wintys/ehcache works
Project Type and Purpose
• This is a small Java library/tool (open-source utility) demonstrating how to use Ehcache 3.x for in-memory caching.
• It provides a utility class (EhcacheManagerUtil) for initializing and interacting with caches, a test harness (EhcacheTest), and a simple domain object (UserInfo) to be cached.
File Structure Analysis
• pom.xml: Maven build and dependency management (includes Ehcache 3.x).
• src/main/java/org/yxt/ehcache:
• EhcacheManagerUtil.java – encapsulates cache manager setup, cache creation, get/put operations.
• EhcacheTest.java – example/test code that exercises the caching util.
• pojo/UserInfo.java – domain model to cache.
• src/main/resources/ehcache.xml – cache configuration (cache names, sizes, expiry policies, resource pools).
Key Components
a. Application Layer
• EhcacheTest (or any client code) invoking cache operations via EhcacheManagerUtil.
b. Utility Layer
• EhcacheManagerUtil: initializes CacheManager from ehcache.xml, exposes getCache(), put(), remove() methods.
c. Cache System Layer
• Ehcache’s CacheManager & Cache<K,V> instances.
• Underlying resource pools: Heap, Off-heap, Disk.
• Expiry and Eviction configured via ehcache.xml.
d. Domain Layer
• UserInfo POJO objects being stored/retrieved.
e. Configuration
• ehcache.xml resource file feeding into CacheManager.
f. (Optional) External Monitoring/Management
• JMX (if enabled) or logs for cache statistics.
Relationships and Data Flow
• Client → EhcacheManagerUtil: calls initialize(), get("userCache"), put(UserInfo), get(id).
• EhcacheManagerUtil → CacheManager: reads ehcache.xml, creates/returns Cache.
• Cache → Resource Pools: stores entries in Heap/Off-heap/Disk as configured.
• Cache miss path (if implemented): delegate to data source (e.g., DB or in-memory stub).
• Client receives UserInfo from Cache or underlying loader.
Architectural Patterns & Principles
• Facade Pattern: EhcacheManagerUtil hides complex CacheManager API.
• Configuration-Driven: Behavior driven by external ehcache.xml.
• Separation of Concerns:
• Client code does not manage low-level cache details.
• Utility class solely handles caching logic.