org.springframework.data.redis.cache.RedisCacheManager Java Examples

The following examples show how to use org.springframework.data.redis.cache.RedisCacheManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisCacheConfig.java    From ywh-frame with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 解决注解方式存放到redis中的值是乱码的情况
 * @param factory 连接工厂
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

    // 配置注解方式的序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration =
            config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
                    //配置注解默认的过期时间
                    .entryTtl(Duration.ofDays(1));
    // 加入白名单   https://github.com/alibaba/fastjson/wiki/enable_autotype
    ParserConfig.getGlobalInstance().addAccept("com.ywh");
    ParserConfig.getGlobalInstance().addAccept("com.baomidou");
    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example #2
Source File: RedisCacheConfig.java    From syhthems-platform with MIT License 6 votes vote down vote up
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Bean
public CacheManager JDKCacheManager() {
    final RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    final RedisCacheConfiguration defaultCacheConfig =
            RedisCacheConfiguration.defaultCacheConfig()

                    // 不缓存 null 值
                    .disableCachingNullValues()
                    // 使用注解时的序列化、反序列化对
                    .serializeKeysWith(stringPair)
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.java()))
                    .prefixKeysWith("syhthems:cache:");
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example #3
Source File: RedisCacheConfig.java    From syhthems-platform with MIT License 6 votes vote down vote up
@Override
@Bean
@Primary
public CacheManager cacheManager() {
    final RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    final RedisCacheConfiguration defaultCacheConfig =
            RedisCacheConfiguration.defaultCacheConfig()

                    // 不缓存 null 值
                    .disableCachingNullValues()
                    // 使用注解时的序列化、反序列化对
                    .serializeKeysWith(stringPair)
                    .serializeValuesWith(jacksonPair)

                    .prefixKeysWith("syhthems:cache:");
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example #4
Source File: CacheConfiguration.java    From push with Apache License 2.0 6 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {

    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(30))
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
            .disableCachingNullValues();

    RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(config)
            .transactionAware()
            .build();

    return redisCacheManager;
}
 
Example #5
Source File: RedisConfigure.java    From cms with Apache License 2.0 6 votes vote down vote up
/**
 * 选择redis作为默认缓存工具
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
            // 设置缓存的默认过期时间,也是使用Duration设置
            .entryTtl(Duration.ofDays(7))
            // 设置 key为string序列化
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer()))
            // 设置value为json序列化
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer()))
            // 不缓存空值
            .disableCachingNullValues();
    return RedisCacheManager
            .builder(redisConnectionFactory)
            .transactionAware()
            .cacheDefaults(configuration)
            .build();
}
 
Example #6
Source File: MicaRedisCacheAutoConfiguration.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Primary
@Bean("redisCacheManager")
public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
	RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
	RedisCacheConfiguration cacheConfiguration = this.determineConfiguration();
	List<String> cacheNames = this.cacheProperties.getCacheNames();
	Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();
	if (!cacheNames.isEmpty()) {
		Map<String, RedisCacheConfiguration> cacheConfigMap = new LinkedHashMap<>(cacheNames.size());
		cacheNames.forEach(it -> cacheConfigMap.put(it, cacheConfiguration));
		initialCaches.putAll(cacheConfigMap);
	}
	boolean allowInFlightCacheCreation = true;
	boolean enableTransactions = false;
	RedisAutoCacheManager cacheManager = new RedisAutoCacheManager(redisCacheWriter, cacheConfiguration, initialCaches, allowInFlightCacheCreation);
	cacheManager.setTransactionAware(enableTransactions);
	return this.customizerInvoker.customize(cacheManager);
}
 
Example #7
Source File: RedisAutoConfigure.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Bean(name = "cacheManager")
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory
        , RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
    RedisCacheConfiguration difConf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofHours(1));

    //自定义的缓存过期时间配置
    int configSize = cacheManagerProperties.getConfigs() == null ? 0 : cacheManagerProperties.getConfigs().size();
    Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(configSize);
    if (configSize > 0) {
        cacheManagerProperties.getConfigs().forEach(e -> {
            RedisCacheConfiguration conf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofSeconds(e.getSecond()));
            redisCacheConfigurationMap.put(e.getKey(), conf);
        });
    }

    return RedisCacheManager.builder(redisConnectionFactory)
            .cacheDefaults(difConf)
            .withInitialCacheConfigurations(redisCacheConfigurationMap)
            .build();
}
 
Example #8
Source File: RedisConfig.java    From biliob_backend with MIT License 5 votes vote down vote up
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    // 初始化一个RedisCacheWriter
    RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    // 初始化RedisCacheManager
    return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration());
}
 
Example #9
Source File: RedisConfig.java    From MI-S with MIT License 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}
 
Example #10
Source File: CacheService.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 管理缓存
 *
 * @param redisTemplate
 * @return
 */

@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    // 设置cache过期时间,时间单位是秒
    rcm.setDefaultExpiration(60);
    Map<String, Long> map = new HashMap<String, Long>();
    map.put("test", 60L);
    rcm.setExpires(map);
    return rcm;
}
 
Example #11
Source File: CacheConfig.java    From vics with MIT License 5 votes vote down vote up
@Primary
@Bean(name = "stats")
public CacheManager stats(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    cacheManager.setUsePrefix(true);
    cacheManager.setDefaultExpiration(TWENTY_MINS_SECS);
    return cacheManager;
}
 
Example #12
Source File: RedisCacheConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisCacheManager cacheManager(RedisTemplate templateRedis) {
    RedisCacheManager redisCacheManager = new RedisCacheManager(templateRedis);
    redisCacheManager.setTransactionAware(true);
    redisCacheManager.setLoadRemoteCachesOnStartup(true);
    redisCacheManager.setUsePrefix(true);

    return redisCacheManager;
}
 
Example #13
Source File: RedisCacheConfiguration.java    From spring-boot-with-multi-redis with MIT License 5 votes vote down vote up
@Primary
@Bean(name = "userCacheManager")
public CacheManager userCacheManager() {
    RedisCacheManager redisCacheManager = new RedisCacheManager(userRedisTemplate);
    redisCacheManager.setDefaultExpiration(3600);
    return redisCacheManager;
}
 
Example #14
Source File: RedisCacheConfig.java    From SuperBoot with MIT License 5 votes vote down vote up
/**
 * 定义缓存对象
 *
 * @return
 */
@Bean
@Override
public CacheManager cacheManager() {
    String[] cacheNames = {"cache", "users", "roles", "modules", "mails",
            "apis", "configs", "info", "groups", "auths",
            "items", "menus", "menupermissions", "resource", "permissions", "permissionsResource",
            "rolemenu", "customers", "duties", "employees", "orgs", "userDetails", "sensitive"};
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate(), Arrays.asList(cacheNames));
    //设置缓存过期时间 默认1天
    redisCacheManager.setDefaultExpiration(86400);
    return redisCacheManager;
}
 
Example #15
Source File: RedisConfig.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        //对象的序列化
        RedisSerializationContext.SerializationPair valueSerializationPair
                = RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer());
        //全局redis缓存过期时间
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(1))
//                .serializeKeysWith()
                .serializeValuesWith(valueSerializationPair);

        return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(factory), redisCacheConfiguration);
    }
 
Example #16
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    // 配置序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example #17
Source File: RedisCacheConfigurer.java    From bird-java with MIT License 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheWriter cacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);

    RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(10))
            .prefixKeysWith(cachePrefix + ":")
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

    return new RedisCacheManager(cacheWriter, cacheConfiguration);
}
 
Example #18
Source File: CacheConfig.java    From vics with MIT License 5 votes vote down vote up
@Bean(name = "longCache")
public CacheManager longCache(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    cacheManager.setUsePrefix(true);
    cacheManager.setDefaultExpiration(TEN_HOURS_SECS);
    return cacheManager;
}
 
Example #19
Source File: RedisConfig.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(factory))
            // 默认缓存过期时间:天
            .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(30)))
            .transactionAware()
            .build();
}
 
Example #20
Source File: RedisCacheConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    return new RedisCacheManager(
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory),
            this.getRedisCacheConfigurationWithTtl(entryTTL),  // 默认策略,未配置的 key 会使用这个
            this.getRedisCacheConfigurationMap()               // 指定 key 策略
    );
}
 
Example #21
Source File: RedisConfig.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * 设置缓存管理器,这里可以配置默认过期时间等
 *
 * @param connectionFactory 连接池
 * @return
 */
@Bean
public CacheManager cacheManager(JedisConnectionFactory jedisConnectionFactory) {
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
            .defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(60));
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(jedisConnectionFactory);
    RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    return manager;
}
 
Example #22
Source File: RedisConfiguration.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a configured {@link CacheManager}.
 * 
 * @return {@link CacheManager}
 */
@Bean
public CacheManager cacheManager() {
     RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplateObject());
     redisCacheManager.setUsePrefix(true);
     
     return redisCacheManager;
}
 
Example #23
Source File: RedisConfiguration.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Configures and returns a {@link CacheManager}.
 * 
 * @return {@link CacheManager}
 */
@Bean
public CacheManager cacheManager() {
     RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplateObject());
     redisCacheManager.setUsePrefix(true);
     
     return redisCacheManager;
}
 
Example #24
Source File: RedisTemplateConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
	RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
		.entryTtl(Duration.ofHours(1));
	return RedisCacheManager
		.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
		.cacheDefaults(redisCacheConfiguration).build();
}
 
Example #25
Source File: CacheConfig.java    From lion with Apache License 2.0 5 votes vote down vote up
/**
 * 配置 redis 缓存失效时间
 */
@Bean
public CacheManager cacheManager(@Autowired RedisConnectionFactory connectionFactory) {
    return RedisCacheManager
            .builder(connectionFactory)
            .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5)))
            .transactionAware()
            .build();
}
 
Example #26
Source File: RedisConfig.java    From DouBiNovel with Apache License 2.0 5 votes vote down vote up
@Override
    @Bean
    public CacheManager cacheManager() {

        // 设置序列化
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
                // value序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
//                .disableCachingNullValues()
                // 缓存过期时间
                .entryTtl(Duration.ofMinutes(5));

        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(lettuceConnectionFactory)
                .cacheDefaults(config)
                .transactionAware();
        @SuppressWarnings("serial")
        Set<String> cacheNames = new HashSet<String>() {
            {
                add("codeNameCache");
            }
        };
        builder.initialCacheNames(cacheNames);
        return builder.build();
    }
 
Example #27
Source File: RedisConfig.java    From FEBS-Security with Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
            .RedisCacheManagerBuilder
            .fromConnectionFactory(redisConnectionFactory);
    return builder.build();
}
 
Example #28
Source File: RedisCacheConfig.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 RedisCacheManager 类,主要是设置序列化,解决乱码问题
 *
 * @param factory RedisConnectionFactory
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    // 解决查询缓存转换异常的问题
    ObjectMapper om = new ObjectMapper();
    om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    // 配置序列化(解决乱码的问题)
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
            .disableCachingNullValues().entryTtl(timeToLive);
    return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}
 
Example #29
Source File: RedisConfig.java    From JetfireCloud with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: RedisConfig.java    From JavaQuarkBBS with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}