Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaje committed Dec 24, 2017
1 parent 327fd41 commit e42b11d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/top/ibase4j/core/util/CacheUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package top.ibase4j.core.util;

import org.apache.commons.lang3.StringUtils;

import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.support.cache.CacheManager;

public class CacheUtil {
Expand Down Expand Up @@ -37,4 +40,31 @@ public static boolean getLock(String key) {
public static void unlock(String key) {
lockManager.unlock(key);
}
}

/**
* 次数检查
* @param key
* @param seconds 缓存时间
* @param frequency 最多次数
* @param message 超出次数提示信息
*/
public static void refreshTimes(String key, int seconds, int frequency, String message) {
if (CacheUtil.getLock(key + "-LOCK")) {
try {
Integer times = 1;
String timesStr = (String)CacheUtil.getCache().get(key);
if (StringUtils.isNotBlank(timesStr)) {
times = Integer.valueOf(timesStr) + 1;
if (times > frequency) {
throw new BusinessException(message);
}
}
CacheUtil.getCache().set(key, times.toString(), seconds);
} finally {
CacheUtil.unlock(key + "-LOCK");
}
} else {
refreshTimes(key, seconds, frequency, message);
}
}
}

0 comments on commit e42b11d

Please sign in to comment.