99 lines
21 KiB
HTML
99 lines
21 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>6. Caching</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="multi_spring-cloud-aws.html" title="Spring Cloud AWS"><link rel="up" href="multi_spring-cloud-aws.html" title="Spring Cloud AWS"><link rel="prev" href="multi__messaging.html" title="5. Messaging"><link rel="next" href="multi__data_access_with_jdbc.html" title="7. Data Access with JDBC"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">6. Caching</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__messaging.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__data_access_with_jdbc.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_caching" href="#_caching"></a>6. Caching</h1></div></div></div><p>Caching in a cloud environment is useful for applications to reduce the latency and to save database round trips.
|
|
Reducing database round trips can significantly reduce the requirements for the database instance. The Spring Framework
|
|
provides, since version 3.1, a unified Cache abstraction to allow declarative caching in applications analogous to the
|
|
declarative transactions.</p><p>Spring Cloud AWS integrates the <a class="link" href="http://aws.amazon.com/elasticache/" target="_top">Amazon ElastiCache</a> service into the Spring unified
|
|
caching abstraction providing a cache manager based on the memcached and Redis protocols. The caching support for Spring
|
|
Cloud AWS provides its own memcached implementation for ElastiCache and uses
|
|
<a class="link" href="http://projects.spring.io/spring-data-redis/" target="_top">Spring Data Redis</a> for Redis caches.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_dependencies_for_redis_caches" href="#_configuring_dependencies_for_redis_caches"></a>6.1 Configuring dependencies for Redis caches</h2></div></div></div><p>Spring Cloud AWS delivers its own implementation of a memcached cache, therefore no other dependencies are needed. For Redis
|
|
Spring Cloud AWS relies on Spring Data Redis to support caching and also to allow multiple Redis drivers to be used. Spring
|
|
Cloud AWS supports all Redis drivers that Spring Data Redis supports (currently Jedis, JRedis, SRP and Lettuce) with Jedis
|
|
being used internally for testing against ElastiCache. A dependency definition for Redis with Jedis is shown in the example</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependencies></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><groupId></span>org.springframework.data<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></groupId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><artifactId></span>spring-data-redis<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></artifactId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><version></span>${spring-data-redis.version}<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></version></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><groupId></span>redis.clients<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></groupId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><artifactId></span>jedis<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></artifactId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><version></span>2.6.1<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></version></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependencies></span></pre><p>Spring Cloud AWS will automatically detect the Redis driver and will use one of them automatically.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_caching_with_xml" href="#_configuring_caching_with_xml"></a>6.2 Configuring caching with XML</h2></div></div></div><p>The cache support for Spring Cloud AWS resides in the context module and can therefore be used if the context module
|
|
is already imported in the project. The cache integration provides its own namespace to configure cache clusters that are
|
|
hosted in the Amazon ElastiCache service. The next example contains a configuration for the cache cluster and the Spring
|
|
configuration to enable declarative, annotation-based caching.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xmlns:aws-cache</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/cloud/aws/cache"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xmlns:cache</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/cache"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xmlns</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/beans"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xsi:schemaLocation</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/cloud/aws/cache
|
|
http://www.springframework.org/schema/cloud/aws/cache/spring-cloud-aws-cache.xsd
|
|
http://www.springframework.org/schema/cache
|
|
http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">></span>
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-context:context-credentials></span>
|
|
...
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></aws-context:context-credentials></span>
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-cluster</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"CacheCluster"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></aws-cache:cache-manager></span>
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><cache:annotation-driven /></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></beans></span></pre><p>The configuration above configures a <code class="literal">cache-manager</code> with one cache with the name <code class="literal">CacheCluster</code> that represents an
|
|
<a class="link" href="http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/ManagingCacheClusters.html" target="_top">ElasticCache cluster</a>.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_mixing_caches" href="#_mixing_caches"></a>6.2.1 Mixing caches</h3></div></div></div><p>Applications may have the need for multiple caches that are maintained by one central cache cluster. The Spring Cloud
|
|
AWS caching support allows to define multiple caches inside one cache manager and also to use externally defined caches
|
|
inside the cache manager.</p><p>The example below demonstrates a configuration example that contains a pre-configured cache with a <code class="literal">cache-ref</code> element
|
|
(which might be a local cache) and a <code class="literal">cache-cluster</code> configuration for ElastiCache cache clusters.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"><aws-cache:cache-manager</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">id</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"cacheManager"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-ref</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">ref</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"memcached"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-cluster</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"SimpleCache"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">/></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></beans></span></pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_defining_expiration" href="#_defining_expiration"></a>6.2.2 Defining expiration</h3></div></div></div><p>The Spring cache demarcation does not support expiry time configuration and leaves it up to the cache implementation
|
|
to support an expiry time. The Spring Cloud AWS cache configuration supports the expiry time setting per cache. The
|
|
expiry time will be passed to the memcached service.</p><p>The <code class="literal">cache-cluster</code> element accepts an expiration attribute that defines the expiration time in seconds.
|
|
No configured values implies that there is an infinite expiration time.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><beans></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-cluster</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">expiration</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"10000"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"CacheCluster"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></beans></span></pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_caching_using_java_configuration" href="#_configuring_caching_using_java_configuration"></a>6.3 Configuring caching using Java configuration</h2></div></div></div><p>Spring Cloud AWS also support the cache configuration with Java configuration classes. On any <code class="literal">Configuration</code> class,
|
|
the caching can be configured using the <code class="literal">org.springframework.cloud.aws.cache.config.annotation.EnableElastiCache</code>
|
|
annotation provided by Spring Cloud AWS. The next example shows a configuration of two cache clusters.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@EnableElastiCache({@CacheClusterConfig(name = "firstCache"), @CacheClusterConfig(name = "secondCache")})</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationConfiguration {
|
|
}</pre><div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Important"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="images/important.png"></td><th align="left">Important</th></tr><tr><td align="left" valign="top"><p>If you leave the <code class="literal">value</code> attribute empty, then all the caches inside your CloudFormation stack (if available)
|
|
will be configured automatically.</p></td></tr></table></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_configuring_expiry_time_for_caches" href="#_configuring_expiry_time_for_caches"></a>6.3.1 Configuring expiry time for caches</h3></div></div></div><p>The Java configuration also allows to configure the expiry time for the caches. This can be done for all
|
|
caches using the <code class="literal">defaultExpiration</code> attribute as shown in the example below.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@EnableElastiCache(defaultExpiration = 23)</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationConfiguration {
|
|
}</pre><p>The expiration can be defined on a cache level using the <code class="literal">@CacheClusterConfig</code> annotations expiration attribute as shown below (using seconds as
|
|
the value).</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@EnableElastiCache({@CacheClusterConfig(name = "firstCache", expiration = 23), @CacheClusterConfig(name = "secondCache", expiration = 42)})</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationConfiguration {
|
|
}</pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_caching_in_spring_boot" href="#_configuring_caching_in_spring_boot"></a>6.4 Configuring caching in Spring Boot</h2></div></div></div><p>The caches will automatically be configured in Spring Boot without any explicit configuration property.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_caching" href="#_using_caching"></a>6.5 Using caching</h2></div></div></div><p>Based on the configuration of the cache, developers can annotate their methods to use the caching for method return values.
|
|
The next example contains a caching declaration for a service for which the return values should be cached</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Service</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ExpensiveService {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Cacheable("CacheCluster")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String calculateExpensiveValue(String key) {
|
|
...
|
|
}
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_memcached_client_implementation" href="#_memcached_client_implementation"></a>6.6 Memcached client implementation</h2></div></div></div><p>There are different memcached client implementations available for Java, the most prominent ones are
|
|
<a class="link" href="https://github.com/couchbase/spymemcached" target="_top">Spymemcached</a> and <a class="link" href="https://github.com/killme2008/xmemcached" target="_top">XMemcached</a>.
|
|
Amazon AWS supports a dynamic configuration and delivers an enhanced memcached client based on Spymemcached to support the
|
|
<a class="link" href="http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/AutoDiscovery.html" target="_top">auto-discovery</a> of new nodes based on
|
|
a central configuration endpoint.</p><p>Spring Cloud AWS relies on the Amazon ElastiCache Client implementation and therefore has a dependency on that.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_cloudformation_2" href="#_using_cloudformation_2"></a>6.7 Using CloudFormation</h2></div></div></div><p>Amazon ElastiCache clusters can also be configured within a stack and then be used by applications. Spring Cloud AWS
|
|
also supports the lookup of stack-configured cache clusters by their logical name with the resolution to the physical
|
|
name. The example below shows a cache cluster configuration inside a CloudFormation template.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CacheCluster"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Type"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"AWS::ElastiCache::CacheCluster"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Properties"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"AutoMinorVersionUpgrade"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"true"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Engine"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"memcached"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CacheNodeType"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"cache.t2.micro"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CacheSubnetGroupName"</span> : <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"sample"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"NumCacheNodes"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"1"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"VpcSecurityGroupIds"</span>: [<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"sample1"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>The cache cluster can then be used with the name <code class="literal">CacheCluster</code> inside the application configuration as shown below:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><beans...></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><aws-cache:cache-cluster</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"CacheCluster"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">expiration</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"15"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">/></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></aws-cache:cache-manager></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><beans></span></pre><p>With the configuration above the application can be deployed with multiple stacks on different environments
|
|
without any configuration change inside the application.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__messaging.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__data_access_with_jdbc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5. Messaging </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-aws.html">Home</a></td><td width="40%" align="right" valign="top"> 7. Data Access with JDBC</td></tr></table></div></body></html> |