使用spring-data-redis操作redis
Posted 雨吃草
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用spring-data-redis操作redis相关的知识,希望对你有一定的参考价值。
redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:property-placeholder location="classpath*:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean> </beans>
redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
操作User对象的demo
@Resource RedisTemplate<String, User> redisTemplate; public void saveUser(User user){ redisTemplate.opsForValue().set(user.getUserId, user); } public User getUser(String userId){
return redisTemplate.opsForValue().get(userId); }
以上是关于使用spring-data-redis操作redis的主要内容,如果未能解决你的问题,请参考以下文章
Spring中使用RedisTemplate操作Redis(spring-data-redis)
Spring中使用RedisTemplate操作Redis(spring-data-redis)