Java Code Examples for org.springframework.data.redis.cache.RedisCache

The following examples show how to use org.springframework.data.redis.cache.RedisCache. These examples are extracted from open source projects. 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
@Override
protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfig) {
	if (StringUtil.isBlank(name) || !name.contains(StringPool.HASH)) {
		return super.createRedisCache(name, cacheConfig);
	}
	String[] cacheArray = name.split(StringPool.HASH);
	if (cacheArray.length < 2) {
		return super.createRedisCache(name, cacheConfig);
	}
	String cacheName = cacheArray[0];
	if (cacheConfig != null) {
		// 转换时间,支持时间单位例如:300ms,第二个参数是默认单位
		Duration duration = DurationStyle.detectAndParse(cacheArray[1], ChronoUnit.SECONDS);
		cacheConfig = cacheConfig.entryTtl(duration);
	}
	return super.createRedisCache(cacheName, cacheConfig);
}
 
Example 2
Source Project: spring-microservice-exam   Source File: MultitenantCacheManager.java    License: MIT License 6 votes vote down vote up
/**
 * 扩展@Cacheable,支持配置失效时间
 *
 * @param name        name
 * @param cacheConfig cacheConfig
 * @return RedisCache
 */
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
    if (StringUtils.isBlank(name) || !name.contains(SPLIT_FLAG)) {
        return super.createRedisCache(name, cacheConfig);
    }
    String[] cacheArray = name.split(SPLIT_FLAG);
    if (cacheArray.length < CACHE_LENGTH) {
        return super.createRedisCache(name, cacheConfig);
    }
    String cacheName = cacheArray[0];
    if (cacheConfig != null) {
        // 从系统属性里读取超时时间
        long cacheAge = Long.getLong(cacheArray[1], -1);
        cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheAge));
    }
    return super.createRedisCache(cacheName, cacheConfig);
}
 
Example 3
Source Project: black-shop   Source File: RedisAutoCacheManager.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected RedisCache createRedisCache(String name,@Nullable RedisCacheConfiguration cacheConfig) {
	if(StrUtil.isBlank(name) || !name.contains("#")){
		return super.createRedisCache(name, cacheConfig);
	}

	String[] cacheArray = name.split("#");
	if (cacheArray.length < 2) {
		return super.createRedisCache(name, cacheConfig);
	}
	if (cacheConfig != null) {
		long cacheTime = Long.parseLong(cacheArray[1]);
		cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheTime));
	}
	return super.createRedisCache(name, cacheConfig);
}
 
Example 4
Source Project: spring-boot-vue-admin   Source File: MyRedisCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
protected Collection<RedisCache> loadCaches() {
  final List<RedisCache> caches = new LinkedList<>();
  for (final Map.Entry<String, RedisCacheConfiguration> entry :
      this.initialCacheConfiguration.entrySet()) {
    caches.add(super.createRedisCache(entry.getKey(), entry.getValue()));
  }
  return caches;
}
 
Example 5
Source Project: gateway-helper   Source File: RedisL2CacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected RedisCache getMissingCache(String name) {
    Long expiresSeconds = expires.get(name);
    if (expiresSeconds != null) {
        return createRedisCache(name, RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues().entryTtl(Duration.ofSeconds(expiresSeconds)));
    } else {
        return createRedisCache(name, RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues());
    }
}
 
Example 6
protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfig) {
    //使用cacheName从genericCacheMap获取泛型类型,如果失败,使用默认方式创建cache。如果成功,创建泛型cache
    Type type = this.genericCacheMap.get(name);
    //重置超时时间和cacheName
    String cacheName = resetExpirationAndName(name);
    if (type == null) {
        return super.createRedisCache(cacheName, cacheConfig);
    }
    return new RedisGenericCache(cacheName, cacheWriter, determineConfiguration(type));
}
 
Example 7
Source Project: DimpleBlog   Source File: MyRedisCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<RedisCache> loadCaches() {
    List<RedisCache> caches = new LinkedList<>();
    for (Map.Entry<String, RedisCacheConfiguration> entry : initialCacheConfiguration.entrySet()) {
        caches.add(super.createRedisCache(entry.getKey(), entry.getValue()));
    }
    return caches;
}
 
Example 8
Source Project: api-gateway-old   Source File: RedisL2CacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected RedisCache getMissingCache(String name) {
    Long expiresSeconds = expires.get(name);
    if (expiresSeconds != null) {
        return createRedisCache(name, RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues().entryTtl(Duration.ofSeconds(expiresSeconds)));
    } else {
        return createRedisCache(name, RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues());
    }
}
 
Example 9
Source Project: todolist   Source File: AppCacheManager.java    License: MIT License 5 votes vote down vote up
@Override
	protected RedisCache createCache(String cacheName) {
//		if(cacheName.equals("token")) {
//			return new RedisCache(cacheName, "token:".getBytes(), this.ops, 24*60*60);
//		}

		return super.createCache(cacheName);
	}
 
Example 10
@Override
protected Cache decorateCache(Cache cache) {
	return new RedisAndLocalCache(this, (RedisCache) cache);
}
 
Example 11
public RedisAndLocalCache(TowLevelCacheManager cacheManager, RedisCache redisCache) {
	this.redisCache = redisCache;
	this.cacheManager = cacheManager;
}
 
Example 12
Source Project: perfect-ssm   Source File: RedisUtil.java    License: Apache License 2.0 4 votes vote down vote up
public void init() {
    template = SpringUtil.getBean("redisTemplate");//RedisCacheConfig中定义了
    cache = new RedisCache(CACHE_NAME, CACHE_NAME.getBytes(), template, EXPIRE_TIME);
}