spring集成redis
来源:互联网 发布:监理行业发展前景知乎 编辑:程序博客网 时间:2024/06/03 23:01
在这里会以注解的方式在springmvc中使用redis。
主要记录redis与spring的集成部分。
准备jar包
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring.data.redis.version}</version></dependency><dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version></dependency>
准备资源文件
首先你需要一个资源文件用来配置redis的连接信息
redis.hostname=localhostredis.port=6379 redis.timeout=2000 redis.usePool=true redis.default.db=0 redis.maxTotal=600 redis.maxIdle=300 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=30000 redis.testOnBorrow=true redis.encode=utf-8 redis.expire=604800000 redis.unlock=false
spring配置文件中配置redis连接池
<!-- jedis 配置 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <!--<property name="maxWaitMillis" value="${redis.maxWait}" /> --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /></bean><!-- redis服务器中心 --><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig" /> <property name="port" value="${redis.port}" /> <property name="hostName" value="${redis.hostname}" /> <!-- <property name="password" value="${redis.password}" /> --> <property name="timeout" value="${redis.timeout}"></property></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property></bean>
配置spring 缓存并且开启注解缓存
<!-- 开启注解缓存 --><cache:annotation-driven cache-manager="cacheManager" /><!-- 配置spring缓存管理为redis --><bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg ref="redisTemplate" /></bean>
如果在配置文件中没有开启注解缓存功能,那么使用注解来进行缓存将会失败
使用redis
用注解@Cacheable、@CachePut、@CacheEvict 将查询结果保存在redis服务器上。
redis的集群部署
<bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="3"></property> <property name="clusterNodes"> <set> <!-- 放主服务器的配置 --> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean> </set> </property></bean>
配置了这个bean之后就ok了
0 0
- redis与Spring集成
- Spring与redis集成
- redis与spring集成
- Spring Cache集成redis
- Spring 集成 Redis
- spring-data集成redis
- redis spring 集成
- spring集成redis详解
- Spring Cache集成redis
- spring redis集成
- spring redis集成
- Spring集成Redis示例
- Spring session redis集成
- Spring 集成 redis
- Spring Jedis集成Redis
- spring和redis集成
- Spring Boot Redis 集成
- Spring + Jedis集成Redis
- JSP标签
- BASS音频库使用的入门教程
- linux配置tomcat+mysql+jdk
- 自己动手用C扩展PHP(二)
- LightOJ 1234(调和级数、有通项公式的发散数列求和)
- spring集成redis
- UEditor之实现配置简单的图片上传示例
- 网页布局,右边距margin为0
- Django官方手册阅读(投票应用)
- ASmack连接Openfire服务器异常
- HDU1142 A Walk Through the Forest(SPFA+记忆化搜索)
- mapreduce编程---寻找图中的三角形
- mybatis高级输出映射(多对多查询)
- 自己动手用C扩展PHP(三)