@liyuj
2017-04-12T15:03:29.000000Z
字数 5016
阅读 3541
Apache-Ignite-1.9.0-中文开发手册
Ignite提供了一个SpringCacheManager-一个Spring缓存抽象的实现。他提供了基于注解的方式来启用Java方法的缓存,这样方法的执行结果就会存储在Ignite缓存中。如果之后同一个方法通过同样的参数集被调用,结果会直接从缓存中获得而不是实际执行这个方法。
Spring缓存抽象文档
关于如何使用Spring缓存抽象的更多信息,包括可用的注解,可以参照这个文档页面:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html.
只需要两个简单的步骤就可以将Ignite缓存嵌入基于Spring的应用:
SpringCacheManager作为缓存管理器。嵌入式节点可以通过SpringCacheManager自己启动,这种情况下需要分别通过configurationPath或者configuration属性提供一个Ignite配置文件的路径或者IgniteConfigurationBean(看下面的例子)。注意同时设置两个属性是非法的以及抛出IllegalArgumentException。
配置路径:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><!-- Provide configuration file path. --><bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager"><property name="configurationPath" value="examples/config/spring-cache.xml"/></bean><!-- Enable annotation-driven caching. --><cache:annotation-driven/></beans>
配置Bean:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><-- Provide configuration bean. --><bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager"><property name="configuration"><bean class="org.apache.ignite.configuration.IgniteConfiguration">...</bean></property></bean><-- Enable annotation-driven caching. --><cache:annotation-driven/></beans>
当缓存管理器初始化时也可能已经有一个Ignite节点正在运行(比如已经通过ServletContextListenerStartup启动了)。这时只需要简单地通过gridName属性提供网格名字就可以了。注意如果不设置网格名字,缓存管理器会试图使用默认的Ignite实例(名字为空的),下面是一个示例:
使用已启动的Ignite实例:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><!-- Provide grid name. --><bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager"><property name="gridName" value="myGrid"/></bean><!-- Enable annotation-driven caching. --><cache:annotation-driven/></beans>
远程节点
注意应用内部启动的节点只是希望连接的网络的一个入口,可以根据需要通过Ignite发行版提供的bin/ignite.{sh|bat}脚本启动尽可能多的远程独立节点,所有这些节点都会参与缓存数据。
虽然通过Ignite配置文件可以获得所有必要的缓存,但是这不是必要的。如果Spring要使用一个不存在的缓存时,SpringCacheManager会自动创建它。
如果不指定,会使用默认值创建一个新的缓存。要定制的话,可以通过dynamicCacheConfiguration属性提供一个配置模板,比如,如果希望使用复制缓存而不是分区缓存,可以像下面这样配置SpringCacheManager:
<bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">...<property name="dynamicCacheConfiguration"><bean class="org.apache.ignite.configuration.CacheConfiguration"><property name="cacheMode" value="REPLICATED"/></bean></property></bean>
也可以在客户端侧使用近缓存,要做到这一点只需要简单地通过dynamicNearCacheConfiguration属性提供一个近缓存配置即可。默认的话近缓存是不启用的,下面是一个例子:
<bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">...<property name="dynamicNearCacheConfiguration"><bean class="org.apache.ignite.configuration.NearCacheConfiguration"><property name="nearStartSize" value="1000"/></bean></property></bean>
一旦在Spring应用上下文中加入了SpringCacheManager,就可以通过简单地加上注解为任意的java方法启用缓存。
通常为很重的操作使用缓存,比如数据库访问。比如,假设有个Dao类有一个averageSalary(...)方法,他计算一个组织内的所有雇员的平均工资,那么可以通过@Cacheable注解来开启这个方法的缓存。
private JdbcTemplate jdbc;@Cacheable("averageSalary")public long averageSalary(int organizationId) {String sql ="SELECT AVG(e.salary) " +"FROM Employee e " +"WHERE e.organizationId = ?";return jdbc.queryForObject(sql, Long.class, organizationId);}
当这个方法第一次被调用时,SpringCacheManager会自动创建一个averageSalary缓存,他也会在缓存中查找事先计算好的平均值然后如果存在的话就会直接返回,如果这个组织的平均值还没有被计算过,那么这个方法就会被调用然后将结果保存在缓存中,因此下一次请求这个组织的平均值,就不需要访问数据库了。
缓存键
因为organizationId是唯一的方法参数,所以他会自动作为缓存键。
如果一个雇员的工资发生变化,可能希望从缓存中删除这个雇员所属组织的平均值,否则averageSalary(...)方法会返回过时的缓存结果。这个可以通过将@CacheEvict注解加到一个方法上来更新雇员的工资:
private JdbcTemplate jdbc;@CacheEvict(value = "averageSalary", key = "#e.organizationId")public void updateSalary(Employee e) {String sql ="UPDATE Employee " +"SET salary = ? " +"WHERE id = ?";jdbc.update(sql, e.getSalary(), e.getId());}
在这个方法被调用之后,这个雇员所属组织的平均值就会被从averageSalary缓存中踢出,这会强迫averageSalary(...)方法在下一次调用时重新计算。
Spring表达式语言(SpEL)
注意这个方法是以雇员为参数的,而平均值是通过组织的Id将平均值存储在缓存中的。为了明确地指定什么作为缓存键,可以使用注解的key参数和Spring表达式语言。
#e.organizationId表达式的意思是从e变量中获取organizationId属性的值。本质上会在提供的雇员对象上调用getOrganizationId()方法,以及将返回的值作为缓存键。