【Java】緩存配置

引入jar包

<!—引入tool-->
<dependency>
<groupId>com.dmsdbj.itoo</groupId>
<artifactId>itoo-tool</artifactId>
</dependency>
<!--redis#-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!—spring-data-redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>

創建spring-redis.xml配置文件

<!----------------------------------redis配置單機版--------------------------->
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="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">
<!--啓用緩存註解功能-->
<cache:annotation-drivencache-manager="redisCacheManager"/>
<!---redis單機版配置-->
<beanid="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionF
actory">
<constructor-arg>
<bean
class="org.springframework.data.redis.connection.RedisStandaloneConfigu
ration"
c:host-name="192.168.22.64"c:port="6379"/>
</constructor-arg>
</bean>
<beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<!--最小空閒數-->
<propertyname="minIdle"value="5"/><!--最大空閒數-->
<propertyname="maxIdle"value="100"/><!--最大連接數
-->
<propertyname="maxTotal"value="300"/><!--最大等待時間
單位毫秒(ms)-->
<propertyname="maxWaitMillis"value="3000"/><!--使用連
接時測試連接是否可用-->
<propertyname="testOnBorrow"value="true"/>
</bean>
<!--緩存key和value序列化配置-->
<beanid="redisCacheManager"
class="com.dmsdbj.itoo.tool.redis.TedisCacheManager"
factory-method="create"
c:connection-factory-ref="jedisConnectionFactory">
<propertyname="keySerializer"ref="keySerializer"/>
<propertyname="valueSerializer"ref="valueSerializer"/>
</bean>
<beanid="keySerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"
/>
<beanid="valueSerializer"
class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializ
er"/>
<!--redis模板-->
<beanid="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<propertyname="connectionFactory"
ref="jedisConnectionFactory"/>
<propertyname="keySerializer"ref="keySerializer"/>
<propertyname="valueSerializer"ref="valueSerializer"/>
</bean>
<!--Session共享-->
<beanid="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.htt
p.RedisHttpSessionConfiguration">
<propertyname="maxInactiveIntervalInSeconds"value="7200"/>
</bean>
</beans>

使用方式
1.註解方式使用
查詢時添加註解進行緩存
增刪改時清除緩存
2.模板方式使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章