当前位置: 首页 > 面试题库 >

如何在spring-data 2.0.x中创建RedisCacheManager

宋嘉懿
2023-03-14
问题内容

我正在将应用程序从Spring Boot 1.5.x迁移到2.0.x。我想保留jedis,但我对的实例化有疑问RedisCacheManager

现在构造函数签名是

RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration)

但在此之前:

RedisCacheManager(RedisOperations redisOperations)

我定义此bean仅RedisTemplate在范围内:

@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
    HandleRedisCacheManager redisCacheManager = new HandleRedisCacheManager(redisTemplate);
    redisCacheManager.setUsePrefix(true);
    return redisCacheManager;
}

现在应该如何创建它?


问题答案:

它不再接受RedisTemplate。所以试试这个:

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,
                                      ResourceLoader resourceLoader) {
    RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
            .builder(redisConnectionFactory)
            .cacheDefaults(determineConfiguration(resourceLoader.getClassLoader()));
    List<String> cacheNames = this.cacheProperties.getCacheNames();
    if (!cacheNames.isEmpty()) {
        builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
    }
    return builder.build();
}

private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
        ClassLoader classLoader) {
    if (this.redisCacheConfiguration != null) {
        return this.redisCacheConfiguration;
    }
    CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

    ObjectMapper mapper = new Jackson2ObjectMapperBuilder()
            .modulesToInstall( new SimpleModule().addSerializer( new NullValueSerializer(null)) )
            .failOnEmptyBeans( false )
            .build();
    mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer( mapper );

    //get the mapper b/c they registered some internal modules
    config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));;

    if (redisProperties.getTimeToLive() != null) {
        config = config.entryTtl(redisProperties.getTimeToLive());
    }
    if (redisProperties.getKeyPrefix() != null) {
        config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        config = config.computePrefixWith(cacheName -> redisProperties.getKeyPrefix() + cacheName + "::");
    }
    if (!redisProperties.isCacheNullValues()) {
        config = config.disableCachingNullValues();
    }
    if (!redisProperties.isUseKeyPrefix()) {
        config = config.disableKeyPrefix();
    }
    return config;
}


 类似资料:
  • 我正在创建JavaMailSender的bean类,并自动拥有javamailsender,但我得到错误 我不能为bean id="mail Sender"创建bean类。我在过去的两天里一直在努力,请让我离开它。 和错误页 组织。springframework。豆。工厂BeanCreationException:创建名为“employeeController”的bean时出错:自动连线依赖项的注

  • 我是Spring Boot新手,尝试创建DefaultMessageListenerContainer,这样我就可以使用weblogic workmanager并以多线程方式运行多个消息侦听器。 有人能提供一些例子吗?到目前为止,我找到了下面的解决方案,但是我如何在Spring-Boot中实现它呢?

  • 我试图按照这个答案将JDBC TokenStore添加到我的应用程序中。 https://stackoverflow.com/a/37595818/148844 它正在使用MemoryTokenStore中的。我需要知道把代码放在哪里 我试着把它放进去 但是我弄错了 引起原因:org.springframework.beans.factory.BeanCreation异常:创建名为'OAuth2C

  • 如何在SpringBoot2.1.4.RELEASE中创建度量?

  • 我想写一个库,以某种方式完成我总是需要的工作。 这个库应该基于spring-boot并在Spring环境中工作。因此,应该可以在您的pom文件中添加依赖项,然后访问服务。 愚蠢的例子:“库”中的功能 现在我可以在我的另一个项目中做以下事情。

  • 我正在Springboot应用程序中的Mongo Db中创建一个视图。下面是相同的代码 我想添加allowDiskUse:true条件,因为我得到以下错误 堆栈跟踪:|/java.lang.Exception:[profile_event_view@stage[副本集:]]数据库错误!|___/Mongo服务器错误(MongoQueryException):查询失败,错误代码为292,错误消息为“

  • 我正在制作一个目录来存储所有上传的文件在我的spring boot应用程序启动时。 此目录的路径存储在application.properties文件中。我正在尝试阅读这个路径,并在项目启动时创建一个目录。在启动时创建目录时,我无法获取路径。

  • 问题内容: 我一直在四处搜寻有关如何使用Swift 2.0为MapView制作MKCircle注释的良好解释,但我似乎找不到足够的解释。有人可以张贴一些示例代码来显示如何创建MKCircle批注吗?这是我用来制作地图并获取坐标的代码。 问题答案: 将展示有关如何使用xcode 8.3.3的swift 3在地图视图上创建圆形叠加层的分步方法 在您的主故事板文件中,将地图工具包视图拖到故事板的场景(视