admin
2019-08-30 6c7c445a0f4462c4e94f24a40bd1f59e8cc6fd63
金币请求频率限制添加时间
2个文件已修改
39 ■■■■■ 已修改文件
fanli/src/main/java/com/yeshi/fanli/aspect/integral/IntegralGetFrequencyLimitAspect.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/util/annotation/integral/IntegralGetFrequencyLimit.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/aspect/integral/IntegralGetFrequencyLimitAspect.java
@@ -16,22 +16,20 @@
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.yeshi.utils.NumberUtil;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.annotation.integral.IntegralGetFrequencyLimit;
import com.yeshi.fanli.util.annotation.integral.IntegralGetVersionLimit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
@Aspect
public class IntegralGetFrequencyLimitAspect {
    @Resource
    private RedisManager redisManager;
    private JedisPool jedisPool;
    private ExpressionParser parser = new SpelExpressionParser();
@@ -60,17 +58,29 @@
            if (realMethod.isAnnotationPresent(IntegralGetFrequencyLimit.class)) {
                IntegralGetFrequencyLimit rs = realMethod.getAnnotation(IntegralGetFrequencyLimit.class);
                String key = rs.key();
                String catchKey = StringUtil.Md5("integral-get-frequency-" + generateKeyBySpEL(key, joinPoint));
                int timeS = rs.time();
                if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(catchKey))) {
                    return null;
                }
                String catchKey = "integral-get-frequency-" + StringUtil.Md5(generateKeyBySpEL(key, joinPoint));
                Jedis jedis = jedisPool.getResource();
                try {
                    return joinPoint.proceed();
                    if (jedis.setnx(catchKey, "1") > 0) {
                        jedis.expire(catchKey, timeS);
                        try {
                            return joinPoint.proceed();
                        } catch (Exception e) {// 异常了本次请求不算
                            jedis.del(catchKey);
                        }
                    } else {
                        return null;
                    }
                } catch (Exception e) {
                    try {
                        return joinPoint.proceed();
                    } finally {
                    }
                } finally {
                    // 30分钟内不允许操作
                    redisManager.cacheCommonString(catchKey, "1", 60 * 30);
                    jedisPool.returnResource(jedis);
                }
            }
        } catch (NoSuchMethodException e) {
fanli/src/main/java/com/yeshi/fanli/util/annotation/integral/IntegralGetFrequencyLimit.java
@@ -19,4 +19,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface IntegralGetFrequencyLimit {
    String key();
    // 时间 (s)默认时间为1800s
    int time() default 30 * 60;
}