首页 / REDIS / spring+redis
spring+redis
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring+redis,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4039字,纯文字阅读大概需要6分钟。
内容图文
配置
1 <? xml version="1.0" encoding="UTF-8" ?> 2 < beans xmlns ="http://www.springframework.org/schema/beans" 3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p ="http://www.springframework.org/schema/p" 5 xmlns:c ="http://www.springframework.org/schema/c" 6 xmlns:cache ="http://www.springframework.org/schema/cache" 7 xsi:schemaLocation ="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/cache 10 http://www.springframework.org/schema/cache/spring-cache.xsd" > 11 12 <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 --> 13 < description >redis配置</description>1415<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">16<property name="maxIdle" value="${redis.pool.maxIdle}"/>17<property name="maxTotal" value="${redis.pool.maxActive}"/>18<property name="maxWaitMillis" value="${redis.pool.maxWait}"/>19<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>20<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>21</bean>2223<!-- JedisConnectionFactory -->24<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">25<property name="hostName" value="${redis.master.ip}"/>26<property name="port" value="${redis.master.port}"/>27<property name="poolConfig" ref="jedisPoolConfig"/>28</bean>2930<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"31 p:connectionFactory-ref="jedisConnectionFactory">32<property name="keySerializer">33<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>34</property>35<property name="valueSerializer">36<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>37</property>38<property name="hashKeySerializer">39<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>40</property>41<property name="hashValueSerializer">42<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>43</property>44</bean>4546<!--spring cache-->47<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"48 c:redisOperations-ref="redisTemplate">49<!-- 默认缓存10分钟 -->50<property name="defaultExpiration" value="600"/>51<property name="usePrefix" value="true"/>52<!-- cacheName 缓存超时配置,半小时,一小时,一天 -->53<property name="expires">54<map key-type="java.lang.String" value-type="java.lang.Long">55<entry key="halfHour" value="1800"/>56<entry key="hour" value="3600"/>57<entry key="oneDay" value="86400"/>58<!-- shiro cache keys -->59<entry key="authorizationCache" value="1800"/>60<entry key="authenticationCache" value="1800"/>61<entry key="activeSessionCache" value="1800"/>62</map>63</property>64</bean>6566<!-- cache注解,和spring-ehcache.xml中的只能使用一个 -->67<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>68</beans>
使用注解
1 package com.wangzhixuan.service.impl; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.cache.annotation.Cacheable; 8 import org.springframework.stereotype.Service; 9 10 import com.wangzhixuan.mapper.UserMapper; 11 import com.wangzhixuan.model.User; 12 13 @Service 14 public class TestService { 15 @Autowired 16 private UserMapper userMapper; 17 18 @Cacheable(value = "hour", key = "#id") 19public User selectById(Serializable id) { 20return userMapper.selectById(id); 21 } 22 @Cacheable(value = "hours") 23public String date() { 24returnnew Date().toString(); 25 } 26 }
原文:http://www.cnblogs.com/javaweb2/p/6255958.html
内容总结
以上是互联网集市为您收集整理的spring+redis全部内容,希望文章能够帮你解决spring+redis所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。