package com.example.config; import com.example.utils.FastJson2JsonRedisSerializer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; 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.script.DefaultRedisScript; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; import java.io.*; import java.util.Map; import java.util.Properties; /** * redis配置 * * @author ltkj */ @Configuration @EnableCaching @Slf4j public class RedisConfig extends CachingConfigurerSupport { @Autowired private ConfigValue configValue; @Bean @SuppressWarnings(value = {"unchecked", "rawtypes"}) public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { log.info("Initializing RedisTemplate..."); RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); // 使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(serializer); // Hash的key也采用StringRedisSerializer的序列化方式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); log.info("RedisTemplate initialized"); return template; } @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig config = new JedisPoolConfig(); // 设置JedisPoolConfig的相关参数,例如最大连接数、最大空闲时间等 // config.setMinIdle(0); // config.setMaxIdle(8); // config.setMaxTotal(8); // config.setMaxWaitMillis(-1); // config.setTestOnBorrow(true); // config.setTestOnReturn(true); return config; } @Bean public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) { JedisConnectionFactory factory = new JedisConnectionFactory(); Map configMap = configValue.getConfigMap(); // 从文件中读取配置信息 // 获取属性值并赋值 factory.setPoolConfig(jedisPoolConfig); // 设置Redis服务器的地址和端口号 factory.setHostName(configMap.get("redisIp")); factory.setPort(Integer.parseInt(configMap.get("redisProt"))); // 如果需要密码验证,设置密码 factory.setPassword(configMap.get("redisPassword")); // 设置其他参数,如数据库索引等 factory.setDatabase(Integer.parseInt(configMap.get("redisIpDatabase"))); // 最后,初始化连接 factory.afterPropertiesSet(); log.info("redis连接成功!!!"); return factory; } @Bean public DefaultRedisScript limitScript() { DefaultRedisScript redisScript = new DefaultRedisScript<>(); redisScript.setScriptText(limitScriptText()); redisScript.setResultType(Long.class); return redisScript; } /** * 限流脚本 */ private String limitScriptText() { return "local key = KEYS[1]\n" + "local count = tonumber(ARGV[1])\n" + "local time = tonumber(ARGV[2])\n" + "local current = redis.call('get', key);\n" + "if current and tonumber(current) > count then\n" + " return tonumber(current);\n" + "end\n" + "current = redis.call('incr', key)\n" + "if tonumber(current) == 1 then\n" + " redis.call('expire', key, time)\n" + "end\n" + "return tonumber(current);"; } }