您的位置:首页 > 财经 > 产业 > 公众号小程序开通_顺德做网站的公司_品牌整合营销推广_网页版

公众号小程序开通_顺德做网站的公司_品牌整合营销推广_网页版

2024/11/17 10:45:45 来源:https://blog.csdn.net/yzcn2013/article/details/143057434  浏览:    关键词:公众号小程序开通_顺德做网站的公司_品牌整合营销推广_网页版
公众号小程序开通_顺德做网站的公司_品牌整合营销推广_网页版

一.在pom.xml文件中引入redis 相关包。

 <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

二.在配置文件application-jdbc.yml 中加入redis的相关配置.

 redis:# redis服务器地址(默认为localhost)#    host: 192.168.124.34host: localhost# redis端口(默认为6379)port: 6379# redis访问密码(默认为空)password: 123456# redis连接超时时间(单位毫秒)timeout: 30000# redis数据库索引(默认为0)database: 10lettuce:pool:max-active: 100 # 连接池最大连接数(使用负值表示没有限制)max-idle: 10 # 连接池中的最大空闲连接min-idle: 5 # 连接池中的最小空闲连接max-wait: 5000ms # 连接池最大阻塞等待时间(使用负值表示没有限制)jedis:pool:enabled: truemax-idle: 100min-idle: 1auto-commit: truemax-wait: 100000max-active: 100time-between-eviction-runs: 10000

三.在代码中编写redis的参数配置,数据调用,数据序列化,redis缓存配置代码

@Configuration
@Service
public class RedisService extends CachingConfigurerSupport {@Value("${spring.redis.database}")private int index;@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.jedis.pool.max-active}")private Integer maxActive;@Value("${spring.redis.jedis.pool.max-idle}")private Integer maxIdle;@Value("${spring.redis.jedis.pool.min-idle}")private Integer minIdle;@Value("${spring.redis.jedis.pool.max-wait}")private long maxWait;@Value("${spring.redis.jedis.pool.enabled}")private boolean enabled;@Value("${spring.redis.jedis.pool.time-between-eviction-runs}")private Integer timeBetweenEvictionRuns;@Beanpublic JedisPool redisPoolFactory() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxTotal(maxActive);jedisPoolConfig.setMaxIdle(maxIdle);jedisPoolConfig.setMinIdle(minIdle);jedisPoolConfig.setMaxWait(Duration.ofMillis(maxWait));jedisPoolConfig.setJmxEnabled(enabled);jedisPoolConfig.setTestOnBorrow(false);jedisPoolConfig.setTestOnReturn(false);// 空闲连接的检测周期jedisPoolConfig.setTimeBetweenEvictionRuns(Duration.ofMillis(timeBetweenEvictionRuns));// 连接的最小空闲时间,达到此值后空闲连接将被移除jedisPoolConfig.setMinEvictableIdleTime(Duration.ofMillis(30000));// 做空闲资源检测时,每次的采样数,默认值3,可根据自身应用连接数进行微调,如果设置为-1,就是对所有连接做空闲监测jedisPoolConfig.setNumTestsPerEvictionRun(-1);JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password,index);return jedisPool;}private Integer time = new Random().nextInt(60 * 60);@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(factory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);return container;}// 从这往下是配置@cache失效时间@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),this.getRedisCacheConfigurationWithTtl(time), // 默认策略,未配置的 key 会使用这个this.getRedisCacheConfigurationMap() // 指定 key 策略);}private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();// 自定义设置缓存时间/* redisCacheConfigurationMap.put("goodsCompCache", this.getRedisCacheConfigurationWithTtl(60 * 60 * 10));*/return redisCacheConfigurationMap;}private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofSeconds(seconds));return redisCacheConfiguration;}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com