org.springframework.boot.convert.DurationStyle Java Examples

The following examples show how to use org.springframework.boot.convert.DurationStyle. 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: RedisAutoCacheManager.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 File: CaptchaUtil.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 从 cache name 中解析 ttl,例如: user:test#300ms,不带单位默认为 s 秒
 *
 * @param cacheName 缓存名
 * @return 超时时间
 */
public static long getTTLFormCacheName(String cacheName) {
	String[] cacheArray = cacheName.split(StringPool.HASH);
	if (cacheArray.length < 2) {
		return -1L;
	}
	Duration duration = DurationStyle.detectAndParse(cacheArray[1], ChronoUnit.SECONDS);
	return duration.toMillis();
}
 
Example #3
Source File: MemcachedCacheProperties.java    From memcached-spring-boot with Apache License 2.0 5 votes vote down vote up
public void setExpirationPerCache(Map<String, String> expirationPerCache) {
    if (expirationPerCache != null) {
        expirationPerCache.forEach((cacheName, cacheExpiration) -> {
            Duration exp = DurationStyle.detect(cacheExpiration).parse(cacheExpiration, ChronoUnit.SECONDS);
            validateExpiration(exp);
            this.expirationPerCache.put(cacheName, exp);
        });
    }
}
 
Example #4
Source File: DataTypeMismatchHighlightingTask.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
private boolean checkType(String type, String text, ClassLoader cl) throws IllegalArgumentException {
    Class<?> clazz = ClassUtils.resolveClassName(type, cl);
    if (clazz != null) {
        try {
            parser.parseType(text, clazz);
        } catch (Exception e1) {
            if (clazz.isEnum()) {
                // generate and try relaxed variants of value
                for (String relaxedName : new RelaxedNames(text)) {
                    try {
                        parser.parseType(relaxedName, clazz);
                        return true;
                    } catch (Exception e2) {
                        // try another variant
                    }
                    if (canceled) {
                        break;
                    }
                }
                return false;
            } else {
                try {
                    // handle a few specific cases where no direct constructor from string or converter exist
                    switch (type) {
                        case "java.nio.file.Path":
                            Paths.get(text);
                            return true;
                        case "java.util.Locale":
                            LocaleUtils.toLocale(text);
                            return true;
                        case "java.time.Duration":
                            DurationStyle.detectAndParse(text);
                            return true;
                        case "org.springframework.util.unit.DataSize":
                            DataSize.parse(text);
                            return true;
                        case "java.lang.Class":
                            cl.loadClass(text);
                            return true;
                        default:
                            return false;
                    }
                } catch (Exception e3) {
                    return false;
                }
            }
        }
    }
    // unresolvable/unknown class, assume user knows what is doing
    return true;
}
 
Example #5
Source File: DiscoveryClientServiceInstanceListSupplier.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
private void resolveTimeout(Environment environment) {
	String providedTimeout = environment.getProperty(SERVICE_DISCOVERY_TIMEOUT);
	if (providedTimeout != null) {
		timeout = DurationStyle.detectAndParse(providedTimeout);
	}
}