| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <!DOCTYPE html> |
| |
| |
| |
| <html lang="en"> |
| <head> |
| <!-- Global site tag (gtag.js) - Google Analytics --> |
| <script async src="https://www.googletagmanager.com/gtag/js?id=UA-1382082-1"></script> |
| <script> |
| window.dataLayer = window.dataLayer || []; |
| function gtag(){dataLayer.push(arguments);} |
| gtag('js', new Date()); |
| |
| gtag('config', 'UA-61232409-1'); |
| </script> |
| |
| |
| |
| <meta charset="UTF-8"> |
| <title>Using Spring Cache With Apache Ignite | Ignite Documentation</title> |
| |
| <link rel="canonical" href="/docs/spring/spring-caching" /> |
| |
| |
| <link rel="stylesheet" href="/assets/css/styles.css?1658382976"> |
| <link rel="stylesheet" href="/assets/css/asciidoc-pygments.css"> |
| <link rel="shortcut icon" href="/favicon.ico"> |
| <meta name='viewport' content='width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0'> |
| |
| <link rel="stylesheet" |
| href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> |
| |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> |
| <script type="text/javascript" src="/assets/js/jquery.swiftype.autocomplete.js?1658382976"></script> |
| <script type="text/javascript" src="/assets/js/anchor.min.js?1658382976"></script> |
| |
| |
| </head> |
| <body> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <header> |
| <!--#include virtual="/includes/promotion_banner.html" --> |
| <div class="container"> |
| <button type='button' class='menu' title='Docs menu'> |
| <img src="/assets/images/menu-icon.svg" width="18" height="12" alt="menu icon" /> |
| </button> |
| <div class='home'> |
| <a href="/" class='home' title='Apache Ignite home'> |
| <img src="/assets/images/apache_ignite_logo.svg" alt="Apache Ignite logo" width="103" height="36" > |
| </a> |
| </div> |
| <form class='search'> |
| <button class="search-close" type='button'><img src='/assets/images/cancel.svg' alt="close" width="10" height="10" /></button> |
| <input type="search" placeholder="Search…" id="search-input"> |
| </form> |
| <button type='button' class='top-nav-toggle'>⋮</button> |
| </div> |
| </header> |
| |
| |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <link rel="stylesheet" href="/assets/css/docs.css"> |
| <section class='page-docs'> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| |
| |
| |
| <nav class='left-nav' data-swiftype-index='false'> |
| |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/aws/aws" class=''>Amazon S3 IP Finder</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/camel/camel-streamer" class=''>Apache Camel Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/flink/flink-streamer" class=''>Apache Flink Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/flume/flume-sink" class=''>Apache Flume Sink</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/azure/azure" class=''>Apache Ignite Azure Module</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/gce/gce" class=''>Apache Ignite GCE Module</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/pub-sub/pub-sub" class=''>Apache Ignite Pub/Sub Module</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/spring/spring-boot" class=''>Apache Ignite and Spring Boot</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/spring/spring-data" class=''>Apache Ignite and Spring Data</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/spring/spring-caching" class=''>Apache Ignite and Spring Cache</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/spring/spring-sessions" class=''>Apache Ignite and Spring Session</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/spring/spring-tx" class=''>Apache Ignite and Spring Transactions</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/kafka/kafka-streamer" class=''>Apache Kafka Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/storm/storm-streamer" class=''>Apache Storm Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/cdc/change-data-capture-extensions" class=''>Change Data Capture Extension</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/jms/jms-streamer" class=''>JMS Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/mqtt/mqtt-streamer" class=''>MQTT Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/perf-statistics/performance-statistics" class=''>Performance Statistics Extension</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/rocketmq/rocketmq-streamer" class=''>RocketMQ Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/topology-validator/topology-validator" class=''>Topology Validator</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/twitter/twitter-streamer" class=''>Twitter Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/zeromq/zeromq-streamer" class=''>ZeroMQ Streamer</a> |
| |
| </li> |
| |
| <li> |
| |
| |
| <a href="/docs/extensions/zookeeper/zookeeper-ip" class=''>ZooKeeper IP Finder</a> |
| |
| </li> |
| |
| </nav> |
| <div class="left-nav__overlay"></div> |
| |
| |
| <article data-swiftype-index='true'> |
| <a class='edit-link' href="/_docs/spring/spring-caching.adoc" target="_blank">Edit</a> |
| |
| <h1>Using Spring Cache With Apache Ignite</h1> |
| |
| <div class="sect1"> |
| <h2 id="overview">Overview</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p><a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html" target="_blank" rel="noopener">Spring Cache</a> |
| abstraction provides an annotation-based way to enable caching for Java methods so that the result of a method execution |
| is stored in an external cache storage. Later, if the same method is called with the same set of parameter values, the result |
| will be retrieved from the cache instead of actually executing the method.</p> |
| </div> |
| <div class="paragraph"> |
| <p>Apache Ignite provides the <code>ignite-spring-cache-ext</code> extension that allows to use Apache Ignite Cache as an external |
| storage for the Spring Cache abstraction. The mentioned above integration is achieved by providing implementations of the |
| <code>CacheManager</code> Spring interface. There are two such implementations: <code>SpringCacheManager</code> and |
| <code>IgniteClientSpringCacheManager</code>, which use either Apache Ignite node or Apache Ignite thin client to connect to the |
| Apache Ignite cluster and perform data caching.</p> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="maven-configuration">Maven Configuration</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>If you use Maven to manage dependencies in your project, you can add Apache Ignite Spring Cache extension |
| dependencies to the application’s <code>pom.xml</code> file like this:</p> |
| </div> |
| <code-tabs><code-tab data-tab=' For all Apache Ignite versions since 2.12.0'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-spring-cache-ext<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite-spring-cache-ext.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-core<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-spring<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite.version}<span class="nt"></version></span> |
| <span class="nt"><exclusions></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-context<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"></exclusions></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-context<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${spring.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='For Apache Ignite 2.11.0'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-spring-cache-ext<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite-spring-cache-ext.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-core<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.ignite<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>ignite-spring<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${ignite.version}<span class="nt"></version></span> |
| <span class="nt"><exclusions></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-context<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-core<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-aop<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-expressions<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-beans<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-jdbc<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"><exclusion></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-tx<span class="nt"></artifactId></span> |
| <span class="nt"></exclusion></span> |
| <span class="nt"></exclusions></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.springframework<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>spring-context<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${spring.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <div class="paragraph"> |
| <p>Replace <code>${ignite-spring-cache-ext.version}</code>, <code>${spring.version}</code>, and |
| <code>${ignite.version}</code> with an actual version of Apache Ignite Spring Cache extension, Spring, and |
| Apache Ignite dependency you need, respectively.</p> |
| </div> |
| <div class="paragraph"> |
| <p>The table below shows available versions of the Apache Ignite Spring Cache extension and compatible versions |
| of the Apache Ignite and Spring.</p> |
| </div> |
| <table class="tableblock frame-all grid-all stripes-even stretch"> |
| <colgroup> |
| <col style="width: 28.5714%;"> |
| <col style="width: 35.7142%;"> |
| <col style="width: 35.7144%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Apache Ignite Spring Cache extension version</th> |
| <th class="tableblock halign-left valign-top">Apache Ignite versions</th> |
| <th class="tableblock halign-left valign-top">Spring versions</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">1.0.0</p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All versions since 2.11.0</p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All versions since 4.3.0</p></td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="apache-ignite-node-cache-manager-configuration">Apache Ignite Node Cache Manager Configuration</h2> |
| <div class="sectionbody"> |
| <div class="sect2"> |
| <h3 id="cluster-connection-configuration">Cluster Connection Configuration</h3> |
| <div class="paragraph"> |
| <p>To plug in an Ignite cache into your Spring-based application that uses Ignite node to connect to Apache Ignite cluster |
| you need to perform just two simple steps:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>Start an Ignite node with proper configuration in embedded mode (i.e., in the same JVM where the application is running). |
| It can already have predefined caches, but it’s not required - caches will be created automatically on first access if required.</p> |
| </li> |
| <li> |
| <p>Configure <code>SpringCacheManager</code> as the cache manager in the Spring application context.</p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>The embedded node can be started by <code>SpringCacheManager</code> itself. In this case you will need to provide a path to either |
| the Ignite configuration XML file or <code>IgniteConfiguration</code> instance via <code>configurationPath</code> or <code>configuration</code> |
| properties respectively (see examples below). Note that setting both is illegal and results in <code>IllegalArgumentException</code>.</p> |
| </div> |
| <h3 id="specifying-apache-ignite-node-configuration" class="discrete">Specifying Apache Ignite Node Configuration</h3> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">SpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="nc">SpringCacheManager</span> <span class="n">mgr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SpringCacheManager</span><span class="o">();</span> |
| |
| <span class="n">mgr</span><span class="o">.</span><span class="na">setConfiguration</span><span class="o">(</span><span class="k">new</span> <span class="nc">IgniteConfiguration</span><span class="o">()</span> |
| <span class="o">.</span><span class="na">setIgniteInstanceName</span><span class="o">(</span><span class="s">"<name of the Ignite node instance>"</span><span class="o">));</span> |
| <span class="c1">// Other required configuration parameters.</span> |
| |
| <span class="k">return</span> <span class="n">mgr</span><span class="o">;</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><beans</span> <span class="na">xmlns=</span><span class="s">"http://www.springframework.org/schema/beans"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xmlns:cache=</span><span class="s">"http://www.springframework.org/schema/cache"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/cache |
| http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span class="nt">></span> |
| <span class="c"><!-- Provide configuration bean. --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.SpringCacheManager"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"configuration"</span><span class="nt">></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.ignite.configuration.IgniteConfiguration"</span><span class="nt">></span> |
| ... |
| <span class="nt"></bean></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Enable annotation-driven caching. --></span> |
| <span class="nt"><cache:annotation-driven/></span> |
| <span class="nt"></beans></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <h3 id="specifying-path-to-apache-ignite-xml-node-configuration-file" class="discrete">Specifying Path to Apache Ignite XML Node Configuration File</h3> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">SpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="nc">SpringCacheManager</span> <span class="n">mgr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SpringCacheManager</span><span class="o">();</span> |
| |
| <span class="n">mgr</span><span class="o">.</span><span class="na">setConfigurationPath</span><span class="o">(</span><span class="s">"<path to an Apache Ignite configuration XML file (path can be absolute or relative to `IGNITE_HOME`)"</span><span class="o">);</span> |
| |
| <span class="k">return</span> <span class="n">mgr</span><span class="o">;</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><beans</span> <span class="na">xmlns=</span><span class="s">"http://www.springframework.org/schema/beans"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xmlns:cache=</span><span class="s">"http://www.springframework.org/schema/cache"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/cache |
| http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span class="nt">></span> |
| <span class="c"><!-- Provide configuration file path. --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.SpringCacheManager"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"configurationPath"</span> <span class="na">value=</span><span class="s">"<path to an Apache Ignite configuration XML file (path can be absolute or relative to `IGNITE_HOME`)"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Enable annotation-driven caching. --></span> |
| <span class="nt"><cache:annotation-driven/></span> |
| <span class="nt"></beans></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <h3 id="specifying-name-of-the-manually-started-apache-ignite-node-instance" class="discrete">Specifying Name of the Manually Started Apache Ignite Node Instance</h3> |
| <div class="paragraph"> |
| <p>It’s possible that you already have an Ignite node running when the cache manager is initialized (e.g., it was started using |
| <code>ServletContextListenerStartup</code>). In this case you should simply provide the grid name via <code>igniteInstanceName</code> property. |
| Note that if you don’t set the grid name as well, the cache manager will try to use the default Ignite instance |
| (the one with the <code>null</code> name). Here is an example:</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">SpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="nc">SpringCacheManager</span> <span class="n">mgr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SpringCacheManager</span><span class="o">();</span> |
| |
| <span class="n">mgr</span><span class="o">.</span><span class="na">setIgniteInstanceName</span><span class="o">(</span><span class="s">"<name of the Apache Ignite node instance>"</span><span class="o">);</span> |
| |
| <span class="k">return</span> <span class="n">mgr</span><span class="o">;</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><beans</span> <span class="na">xmlns=</span><span class="s">"http://www.springframework.org/schema/beans"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xmlns:cache=</span><span class="s">"http://www.springframework.org/schema/cache"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/cache |
| http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span class="nt">></span> |
| <span class="c"><!-- Provide grid name. --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.SpringCacheManager"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"igniteInstanceName"</span> <span class="na">value=</span><span class="s">"<name of the Apache Ignite node instance>"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Enable annotation-driven caching. --></span> |
| <span class="nt"><cache:annotation-driven/></span> |
| <span class="nt"></beans></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <div class="title">Note</div> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>Keep in mind that the node started inside your application is an entry point to the whole topology it connects to. |
| You can start as many remote standalone nodes as you need and all these nodes will participate in caching the data.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="dynamic-caches">Dynamic Caches</h3> |
| <div class="paragraph"> |
| <p>While you can have all required caches predefined in Ignite configuration, it’s not required. If Spring wants to use a |
| cache that doesn’t exist, the <code>SpringCacheManager</code> will automatically create it.</p> |
| </div> |
| <div class="paragraph"> |
| <p>If otherwise not specified, a new cache will be created with default configuration. To customize it, you can provide a configuration |
| template via <code>dynamicCacheConfiguration</code> property. For example, if you want to use <code>REPLICATED</code> caches instead of |
| <code>PARTITIONED</code>, you should configure <code>SpringCacheManager</code> like this:</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">SpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="nc">SpringCacheManager</span> <span class="n">mgr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SpringCacheManager</span><span class="o">();</span> |
| <span class="o">...</span> |
| |
| <span class="n">mgr</span><span class="o">.</span><span class="na">setDynamicCacheConfiguration</span><span class="o">(</span><span class="k">new</span> <span class="nc">CacheConfiguration</span><span class="o"><>(</span><span class="s">"<cache name>"</span><span class="o">)</span> |
| <span class="o">.</span><span class="na">setCacheMode</span><span class="o">(</span><span class="nc">CacheMode</span><span class="o">.</span><span class="na">REPLICATED</span><span class="o">));</span> |
| |
| <span class="k">return</span> <span class="n">mgr</span><span class="o">;</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.SpringCacheManager"</span><span class="nt">></span> |
| ... |
| |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"dynamicCacheConfiguration"</span><span class="nt">></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.ignite.configuration.CacheConfiguration"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"name"</span> <span class="na">value=</span><span class="s">"<cache name>"</span><span class="nt">/></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"cacheMode"</span> <span class="na">value=</span><span class="s">"REPLICATED"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <div class="paragraph"> |
| <p>You can also utilize near caches on client side. To achieve this, simply provide near cache configuration via the |
| <code>dynamicNearCacheConfiguration</code> property. By default, near cache is not created. Here is an example:</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">SpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="nc">SpringCacheManager</span> <span class="n">mgr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SpringCacheManager</span><span class="o">();</span> |
| <span class="o">...</span> |
| |
| <span class="n">mgr</span><span class="o">.</span><span class="na">setDynamicNearCacheConfiguration</span><span class="o">(</span><span class="k">new</span> <span class="nc">NearCacheConfiguration</span><span class="o"><>().</span><span class="na">setNearStartSize</span><span class="o">(</span><span class="mi">1000</span><span class="o">));</span> |
| |
| <span class="k">return</span> <span class="n">mgr</span><span class="o">;</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.SpringCacheManager"</span><span class="nt">></span> |
| ... |
| |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"dynamicNearCacheConfiguration"</span><span class="nt">></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.ignite.configuration.NearCacheConfiguration"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"nearStartSize"</span> <span class="na">value=</span><span class="s">"1000"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="apache-ignite-thin-client-cache-manager-configuration">Apache Ignite Thin Client Cache Manager Configuration</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>This chapter shows how to set up <code>IgniteClientSpringCacheManager</code> that relies on Ignite thin client to connect |
| to the Ignite cluster and perform caching.</p> |
| </div> |
| <div class="admonitionblock important"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <div class="title">Important</div> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p><code>IgniteClientSpringCacheManager</code> does not support Spring Cache synchronous mode |
| (<a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#sync--" target="_blank" rel="noopener">Cacheable#sync</a>). |
| If this feature is crucial to your application, choose the |
| <a href="/docs/extensions/extensions-and-integrations/spring/spring-caching#apache-ignite-node-cache-manager-configuration">SpringCacheManager</a> |
| that uses an Ignite node to connect to Ignite cluster.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="sect2"> |
| <h3 id="cluster-connection-configuration-2">Cluster Connection Configuration</h3> |
| <div class="paragraph"> |
| <p>Cluster connection configuration defines Apache Ignite thin client used by <code>IgniteClientSpringCacheManager</code> to access |
| the cluster. |
| There are several approaches to do this:</p> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <div class="title">Note</div> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>It is incorrect to mix multiple approaches - this results in the <code>IllegalArgumentException</code> exception during the manager startup.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <h3 id="specifying-instance-of-the-apache-ignite-thin-client" class="discrete">Specifying Instance of the Apache Ignite Thin Client</h3> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">IgniteClient</span> <span class="nf">igniteClient</span><span class="o">()</span> <span class="o">{</span> |
| <span class="k">return</span> <span class="nc">Ignition</span><span class="o">.</span><span class="na">startClient</span><span class="o">(</span><span class="k">new</span> <span class="nc">ClientConfiguration</span><span class="o">().</span><span class="na">setAddresses</span><span class="o">(</span><span class="s">"127.0.0.1:10800"</span><span class="o">));</span> |
| <span class="o">}</span> |
| |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">IgniteClientSpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">(</span><span class="nc">IgniteClient</span> <span class="n">cli</span><span class="o">)</span> <span class="o">{</span> |
| <span class="k">return</span> <span class="k">new</span> <span class="nf">IgniteClientSpringCacheManager</span><span class="o">().</span><span class="na">setClientInstance</span><span class="o">(</span><span class="n">cli</span><span class="o">);</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><beans</span> <span class="na">xmlns=</span><span class="s">"http://www.springframework.org/schema/beans"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xmlns:cache=</span><span class="s">"http://www.springframework.org/schema/cache"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/cache |
| http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span class="nt">></span> |
| <span class="c"><!-- |
| Note that org.apache.ignite.IgniteClientSpringBean is available since Apache Ignite 2.11.0 version. |
| For Apache Ignite 2.10.0 and earlier `org.apache.ignite.client.IgniteClient` bean should be created |
| manually with concern of its connection to the Ignite cluster. |
| --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"igniteClient"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.IgniteClientSpringBean"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"clientConfiguration"</span><span class="nt">></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.ignite.configuration.ClientConfiguration"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"addresses"</span><span class="nt">></span> |
| <span class="nt"><list></span> |
| <span class="nt"><value></span>127.0.0.1:10800<span class="nt"></value></span> |
| <span class="nt"></list></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Provide Apache Ignite thin client instance. --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.IgniteClientSpringCacheManager"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"clientInstance"</span> <span class="na">ref=</span><span class="s">"igniteClient"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Use annotation-driven cache configuration. --></span> |
| <span class="nt"><cache:annotation-driven/></span> |
| <span class="nt"></beans></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <h3 id="specifying-apache-ignite-thin-client-configuration" class="discrete">Specifying Apache Ignite Thin Client Configuration</h3> |
| <div class="paragraph"> |
| <p>In this case, Apache Ignite thin client instance is started automatically by the <code>IgniteClientSpringCacheManager</code> based |
| on the provided configuration.</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="nd">@Configuration</span> |
| <span class="nd">@EnableCaching</span> |
| <span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringApplicationConfiguration</span> <span class="o">{</span> |
| <span class="nd">@Bean</span> |
| <span class="kd">public</span> <span class="nc">IgniteClientSpringCacheManager</span> <span class="nf">cacheManager</span><span class="o">()</span> <span class="o">{</span> |
| <span class="k">return</span> <span class="k">new</span> <span class="nf">IgniteClientSpringCacheManager</span><span class="o">()</span> |
| <span class="o">.</span><span class="na">setClientConfiguration</span><span class="o">(</span><span class="k">new</span> <span class="nc">ClientConfiguration</span><span class="o">()</span> |
| <span class="o">.</span><span class="na">setAddresses</span><span class="o">(</span><span class="s">"127.0.0.1:10800"</span><span class="o">));</span> |
| <span class="o">}</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab><code-tab data-tab='XML'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="xml"><span class="nt"><beans</span> <span class="na">xmlns=</span><span class="s">"http://www.springframework.org/schema/beans"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xmlns:cache=</span><span class="s">"http://www.springframework.org/schema/cache"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/cache |
| http://www.springframework.org/schema/cache/spring-cache.xsd"</span><span class="nt">></span> |
| <span class="c"><!-- Provide configuration bean. --></span> |
| <span class="nt"><bean</span> <span class="na">id=</span><span class="s">"cacheManager"</span> <span class="na">class=</span><span class="s">"org.apache.ignite.cache.spring.IgniteClientSpringCacheManager"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"clientConfiguration"</span><span class="nt">></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.ignite.configuration.ClientConfiguration"</span><span class="nt">></span> |
| <span class="nt"><property</span> <span class="na">name=</span><span class="s">"addresses"</span><span class="nt">></span> |
| <span class="nt"><list></span> |
| <span class="nt"><value></span>127.0.0.1:10800<span class="nt"></value></span> |
| <span class="nt"></list></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span> |
| <span class="nt"></property></span> |
| <span class="nt"></bean></span> |
| |
| <span class="c"><!-- Use annotation-driven cache configuration. --></span> |
| <span class="nt"><cache:annotation-driven/></span> |
| <span class="nt"></beans></span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| </div> |
| <div class="sect2"> |
| <h3 id="dynamic-caches-2">Dynamic Caches</h3> |
| <div class="paragraph"> |
| <p>Dynamic Caches configuration for <code>IgniteClientSpringCacheManager</code> is performed the same way as for |
| <a href="/docs/extensions/extensions-and-integrations/spring/spring-caching#dynamic-caches">SpringCacheManager</a> |
| that uses Apache Ignite node instance to access the cluster.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="example">Example</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>Once you have added <code>SpringCacheManager</code> to your Spring application context, you can enable caching for any Java method by simply attaching an annotation to it.</p> |
| </div> |
| <div class="paragraph"> |
| <p>Usually, you would use caching for heavy operations, like database access. For example, let’s assume you have a DAO class with |
| <code>averageSalary(…​)</code> method that calculates the average salary of all employees in an organization. You can use <code>@Cacheable</code> |
| annotation to enable caching for this method:</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="kd">private</span> <span class="nc">JdbcTemplate</span> <span class="n">jdbc</span><span class="o">;</span> |
| |
| <span class="nd">@Cacheable</span><span class="o">(</span><span class="s">"averageSalary"</span><span class="o">)</span> |
| <span class="kd">public</span> <span class="kt">long</span> <span class="nf">averageSalary</span><span class="o">(</span><span class="kt">int</span> <span class="n">organizationId</span><span class="o">)</span> <span class="o">{</span> |
| <span class="nc">String</span> <span class="n">sql</span> <span class="o">=</span> |
| <span class="s">"SELECT AVG(e.salary) "</span> <span class="o">+</span> |
| <span class="s">"FROM Employee e "</span> <span class="o">+</span> |
| <span class="s">"WHERE e.organizationId = ?"</span><span class="o">;</span> |
| |
| <span class="k">return</span> <span class="n">jdbc</span><span class="o">.</span><span class="na">queryForObject</span><span class="o">(</span><span class="n">sql</span><span class="o">,</span> <span class="nc">Long</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">organizationId</span><span class="o">);</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <div class="paragraph"> |
| <p>When this method is called for the first time, <code>SpringCacheManager</code> will automatically create a <code>averageSalary</code> cache. |
| It will also lookup the pre-calculated average value in this cache and return it right away if it’s there. If the average |
| for this organization is not calculated yet, the method will be called and the result will be stored in cache. So next |
| time you request the average salary for this organization, you will not need to query the database.</p> |
| </div> |
| <div class="paragraph"> |
| <p>If the salary of one of the employees is changed, you may want to remove the average value for the organization this |
| employee belongs to, because otherwise the <code>averageSalary(…​)</code> method will return obsolete cached result. This can be |
| achieved by attaching <code>@CacheEvict</code> annotation to a method that updates employee’s salary:</p> |
| </div> |
| <code-tabs><code-tab data-tab='Java'><div class="listingblock"> |
| <div class="content"> |
| <pre class="rouge highlight"><code data-lang="java"><span class="kd">private</span> <span class="nc">JdbcTemplate</span> <span class="n">jdbc</span><span class="o">;</span> |
| |
| <span class="nd">@CacheEvict</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"averageSalary"</span><span class="o">,</span> <span class="n">key</span> <span class="o">=</span> <span class="s">"#e.organizationId"</span><span class="o">)</span> |
| <span class="kd">public</span> <span class="kt">void</span> <span class="nf">updateSalary</span><span class="o">(</span><span class="nc">Employee</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> |
| <span class="nc">String</span> <span class="n">sql</span> <span class="o">=</span> |
| <span class="s">"UPDATE Employee "</span> <span class="o">+</span> |
| <span class="s">"SET salary = ? "</span> <span class="o">+</span> |
| <span class="s">"WHERE id = ?"</span><span class="o">;</span> |
| |
| <span class="n">jdbc</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">sql</span><span class="o">,</span> <span class="n">e</span><span class="o">.</span><span class="na">getSalary</span><span class="o">(),</span> <span class="n">e</span><span class="o">.</span><span class="na">getId</span><span class="o">());</span> |
| <span class="o">}</span></code></pre> |
| </div> |
| </div></code-tab></code-tabs> |
| <div class="paragraph"> |
| <p>After this method is called, average value for the provided employee’s organization will be evicted from the <code>averageSalary</code> cache. |
| This will force <code>averageSalary(…​)</code> to recalculate the value next time it’s called.</p> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <div class="title">Note</div> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>Note that this method receives employee as a parameter, while average values are saved in cache by <code>organizationID</code>. |
| To explicitly specify what is used as a cache key, we used key parameter of the annotation and Spring Expression Language.</p> |
| </div> |
| <div class="paragraph"> |
| <p>The <code>#e.organizationId</code> expression means that we need to extract the value of <code>organizationId</code> property from <code>e</code> variable. |
| Essentially, <code>getOrganizationId()</code> method will be called on provided employee object and the returned value will be used as the cache key.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| </div> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <div class="copyright"> |
| © 2022 The Apache Software Foundation.<br/> |
| Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation. |
| |
| </div> |
| |
| </article> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <nav class="right-nav" data-swiftype-index='false'> |
| <ul class="sectlevel1"> |
| <li><a href="#overview">Overview</a></li> |
| <li><a href="#maven-configuration">Maven Configuration</a></li> |
| <li><a href="#apache-ignite-node-cache-manager-configuration">Apache Ignite Node Cache Manager Configuration</a> |
| <ul class="sectlevel2"> |
| <li><a href="#cluster-connection-configuration">Cluster Connection Configuration</a></li> |
| <li><a href="#dynamic-caches">Dynamic Caches</a></li> |
| </ul> |
| </li> |
| <li><a href="#apache-ignite-thin-client-cache-manager-configuration">Apache Ignite Thin Client Cache Manager Configuration</a> |
| <ul class="sectlevel2"> |
| <li><a href="#cluster-connection-configuration-2">Cluster Connection Configuration</a></li> |
| <li><a href="#dynamic-caches-2">Dynamic Caches</a></li> |
| </ul> |
| </li> |
| <li><a href="#example">Example</a></li> |
| </ul> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| |
| <footer> |
| </footer> |
| |
| </nav> |
| |
| </section> |
| <script type='module' src='/assets/js/code-copy-to-clipboard.js' async></script> |
| |
| <script> |
| // inits deep anchors -- needs to be done here because of https://www.bryanbraun.com/anchorjs/#dont-run-it-too-late |
| anchors.add('.page-docs h1, .page-docs h2, .page-docs h3:not(.discrete), .page-docs h4, .page-docs h5'); |
| anchors.options = { |
| placement: 'right', |
| visible: 'always' |
| }; |
| </script> |
| </body> |
| <script type='module' src='/assets/js/index.js?1658382976' async></script> |
| </html> |