Files
spring-cloud-static/spring-cloud-aws/1.2.2.RC1/multi/multi__caching.html
2017-10-20 16:07:36 -04:00

99 lines
21 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>6.&nbsp;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.&nbsp;Messaging"><link rel="next" href="multi__data_access_with_jdbc.html" title="7.&nbsp;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.&nbsp;Caching</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__messaging.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<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.&nbsp;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&nbsp;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">&lt;dependencies&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;dependency&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;groupId&gt;</span>org.springframework.data<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/groupId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;artifactId&gt;</span>spring-data-redis<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/artifactId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;version&gt;</span>${spring-data-redis.version}<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/version&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/dependency&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;dependency&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;groupId&gt;</span>redis.clients<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/groupId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;artifactId&gt;</span>jedis<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/artifactId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;version&gt;</span>2.6.1<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/version&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/dependency&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/dependencies&gt;</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&nbsp;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">&lt;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">&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-context:context-credentials&gt;</span>
...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-context:context-credentials&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;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"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;cache:annotation-driven /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</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&nbsp;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">&lt;beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;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">&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;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"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;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">/&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</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&nbsp;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">&lt;beans&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;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"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</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&nbsp;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&nbsp;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&nbsp;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&nbsp;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&nbsp;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&nbsp;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">&lt;beans...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;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">/&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-cache:cache-manager&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans&gt;</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>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__data_access_with_jdbc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.&nbsp;Messaging&nbsp;</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">&nbsp;7.&nbsp;Data Access with JDBC</td></tr></table></div></body></html>