快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

怎么在SpringBoot中添加Redis

这篇文章将为大家详细讲解有关怎么在SpringBoot中添加redis,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、静海网站维护、网站推广。

引入依赖

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


  org.springframework.boot
  spring-boot-starter-data-redis
  2.0.5.RELEASE

配置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 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是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

关于怎么在SpringBoot中添加Redis就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站题目:怎么在SpringBoot中添加Redis
标题路径:http://6mz.cn/article/jedsss.html

其他资讯