springboot 2.0中redis的使用

@auther:Lianggs

由于springboot 2.0升级后,很多的以前的方法都已被替换或者删除,导致原来可用的方法都不能用了,折腾了一天半的时间,终于搞定了。分享出来供有需求的同仁参考
配置什么的就不介绍了,和以前一样,
实现代码,config:
import java.util.concurrent.CountDownLatch ;
import org.springframework.beans.factory.annotation. Autowired ;
import org.springframework.cache.annotation.CachingConfigurerSupport ;
import org.springframework.cache.annotation. EnableCaching ;
import org.springframework.context.annotation. Bean ;
import org.springframework.context.annotation. Configuration ;
import org.springframework.data.redis.connection.RedisConnectionFactory ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.data.redis.listener.PatternTopic ;
import org.springframework.data.redis.listener.RedisMessageListenerContainer ;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter ;


@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory ,
MessageListenerAdapter listenerAdapter) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer() ;
container.setConnectionFactory(connectionFactory) ;
container.addMessageListener(listenerAdapter , new PatternTopic( "chat" )) ;

return container ;
}

@Bean
MessageListenerAdapter listenerAdapter (Receiver receiver) {
return new MessageListenerAdapter(receiver , "receiveMessage" ) ;
}

@Bean
Receiver receiver (CountDownLatch latch) {
return new Receiver(latch) ;
}

@Bean
CountDownLatch latch () {
return new CountDownLatch( 1 ) ;
}

@Bean
StringRedisTemplate template (RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory) ;
}
public class Receiver {

private CountDownLatch latch ;
@Autowired
public Receiver (CountDownLatch latch) {
this . latch = latch ;
}
public void receiveMessage (String message) {
latch .countDown() ;
}
}
}

工具类:
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.stereotype. Component ;

import javax.annotation. Resource ;
import java.io.Serializable ;
import java.util.concurrent.TimeUnit ;

@Component
public class RedisService {

private static final Integer time = 300 ;

private static RedisTemplate redisTemplate ;

@Resource
public void setRedisTemplate (RedisTemplate redisTemplate){
this . redisTemplate =redisTemplate ;
}

/**
* 设置过期时间,秒
* @param key
* @param expireTime
* @return
*/
public static Boolean expire (String key , Integer expireTime){
return redisTemplate .expire(key , expireTime , TimeUnit. SECONDS ) ;
}

public static void set (String key , Serializable value , Integer time){
redisTemplate .opsForValue().set(key , value) ;
expire (key , time) ;
}

public static void set (String key , Serializable value){
redisTemplate .opsForValue().set(key , value) ;
expire (key , time ) ;
}

public static Object get (String key){
return redisTemplate .opsForValue().get(key) ;
}

public static Boolean delete (String key){
return redisTemplate .delete(key) ;
}

public static Boolean hasKey (String key) {
return redisTemplate .hasKey(key) ;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值