89
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission. Caching with Spring: Advanced Topics and Best Practices Michael Plöd @bitboss

Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Embed Size (px)

DESCRIPTION

Caching is relevant for a wide range of business applications and there is a huge variety of products in the market ranging from easy to adopt local heap based caches to powerful distributed data grids. This talk addresses advanced usage of Spring’s caching abstraction such as integrating a cache provider that is not integrated by the default Spring Package. In addition to that I will also give an overview of the JCache Specification and it’s adoption in the Spring ecosystem. Finally the presentation will also address various best practices for integrating various caching solutions into enterprise grade applications that don’t have the luxury of having „eventual consistency“ as a non-functional requirement.

Citation preview

Page 1: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Caching with Spring: Advanced Topics and Best Practices

Michael Plöd@bitboss

Page 2: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

I will talk aboutCaching Types / Topologies

Best Practices for Caching in Enterprise Applications Caching with Spring JCache and Spring

I will NOT talk aboutLatency / Synchronization discussion

What is the best caching product on the market HTTP / Database Caching

Caching in JPA, Hibernate or other ORMs

Page 3: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache!/ kæʃ /!In computing, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes.

Source: http://en.wikipedia.org/wiki/Cache_(computing)

Page 4: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

That’s awesome. Let’s cache everything and everywhere and distribute it all in

a Cluster in a transactional manner ohhh by the way: Twitter has been

doing that for ages

Are you crazy?

Page 5: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Business-Applications

!= Twitter / Facebook & co.

Page 6: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Many enterprise grade projects are adapting caching too

defensive or too offensive and are running into consistency or

performance issues because of that

Page 7: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

But with a well adjusted caching strategy you will make your

application more scalable, faster and cheaper to operate.

Page 8: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

CACHESTypes of

Places for

Local Cache, Data Grid, Document Store, JPA First Level Cache, JPA Second Level Cache,

Hybrid Cache

Database, Heap, HTTP Proxy, Browser, Prozessor, Disk, Off Heap, Persistence-

Framework, Application

Page 9: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

We will focus on local and distributed caching at the

application level with the Spring Framework

Page 10: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 11: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 12: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

1 Identify suitable layers for caching

Page 13: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

ComplaintManagementRestController

ComplaintManagementBusinessService

DataAggrgationManager

Host!Commands

SAP!Commands

Spring Data Repository

HTTP Caching

Read Operations

Read Operations

Read Operations

Read Operations

Read and Write

Operations

Suitable Layers

for Caching

Page 14: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

2 Stay local as long as possible

Page 15: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Lokal In-Memory

JVM

Cache

Page 16: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

ClusteredJVM

Cache

JVM

Cache

JVM

Cache

JVM

Cache

Page 17: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 18: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

JVM

JVM

JVM

JVM

Clustered - with sync

Cache

Cache

Cache

Cache

Page 19: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

JVM

JVM

JVM

JVM

Clustered - with sync

Cache

Cache

Cache

Cache

Invalidation

Replication

Page 20: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

3 Avoid real replication where possible

Page 21: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

Invalidation - Option 1

Page 22: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

Invalidation - Option 1

#1PUT (Insert)

PUT (Insert)

#1

#1PUT (Insert)

PUT (Insert)

#1

Page 23: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

#1 #1

#1#1

Invalidation - Option 1

Page 24: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

#1 #1

PUT (Update)

#1#1

Invalidation - Option 1

Page 25: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

PUT (Update)

#1

Invalidation - Option 1

Page 26: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

Invalidation - Option 2

Page 27: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

Invalidation - Option 2

PUT (Insert)

#1

Page 28: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

Replication

Page 29: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

#1

#1 #1

Replication

#1PUT (Insert)

Page 30: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

#1

#1 #1

Replication

#1

Page 31: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Cache

Cache

Cache

Cache

#1

#1 #1

Replication

#1 PUT (Update)

Page 32: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

As of now every cache could potentially hold every data which

consumes heap memory

Page 33: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Big Heap

?

Page 34: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 35: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

4 Avoid big heaps just for caching

Page 36: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Big heap leads to long major GCs

Application Data

Cache

32 G

B

Page 37: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Long GCs can destabilize your cluster

JVM

Cache

JVM

Cache

JVM

Cache

JVM

Cache

Page 38: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Small caches are a bad idea!

"

Many evictions, fewer hits, no „hot data“.

This is especially critical for

replicating caches.

Page 39: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

5 Use a distributed cache for big amounts of data

Page 40: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Distributed CachesJVM

JVM JVM

JVM

Cache Node

1

Cache Node

2

Cache Node

3

Page 41: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

1Customer #23

Customer #30

Customer #27

Customer #32

Page 42: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

1 2Customer #23

Customer #30

Customer #27

Customer #32

BACKUP #27

BACKUP #32

BACKUP #23

BACKUP #30

Data is being distributed and

backed up

Page 43: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

3

1 2Customer #23

Customer #30

Customer #27

Customer #32

BACKUP #27

BACKUP #32

BACKUP #23

BACKUP #30

Page 44: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

43

1 2Customer #23

Customer #30

Customer #27

Customer #32

BACKUP #27

BACKUP #32

BACKUP #23

BACKUP #30

Page 45: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

DEMO Hazelcast

Page 46: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

A distributed cache leads to smaller heaps, more capacity and

is easy to scale

Application Data

Cache

2 - 4

GB

… Cache

Page 47: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

6 The operations specialist is your new best friend

Page 48: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Clustered caches are complex. Please make sure that operations and networking are involved as early as

possible.

Page 49: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 50: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

7 Make sure that only suitable data gets cached

Page 51: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

The best cache candidates are read-mostly data, which are

expensive to obtain

Page 52: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

If you urgently must cache write-intensive data make sure to use a

distributed cache and not a replicated or invalidating one

Page 53: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 54: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

8 Only use existing cache implementations

Page 55: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

NEVER write your own cache

implementation

EVER

Page 56: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

CACHEImplementations

Infinispan, EHCache, Hazelcast, Couchbase, Memcache, OSCache, SwarmCache, Xtreme

Cache, Apache DirectMemory

Terracotta, Coherence, Gemfire, Cacheonix, WebSphere eXtreme Scale, Oracle 12c In

Memory Database

Page 57: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Which data shall I cache?

Where shall I cache?

Which cache shall I use?

Which impact does it have on my infrastructure

How about data-consistency

How do I introduce caching?

How about caching in Spring?

Page 58: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

9 Introduce Caching in three steps

Page 59: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Optimize your application

Local Cache Distributed Cache

Performance Boost

Performance Loss

Page 60: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

10 Optimize Serialization

Page 61: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Example: Hazelcast putting and getting 10.000 objects locally

GET Time PUT Time Payload Size

Serializable ? ? ?

DataSerializable ? ? ?

IdentifierData

Serializable? ? ?

Page 62: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

DEMO Serialization

Page 63: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Example: Hazelcast putting and getting 10.000 objects locally

GET Time PUT Time Payload Size

Serializable 1287 ms 1220 ms 1164 byte

DataSerializable 443 ms 408 ms 916 byte

IdentifierData

Serializable264 ms 207 ms 882 byte

Page 64: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

JAVA SERIALIZATIONSUCKS

for Caching if alternatives are present

Page 65: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

11Use Off-Heap Storage for Cache instances with more than 4 GB Heap Size

Page 66: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Off Heap

30 G

B RA

M

JVM

Cache Runtime

Cache Data

2 G

B H

EAP

No Garbage Collection

Very short Garbage Collections

Page 67: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

12 Mind the security gap

Page 68: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Application

„CRM“ „Host“ DB

SecuritySecuritySecurity

CacheCRM Data

SAP Data

DB Data?

Mind security when reading data from the cache

Page 69: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

I <3 Spring "

Bot

censored

I’m at a Spring conference

and this guy is 50 slides in and hasn’t

yet mentioned Spring even if he advertised

Page 70: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

13 Abstract your cache provider

Page 71: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public Account retrieveAccount(String accountNumber) {! Cache cache = ehCacheMgr.getCache(„accounts“);! Account account = null;! Element element = cache.get(accountNumber);! if(element == null) {! //execute some business logic for retrieval! //account = result of logic above! cache.put(new Element(accountNumber, account));! } else {! account = (Account)element.getObjectValue();! }! return account;!}

Tying your code to a cache provider is bad practice

Page 72: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public Account retrieveAccount(String accountNumber) {! Cache cache = ehCacheMgr.getCache(„accounts“);! Account account = null;! Element element = cache.get(accountNumber);! if(element == null) {! //execute some business logic for retrieval! //account = result of logic above! cache.put(new Element(accountNumber, account));! } else {! account = (Account)element.getObjectValue();! }! return account;!}

Try switching from EHCache to Hazelcast

You will have to

adjust these lines of code

to the Hazelcast

API

Page 73: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public Account retrieveAccount(String accountNumber) {! Cache cache = ehCacheMgr.getCache(„accounts“);! Account account = null;! Element element = cache.get(accountNumber);! if(element == null) {! //execute some business logic for retrieval! //account = result of logic above! cache.put(new Element(accountNumber, account));! } else {! account = (Account)element.getObjectValue();! }! return account;!}

You can’t switch cache providers between environments

EHCache is tightly

coupled to your code

Page 74: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public Account retrieveAccount(String accountNumber) {! Cache cache = ehCacheMgr.getCache(„accounts“);! Account account = null;! Element element = cache.get(accountNumber);! if(element == null) {! //execute some business logic for retrieval! //account = result of logic above! cache.put(new Element(accountNumber, account));! } else {! account = (Account)element.getObjectValue();! }! return account;!}

You mess up your business logic with infrastructure

This is all caching

related code without any

business relevance

Page 75: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

<cache:annotation-driven cache-manager="ehCacheManager"/>!"

<!-- EH Cache local -->!<bean id="ehCacheManager" ! class="org.springframework.cache.ehcache.EhCacheCacheManager"! p:cacheManager-ref="ehcache"/>!! !<bean id="ehcache" ! class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"!! p:configLocation="/ehcache.xml"/>

@Cacheable("Customers")!public Customer getCustomer(String customerNumber) {!! …!}

Introducing Spring’s cache abstraction

Page 76: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Spring’s Caching Annotations

Annotation Description

@Cacheable Demarcates cachable methods, can read and write to the cache(s)

@CacheEvict Demarcates methods that perform cache eviction, that is methods that act as triggers for removing data from the cache.

@CachePut Updates the cache with the annotated method’s return value. Will always execute the method.

@Caching Allows multiple nested @Cacheable, @CacheEvict and @CachePut annotations to be used on the same method

@CacheConfigClass-level annotation that allows to share the cache names, the custom

KeyGenerator, the custom CacheManager and finally the custom CacheResolver. Does not enable caching.

Page 77: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Default Key Generation Strategy

@Cacheable("Customers")!public Customer getCustomer(String customerNumber) {!! …!}

@Cacheable("CustomerList")!public List<Customer> listCustomers(int start, int count) {!! …!}

@Cacheable("MonthlyReport")!public Report getMonthlyReport() {!! …!}

customerNumber

SimpleKey containingstart and count

SimpleKey.EMPTY

KeyAnnotation

Page 78: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public class MyOwnKeyGenerator implements KeyGenerator {! @Override! public Object generate(Object target, Method method, Object... params) {! if (params.length == 0) {! return new SimpleKey("EMPTY");! }! if (params.length == 1) {! Object param = params[0];! if (param != null && !param.getClass().isArray()) {! return param;! }! }! return new SimpleKey(params);! }!}

You need a custom default KeyGenerator?

<cache:annotation-driven cache-manager="hazelcastCacheManager" keyGenerator="myOwnKeyGenerator" />

Page 79: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

SpEL in Caching Annotations

@Cacheable("concerts", key="#location.id")!public List<Concert> findConcerts(Location location)

@Cacheable("concerts", key="T(someType).hash(#location)")!public List<Concert> findConcerts(Location location)

@Cacheable("concerts", condition="#location.city == 'Dallas')", unless="#location.outOfBusiness")!public List<Concert> findConcerts(Location location)

Key: id of location

EffectAnnotation

@CachePut("locations", key="#result.id")!public Location saveLocation(Location location)

Key: hashCode of location

Conditional Caching if Location is in Dallas in operating

Key: generated id of result

Page 80: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

I have multiple Caches and Cache Managers!

@Cacheable("concerts", cacheManager="hazelCastCacheManager")!public List<Concert> findConcerts(Location location)

@Cacheable("bands", cacheManager="gemfireCacheManager"))!public List<Band> listBand(int start, int count)

@Cacheable("bands", cacheResolver="myOwnCacheResolver"))!public List<Band> listBand(int start, int count)

Programmatic resolution through an implementation of the CacheResolver Interface

Manual Assignment

Manual Assignment

Page 81: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

public class MyOwnCacheResolver extends AbstractCacheResolver {! @Autowired public MyOwnCacheResolver(CacheManager cacheManager) { super(cacheManager); } protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {! return getCacheNames(context.getTarget().getClass());! }!"

private getCacheNames(Class<?> businessServiceClass) { ... }!}

Working with CacheResolvers

@Cacheable("bands", cacheResolver="myOwnCacheResolver"))!public List<Band> listBand(int start, int count)

Page 82: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

You can use your own custom Annotations

@Retention(RetentionPolicy.RUNTIME)!@Target({ElementType.METHOD})!@Cacheable("concerts", key="id")!public @interface DefaultConcertCacheable {!}

@DefaultConcertCacheable!public Concert getConcert(Long id)

Page 83: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Spring 4.x is the first commerically supported

container with JCache (JSR-107) Support!

That’s years ahead of

any JEE Server

Page 84: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Spring vs JCache Annotations

Spring JCache Description

@Cacheable @CacheResult Similar, but @CacheResult can cache Exceptions and force method execution

@CacheEvict @CacheRemove Similar, but @CacheRemove supports eviction in the case of Exceptions

@CacheEvict (removeAll=true) @CacheRemoveAll Same rules as for @CacheEvict vs @CacheRemove

@CachePut @CachePutDifferent semantic: cache content must be annotated with

@CacheValue. JCache brings Exception caching and caching before or after method execution

@CacheConfig @CachePut Identical

Page 85: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Except for the dependencies JCache API and spring-context-support no further steps need to

be taken to enable JCache Annotations in Spring

Applications

Page 86: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

? Your Cache Provider has no

integration for Spring or JCache

Page 87: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Code Walkthrough

CacheManager & Cache API

Page 88: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

How do I disable caching for Unit Tests?

<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">! <property name="cacheManagers">! <list>! <ref bean="guavaCache"/>! <ref bean="ehCache"/>! </list>! </property>! <property name="fallbackToNoOpCache" value="true"/>!</bean>

Page 89: Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES

Thank you!

82

https://github.com/mploed

@bitboss

Michael Plöd - Freelance Consultant

http://slideshare.net/mploed