redis并发量大
@GetMapping(value = "/limitRate") public ServiceResult limitRate() { ServiceResult serviceResult = null; if(redisManage.getValue("LimitCount")!=null) { Integer countExist = (Integer) redisManage.getValue("LimitCount"); Long expireTimes = redisManage.getExpire("LimitCount"); if(expireTimes>-1) { if(countExist>10) { serviceResult = new ServiceResult(-102,"LimitCount没秒超过10次访问,返回错误"); return serviceResult; }else { String count = String.valueOf(countExist+1); redisManage.increValue("LimitCount"); serviceResult = new ServiceResult(HttpResultEnum.SUCCESS); return serviceResult; }else { redisManage.delValue("LimitCount"); redisManage.putValueExpireTimes("LimitCount",1,10L); serviceResult = new ServiceResult(100,"LimitCount超时,删除后,创建LimitCount=1"); serviceResult.setData(1); return serviceResult; }else { redisManage.putValueExpireTimes("LimitCount",1,10L); serviceResult = new ServiceResult(100,"LimitCount不存在,创建LimitCount=1"); serviceResult.setData(1); return serviceResult; }
/** * @param key * @return */ public Integer increValue(String key) { ValueOperationsvalueOperations = redisTemplate.opsForValue(); try{ valueOperations.increment(key,1); LoggerUtil.info(logger, "key自增=" + valueOperations.get(key)); }catch (Exception ex) { return (Integer) valueOperations.get(key); /** * @param key * @return */ public void delValue(String key) { LoggerUtil.info(logger, "删除key=" + key); if (redisTemplate.hasKey(key)) { /** * @param key * @param value * @param expireTimes default 3600s */ public void putValueExpireTimes(String key,String value,Long expireTimes) { LoggerUtil.info(logger, "保存key=" + key+";value=" + value); if(expireTimes==null || expireTimes == 0L) { expireTimes = 3600L; LoggerUtil.info(logger, "设置超时时间:" + redisTemplate.getExpire(key, TimeUnit.SECONDS)); }
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.min-idle}") private int minIdle; @Value("${spring.redis.pool.max-active}") private int maxActive; /** * @param factory * @return */ public RedisTemplateredisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template); //设置序列化工具,这样ReportBean不需要实现Serializable接口 template.afterPropertiesSet(); return template; private void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); /** * @return */ public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); //存储的库 factory.setDatabase(database); //设置连接超时时间 factory.setTimeout(timeout); factory.setUsePool(true); return factory; /** * @return */ public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); return jedisPoolConfig; }
以上就是土嘎嘎小编为大家整理的Redis实现限流功能相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!