1.导入pom文件
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2.编写redis.properties文件
#redis配置
redis.ip=localhost
redis.port=6379
redis.password=123456
redis.maxTotal=1000
redis.maxactive=64
redis.maxIdle=20
redis.maxWaitMillis=3000
redis.timeout=3000
redis.testOnBorrow=true
redis.testOnReturn=true
redis.blockWhenExhausted=true
3.spring配置文件中添加容器
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="blockWhenExhausted"
value="${redis.blockWhenExhausted}" />
</bean>
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean >
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
4.基本使用
1.通用操作
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.delete(key);
Boolean redisTemplate.expire(key, timeout, unit);
Boolean redisTemplate.expireAt(key, date);
DataType redisTemplate.type(key);
Long redisTemplate.getExpire(key);
Long redisTemplate.getExpire(key, unit);
redisTemplate.hasKey(key);
Set<String> redisTemplate.keys(pattern);
void redisTemplate.rename(oldKey, newKey);
Boolean redisTemplate.renameIfAbsent(oldKey, newKey);
2.String类型操作
redisTemplate.opsForValue().set("key", "value");
String value = redisTemplate.opsForValue().get("key");
3.Hash类型操作
redisTemplate.opsForHash().put("hash-key", "hash-field", "hash-value");
Object value = redisTemplate.opsForHash().get("hash-key", "hash-field");
Map<Object, Object> hash = redisTemplate.opsForHash().entries("hash-key");
redisTemplate.opsForHash().putAll(key, maps);
Boolean redisTemplate.opsForHash().hasKey(key, field);
4.List类型操作
redisTemplate.opsForList().leftPush("list-key", "value1");
redisTemplate.opsForList().index(key, index);
List<Object> list = redisTemplate.opsForList().range("list-key", 0, -1);
redisTemplate.opsForList().leftPushAll(key, value);
redisTemplate.opsForList().set(key, index, value);
redisTemplate.opsForList().size(key);
5.Set类型操作
redisTemplate.opsForSet().add("set-key", "value1");
Set<Object> set = redisTemplate.opsForSet().members("set-key");
redisTemplate.opsForSet().remove(key, values);
redisTemplate.opsForSet().size(key);
redisTemplate.opsForSet().isMember(key, value);
redisTemplate.opsForSet().members(key);
6.ZSet类型操作
redisTemplate.opsForZSet().add("zset-key", "value1", 10);
Set<Object> set = redisTemplate.opsForZSet().range("zset-key", 0, -1);
redisTemplate.opsForZSet().remove(key, values);
redisTemplate.opsForZSet().score(key, value);
redisTemplate.opsForZSet().incrementScore(key, value, delta);
redisTemplate.opsForZSet().rank(key, value);
redisTemplate.opsForZSet().reverseRank(key, value);