Spring5中Redis配置

编程

技术更新太快了要跟上。。。

Redis的Maven引用

        <dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>2.2.4.RELEASE</version>

</dependency>

Redis的配置

    <!-- redis配置 -->

<!--初始化连接池配置-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<property name="maxIdle" value="${redis.maxIdle}"/>

<property name="maxTotal" value="${redis.maxTotal}"/>

<property name="maxWaitMillis" value="10000"/>

<property name="testOnBorrow" value="true"/>

<property name="testOnReturn" value="true"/>

</bean>

<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">

<constructor-arg name="thePassword" value="${redis.password}"></constructor-arg>

</bean>

<!-- RedisSentinelConfiguration(主从复制)-->

<!-- RedisClusterConfiguration(集群) -->

<!-- 单机模式 -->

<bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">

<property name="hostName" value="${redis.host}"/>

<property name="port" value="${redis.port}"/>

<property name="password" ref="redisPassword"/>

</bean>

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"/>

</bean>

<!--手动设置 key 与 value 的序列化方式-->

<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

<!--配置jedis模板 -->

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory"/>

<property name="keySerializer" ref="keySerializer"/>

<property name="valueSerializer" ref="valueSerializer"/>

<property name="hashKeySerializer" ref="keySerializer"/>

<property name="hashValueSerializer" ref="valueSerializer"/>

</bean>

<!--也可以StringRedisTemplate 专注于String的操作 -->

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

<!--<property name="connectionFactory" ref="jedisConnectionFactory"></property>-->

<!--在StringRedisTemplate与redisTemplate不同,可以直接造构造器中传入ConnectionFactory-->

<constructor-arg name="connectionFactory" ref="jedisConnectionFactory"></constructor-arg>

<property name="keySerializer" ref="keySerializer"/>

<property name="valueSerializer" ref="valueSerializer"/>

<property name="hashKeySerializer" ref="keySerializer"/>

<property name="hashValueSerializer" ref="valueSerializer"/>

</bean>

 

Redis简单工具

package com.fourlambs.proxyip.base;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import java.util.Iterator;

import java.util.concurrent.TimeUnit;

/**

* @author yuwan [2018/11/24 16:57]

* @comment Redis util.

*/

@Component

public class RedisUtil {

@Autowired

private RedisTemplate redisTemplate;

public RedisTemplate getRedisTemplate() {

return redisTemplate;

}

/**

* 存字符串缓存,并设置过期时间

*

* @param key

* @param value

* @param timeOut 单位s

*/

public Boolean set(String key, String value, Long timeOut) {

redisTemplate.opsForValue().set(key, value);

//给缓存设置清除时间

return redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);

}

public Boolean setObject(String key, Object object, Long timeOut) {

redisTemplate.opsForValue().set(key, object);

//给缓存设置清除时间

return redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);

}

//计数

public long increment(String key, long lon, long timeout) {

long l = redisTemplate.opsForValue().increment(key, lon);

//给缓存设置清除时间

boolean b = redisTemplate.expire(key, timeout, TimeUnit.SECONDS);

return b ? l : 0;

}

/**

* 获取缓存

*/

public Object get(Object key) {

return redisTemplate.opsForValue().get(key);

}

/**

* 有没有缓存

*/

public Boolean hasKey(Object key) {

return redisTemplate.hasKey(key);

}

/**

* 删除

*/

public void delete(Object key) {

redisTemplate.delete(key);

}

/**

* 获取所有的key

* @param keyHeader 如:session*

*/

public Iterator getKeys(Object keyHeader) {

return redisTemplate.keys(keyHeader).iterator();

}

/**

* 获取剩余过期时间 秒

* @param key

* @return -1 则无限时间

*/

public Long getExpire(String key) {

return redisTemplate.getExpire(key, TimeUnit.SECONDS);

}

/**

* 获取剩余过期时间

* @param key

* @return -1 则无限时间

*/

public Long getExpire(String key, TimeUnit timeUnit) {

return redisTemplate.getExpire(key, timeUnit);

}

/**

* 设置过期时间 秒

* @param key

* @param timeout

* @return 是否设置成功

*/

public Boolean expire(String key, Long timeout, TimeUnit timeUnit) {

return redisTemplate.expire(key, timeout, timeUnit);

}

// 追加时间

public Boolean expire(String key, long timeout) {

return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);

}

}

 

 

 

以上是 Spring5中Redis配置 的全部内容, 来源链接: utcz.com/z/514315.html

回到顶部