springboot集成redis

    spring全家桶之springboot集成redis。

 

一:首先引入jar包,maven项目添加依赖:
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-data-redis </artifactId>
</dependency>
二:配置数据源

  properties形式: 

#============== redis ===================
spring.redis.host = localhost
spring.redis.port = 6379
#spring.redis.password=
spring.redis.database = 1
spring.redis.pool.max-active = 8
spring.redis.pool.max-wait = -1
spring.redis.pool.max-idle = 500
spring.redis.pool.min-idle = 0
spring.redis.timeout = 0
 或者yml形式配置:
 
spring:
  redis:
     host:  localhost
     port: 6379
     timeout: 6000
     pool:
        max-total: 10000
 
 
三 缓存服务,用redisTemplate来进行访问
 
/**
* @Author:  jeyson
* @Time: 2018/6/6 10:45
* @Description:这里只提供了string类型,可以将其他类型加入
*/
@Service
public class RedisService {
 
@Autowired
 RedisTemplate redisTemplate ;

 

 
public void setCache (String key , String value ,int exp){
   ValueOperations<String , String> ops= redisTemplate .opsForValue() ;
   ops.set(key , value , exp , TimeUnit. SECONDS ) ;
}
 
public String getCache (String key){
    ValueOperations<String , String> ops= redisTemplate .opsForValue() ;
    return ops.get(key) ;
  }
 
}
 这里只写了string类型的操作。

 

四:测试一下

 

@Test
public void testRedis (){
    String key= "jeyson:code: 1 245" ;
    redisService .setCache(key , "12341" , 60 ) ;
    System. out .println( "验证码:" + redisService .getCache(key)) ;
}

 源码地址:

   https://github.com/LiuJishuai/springboot-study

转载于:https://www.cnblogs.com/jeyson/p/9147083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值