Professional Documents
Culture Documents
Preface 1
Introduction 1
The JCache API (JSR 107) 1
JCache "Hello World" 2
Cache 3
CacheManager 3
CachingProvider 4
Caching 5
Configuration 5
Expiry Policy 6
Listeners and Listener Filters 6
Cache Entry Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Cache Manager Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Listener Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Loaders and Writers 8
Cache Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Cache Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Cache Entry Processors 9
Annotations 9
Monitoring And Management 10
Management and Monitoring Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
JMX API for JCache Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Vendor Specific Features 11
Cache Topologies And Modes 12
Resources 12
PREFACE
PREFACE applications by simply annotating methods
with caching annotations like @Cacheable,
Welcome to the Java Caching Cheatsheet! Caching @CachePut, and @CacheEvict to control caching
plays a crucial role in improving application behavior at the method level.
performance, scalability, and reliability by storing
• HTTP Caching: For web applications, Java
frequently accessed data in memory for fast
supports HTTP caching mechanisms using
retrieval. Whether you’re a developer, architect, or
headers like Cache-Control and ETag. These
system administrator, understanding caching
headers enable browsers and web servers to
concepts, strategies, and best practices is essential
cache resources like images, stylesheets, and
for building efficient and responsive applications.
scripts, reducing the need to re-download them
on subsequent requests.
This cheatsheet provides a comprehensive
overview of caching systems, covering topics such • Database Caching: Caching can be applied at
as cache topologies, implementation strategies, the database level to store frequently accessed
management, and monitoring. Whether you’re new query results. Java applications often leverage
to caching or looking to deepen your knowledge, technologies like Hibernate for object-relational
this cheatsheet serves as a handy reference for mapping, which includes caching capabilities.
mastering caching fundamentals and optimizing
your Java applications. Java caching plays a crucial role in optimizing
application performance, especially in scenarios
INTRODUCTION
INTRODUCTION
where data retrieval or computation is resource-
intensive. It is essential to carefully design and
Java caching is a technique used to store and configure caching strategies based on the specific
manage frequently accessed data in a temporary requirements of the application to strike a balance
storage area, known as a cache, to improve between improved performance and efficient
application performance. Caching helps reduce the resource utilization.
time and resources needed to fetch or compute data
by keeping a copy readily available. THEJCACHE
THE JCACHEAPI
API(JSR
(JSR107)
107)
In Java, caching can be implemented at various JSR-107, commonly known as JCache, is a Java
levels, including: specification for a caching API and standardizing
caching annotations. The goal of JCache is to
• Memory Caching: Java provides in-memory provide a common way for Java applications to
caching mechanisms using data structures like interact with caching systems, fostering portability
HashMap or specialized libraries like Guava across different caching providers like the ones
Cache or Caffeine. These allow developers to mentioned above.
store key-value pairs in memory, making data
retrieval faster than fetching from the original Key features of JCache include:
source.
• API Standardization: JCache defines a set of
• Distributed Caching: In distributed systems,
standard interfaces and classes for interacting
caching can be extended across multiple nodes
with caches. It is not tied to a specific caching
to share cached data. Libraries and platforms
implementation. Instead, it allows for the
like Ehcache, Apache Ignite, Oracle Coherence,
integration of multiple caching providers that
Infinispan, VMware GemFire, or Redis provide
adhere to the specification.
distributed caching solutions, enabling efficient
sharing of cached data among different • Annotations: JCache introduces annotations
instances of an application. such as @CacheResult, @CachePut, and
@CacheRemove that can be used to control
• Application-Level Caching: Many Java
caching behavior in a declarative manner.
frameworks and libraries offer built-in caching
These annotations are similar to those found in
mechanisms. Spring, for example, provides a
frameworks like Spring.
comprehensive caching abstraction that allows
developers to easily integrate caching into their • Basic Caching Operations: JCache supports
along with their override method and the event remote services, to ensure that requested data is
type they handle. available in the cache for subsequent accesses.
Cache readers are typically used in conjunction
Listener Override Event Type with read-through caching strategies, where cache is
Interface Method treated as the main data store and missing cache
entries are immediately fetched from the integrated
CacheEntryCreat onCreated Cache entry
edListener<K, backend store.
creation
V>
CacheEntryUpdat onUpdated Cache Loader Override Description
Cache entry
edListener<K, Implementatio Method
update
V> n
CacheEntryRemov onRemoved Cache entry javax.cache.Cac load Loads data for
edListener<K, removal heLoader<K, V> the specified
V>
key into the
CacheEntryExpir onExpired Cache entry cache from an
edListener<K, expiration
V> external source.
JMX API FOR JCACHE MANAGEMENT The unwrap method allows developers to obtain the
underlying implementation-specific object
Here’s a table listing some of the most commonly
associated with a specific JCache class or interface.
used JMX APIs for managing and monitoring JCache
This method is useful when developers need to
implementations:
access implementation-specific features or
functionalities that are not provided by the
JMX API Description standard JCache API.
javax.cache.management. Exposes cache
CacheMXBean management and Classes and Interfaces Supporting unwrap:
monitoring
functionalities such as Class/Interface Description
cache statistics, javax.cache.Cache Represents a cache in
configuration details, the JCache API. Allows
and operations through for storing, retrieving,
JMX. and managing cached
javax.cache.management. Provides access to cache key-value pairs.
CacheStatisticsMXBean statistics such as javax.cache.CacheManage Represents a cache
hit/miss counts, eviction r manager in the JCache
counts, and cache size API. Manages the
through JMX. lifecycle of caches and
javax.cache.management. Represents a cache provides cache creation.
CacheManagerMXBean manager’s management javax.cache.Cache.Entry Represents an entry in a
interface, exposing cache. Provides access to
methods for cache the key, value, and
creation, destruction, metadata associated
and management via with the entry.
JMX.
javax.cache.management. Offers cache manager
CacheManagerStatisticsM statistics such as the // Unwrap Cache to Hazelcast ICache
XBean
number of caches ICache<String, String>
created, destroyed, and hazelcastCache = cache.unwrap(
remaining through JMX. ICache.class);
Keep in mind that using this feature is not • Client-side Cache (Embedded mode): In this
recommended if true caching provider portability mode, caching is performed at the client-side,
is what you need, since your application would be typically within the application’s JVM. Cached
coupled to vendor-specific APIs. data is stored locally, reducing the need to fetch
data from remote caches or servers. It can
CACHETOPOLOGIES
CACHE TOPOLOGIESAND
ANDMODES
MODES improve application performance by reducing
network latency and server load. However, it
In the context of caching systems, a cache topology may lead to data inconsistency if the cache is
refers to the arrangement or structure of caches not synchronized with the server-side data.
within a distributed caching environment. Different
• Server-side Cache (Client-Server mode): In
cache topologies offer various trade-offs in terms of
contrast to client-side caching, server-side
performance, scalability, and consistency. Here are
caching stores cached data within cache
some common cache topologies:
servers or nodes. It offloads caching
responsibilities from the client application and
• Single-node Cache: In this topology, there is
centralizes cache management and
only one cache node, typically running on a
coordination. Server-side caching can provide
single server or instance. It’s the simplest form
better control over caching policies and data
of caching and provides basic caching
consistency but may introduce additional
capabilities. However, it lacks scalability and
network latency for cache retrieval operations.
fault tolerance.
• Multiple Independent Caches: Multiple cache Each cache topology and mode has its advantages
nodes operate independently, with each node and disadvantages, and the choice of
managing its own cache. This topology allows topology/mode combination depends on factors
for better scalability compared to a single-node such as application requirements, scalability needs,
cache as multiple cache instances can handle fault tolerance, and data consistency
requests concurrently. However, it lacks data considerations. Organizations often employ a
consistency across caches. combination of cache topologies and modes to meet
their specific caching requirements effectively.
• Replicated Cache: In a replicated cache
topology, the entire dataset is replicated across
RESOURCES
RESOURCES
all cache nodes. This ensures that each cache
node holds a complete copy of the data. It offers
high availability and fault tolerance since any The following resources offer a wealth of
node failure can be mitigated by other replicas. information on caching systems, from basic
However, it may lead to increased network concepts to advanced topics, making them valuable
traffic and memory consumption due to data references for developers, architects, and system
duplication. administrators working with caching technologies.
• Ehcache Documentation:
JCG delivers over 1 million pages each month to more than 700K software
developers, architects and decision makers. JCG offers something for everyone,
including news, tutorials, cheat sheets, research guides, feature articles, source code
and more.
CHEATSHEET FEEDBACK
WELCOME
support@javacodegeeks.com
Copyright © 2014 Exelixis Media P.C. All rights reserved. No part of this publication may be SPONSORSHIP
reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, OPPORTUNITIES
mechanical, photocopying, or otherwise, without prior written permission of the publisher. sales@javacodegeeks.com