记录使用springboot整合redis

pom文件中添加如下两个包

                <!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

ApplicationStarter开始工程文件上添加注解:@EnableCaching,好像这个注解也可以加在配置redis的配置类上.

然后创建redis配置文件类:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 和redis有关的配置在这里
 * @author yhy
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

  @Bean(name = "cacheManager")
  public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    // 设置缓存过期时间(单位秒)
    rcm.setDefaultExpiration(30);
    return rcm;
  }
//下面这个配置可以不用配置,当你使用的springboot配置文件(.yml文件)配置有redis的配置信息,springboot会自动读取,然后创建redis的连接对象.
//不过需要注意的是,redis的配置信息需要配置在spring:这个属性下面,不然读取不到,如:
spring:
  profiles: dev
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: 
    username:  
    password:  
    driverClassName: com.mysql.jdbc.Driver
#redis
  redis:
    host: redis地址
    port: 6379
    password: redis密码
    pool:
      max-active: 20
      max-wait: 1
      max-idle: 10
      min-idle: 1
    database: redis仓库(这是我们公司的叫法,应该就是redis的库)
//层级关系是redis属于spring这个属性.这样就可以使用redis了.当然,如果你还需要操作redis,就需要配置redisTemplate.这个就网上搜一下,很多的.
//  @Bean
//  public RedisConnectionFactory redisCF() {
//    // 如果什么参数都不设置,默认连接本地6379端口
//    JedisConnectionFactory factory = new JedisConnectionFactory();
//    factory.setPort(6379);
//    factory.setHostName("192.168.0.24");
//    factory.setPassword("19181716");
//    factory.setDatabase(14);
//    return factory;
//  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值