[关闭]
@liyuj 2017-04-12T15:03:29.000000Z 字数 5016 阅读 2932

Apache-Ignite-1.9.0-中文开发手册

10.Spring

10.1.Spring缓存

10.1.1.摘要

Ignite提供了一个SpringCacheManager-一个Spring缓存抽象的实现。他提供了基于注解的方式来启用Java方法的缓存,这样方法的执行结果就会存储在Ignite缓存中。如果之后同一个方法通过同样的参数集被调用,结果会直接从缓存中获得而不是实际执行这个方法。

Spring缓存抽象文档
关于如何使用Spring缓存抽象的更多信息,包括可用的注解,可以参照这个文档页面:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html.

10.1.2.如何启用缓存

只需要两个简单的步骤就可以将Ignite缓存嵌入基于Spring的应用:

嵌入式节点可以通过SpringCacheManager自己启动,这种情况下需要分别通过configurationPath或者configuration属性提供一个Ignite配置文件的路径或者IgniteConfigurationBean(看下面的例子)。注意同时设置两个属性是非法的以及抛出IllegalArgumentException
配置路径:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:cache="http://www.springframework.org/schema/cache"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/cache
  8. http://www.springframework.org/schema/cache/spring-cache.xsd">
  9. <!-- Provide configuration file path. -->
  10. <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
  11. <property name="configurationPath" value="examples/config/spring-cache.xml"/>
  12. </bean>
  13. <!-- Enable annotation-driven caching. -->
  14. <cache:annotation-driven/>
  15. </beans>

配置Bean:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:cache="http://www.springframework.org/schema/cache"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/cache
  8. http://www.springframework.org/schema/cache/spring-cache.xsd">
  9. <-- Provide configuration bean. -->
  10. <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
  11. <property name="configuration">
  12. <bean class="org.apache.ignite.configuration.IgniteConfiguration">
  13. ...
  14. </bean>
  15. </property>
  16. </bean>
  17. <-- Enable annotation-driven caching. -->
  18. <cache:annotation-driven/>
  19. </beans>

当缓存管理器初始化时也可能已经有一个Ignite节点正在运行(比如已经通过ServletContextListenerStartup启动了)。这时只需要简单地通过gridName属性提供网格名字就可以了。注意如果不设置网格名字,缓存管理器会试图使用默认的Ignite实例(名字为空的),下面是一个示例:
使用已启动的Ignite实例:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:cache="http://www.springframework.org/schema/cache"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/cache
  8. http://www.springframework.org/schema/cache/spring-cache.xsd">
  9. <!-- Provide grid name. -->
  10. <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
  11. <property name="gridName" value="myGrid"/>
  12. </bean>
  13. <!-- Enable annotation-driven caching. -->
  14. <cache:annotation-driven/>
  15. </beans>

远程节点
注意应用内部启动的节点只是希望连接的网络的一个入口,可以根据需要通过Ignite发行版提供的bin/ignite.{sh|bat}脚本启动尽可能多的远程独立节点,所有这些节点都会参与缓存数据。

10.1.3.动态缓存

虽然通过Ignite配置文件可以获得所有必要的缓存,但是这不是必要的。如果Spring要使用一个不存在的缓存时,SpringCacheManager会自动创建它。
如果不指定,会使用默认值创建一个新的缓存。要定制的话,可以通过dynamicCacheConfiguration属性提供一个配置模板,比如,如果希望使用复制缓存而不是分区缓存,可以像下面这样配置SpringCacheManager:

  1. <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
  2. ...
  3. <property name="dynamicCacheConfiguration">
  4. <bean class="org.apache.ignite.configuration.CacheConfiguration">
  5. <property name="cacheMode" value="REPLICATED"/>
  6. </bean>
  7. </property>
  8. </bean>

也可以在客户端侧使用近缓存,要做到这一点只需要简单地通过dynamicNearCacheConfiguration属性提供一个近缓存配置即可。默认的话近缓存是不启用的,下面是一个例子:

  1. <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
  2. ...
  3. <property name="dynamicNearCacheConfiguration">
  4. <bean class="org.apache.ignite.configuration.NearCacheConfiguration">
  5. <property name="nearStartSize" value="1000"/>
  6. </bean>
  7. </property>
  8. </bean>

10.1.4.示例

一旦在Spring应用上下文中加入了SpringCacheManager,就可以通过简单地加上注解为任意的java方法启用缓存。
通常为很重的操作使用缓存,比如数据库访问。比如,假设有个Dao类有一个averageSalary(...)方法,他计算一个组织内的所有雇员的平均工资,那么可以通过@Cacheable注解来开启这个方法的缓存。

  1. private JdbcTemplate jdbc;
  2. @Cacheable("averageSalary")
  3. public long averageSalary(int organizationId) {
  4. String sql =
  5. "SELECT AVG(e.salary) " +
  6. "FROM Employee e " +
  7. "WHERE e.organizationId = ?";
  8. return jdbc.queryForObject(sql, Long.class, organizationId);
  9. }

当这个方法第一次被调用时,SpringCacheManager会自动创建一个averageSalary缓存,他也会在缓存中查找事先计算好的平均值然后如果存在的话就会直接返回,如果这个组织的平均值还没有被计算过,那么这个方法就会被调用然后将结果保存在缓存中,因此下一次请求这个组织的平均值,就不需要访问数据库了。

缓存键
因为organizationId是唯一的方法参数,所以他会自动作为缓存键。

如果一个雇员的工资发生变化,可能希望从缓存中删除这个雇员所属组织的平均值,否则averageSalary(...)方法会返回过时的缓存结果。这个可以通过将@CacheEvict注解加到一个方法上来更新雇员的工资:

  1. private JdbcTemplate jdbc;
  2. @CacheEvict(value = "averageSalary", key = "#e.organizationId")
  3. public void updateSalary(Employee e) {
  4. String sql =
  5. "UPDATE Employee " +
  6. "SET salary = ? " +
  7. "WHERE id = ?";
  8. jdbc.update(sql, e.getSalary(), e.getId());
  9. }

在这个方法被调用之后,这个雇员所属组织的平均值就会被从averageSalary缓存中踢出,这会强迫averageSalary(...)方法在下一次调用时重新计算。

Spring表达式语言(SpEL)
注意这个方法是以雇员为参数的,而平均值是通过组织的Id将平均值存储在缓存中的。为了明确地指定什么作为缓存键,可以使用注解的key参数和Spring表达式语言
#e.organizationId表达式的意思是从e变量中获取organizationId属性的值。本质上会在提供的雇员对象上调用getOrganizationId()方法,以及将返回的值作为缓存键。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注