当前位置: 移动技术网 > IT编程>开发语言>Java > 在SpringBoot中添加Redis及配置方法

在SpringBoot中添加Redis及配置方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

英克必成刷钻,刘汝斌,成都水疗论坛

在实际的开发中,会有这样的场景。有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长。 此时就可以考虑在项目中加入缓存。

引入依赖

在maven项目中引入如下依赖。并且需要在本地安装redis。

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-data-redis</artifactid>
  <version>2.0.5.release</version>
</dependency>

配置redis

在springboot的配置文件中添加如下代码。

redis:
  host: 127.0.0.1
  port: 6379
  timeout: 5000
  database: 0
  jedis:
   pool:
    max-idle: 8
    max-wait:
    min-idle: 0

添加redis配置文件

新建名为redisconfig的配置类。

import com.fasterxml.jackson.annotation.jsonautodetect;
import com.fasterxml.jackson.annotation.propertyaccessor;
import com.fasterxml.jackson.databind.objectmapper;
import org.springframework.cache.cachemanager;
import org.springframework.cache.annotation.cachingconfigurersupport;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.cache.interceptor.keygenerator;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.cache.rediscacheconfiguration;
import org.springframework.data.redis.cache.rediscachemanager;
import org.springframework.data.redis.cache.rediscachewriter;
import org.springframework.data.redis.connection.redisconnectionfactory;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.data.redis.serializer.jackson2jsonredisserializer;

import java.time.duration;

/**
 * redisconfig
 *
 * @author detectivehlh
 * @date 2018-10-11 14:39
 **/
@configuration
@enablecaching
public class redisconfig extends cachingconfigurersupport {
  @bean
  @override
  public keygenerator keygenerator() {
    return (target, method, params) -> {
      stringbuilder sb = new stringbuilder();
      sb.append(target.getclass().getname());
      sb.append(method.getname());
      for (object obj : params) {
        sb.append(obj.tostring());
      }
      return sb.tostring();
    };
  }

  @bean
  public redistemplate<string, string> redistemplate(redisconnectionfactory factory) {
    objectmapper om = new objectmapper();
    om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any);
    om.enabledefaulttyping(objectmapper.defaulttyping.non_final);
    //redis序列化
    jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class);
    jackson2jsonredisserializer.setobjectmapper(om);

    stringredistemplate template = new stringredistemplate(factory);
    template.setvalueserializer(jackson2jsonredisserializer);
    template.afterpropertiesset();
    return template;
  }

  /**
   * 自定义cachemanager
   */
  @bean
  public cachemanager cachemanager(redistemplate redistemplate) {
    //全局redis缓存过期时间
    rediscacheconfiguration rediscacheconfiguration = rediscacheconfiguration.defaultcacheconfig().entryttl(duration.ofdays(1));
    rediscachewriter rediscachewriter = rediscachewriter.nonlockingrediscachewriter(redistemplate.getconnectionfactory());
    return new rediscachemanager(rediscachewriter, rediscacheconfiguration);
  }
}

添加缓存配置

在项目的service层中的实现类中,添加@cacheable注解。

import java.util.hashmap;

/**
 * userloginserviceimpl
 *
 * @author detectivehlh
 * @date 2018-10-10 17:20
 **/
@service
public class userloginserviceimpl implements userloginservice {
  @autowired
  private userloginmapper userloginmapper;

  @override
  @cacheable(value = "usercache")
  public hashmap getbyusername(string username) {
    system.out.println("此时没有走缓存");
    return userloginmapper.getbyusername(username);
  }
}

然后调用一次该接口。就可以在redis中看到如下的key。

"usercache::com.detectivehlh.api.service.impl.userloginserviceimplgetbyusernamesolarfarm"

同时,可以在控制台中看到有"此时没有走缓存"的输出。然后再次调用该接口,就可以看到返回的速度明显变快,并且没有"此时没有走缓存"输出。说明 此时的接口走的是缓存。

总结

以上所述是小编给大家介绍的在springboot中添加redis及配置方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网