如何做转运网站,黄聪 wordpress,产品造型设计,商城系统网站模板文章目录 前言一、 springboot 版本二、引入 redis 依赖三、增加配置文件四、增加配置类1、 RedissonConfig2、RedisConfig 五、增加操作类#xff0c;主要操作 string 总结 前言
一、 springboot 版本 spring-boot.version2.3.5.RELEASE/spring-boot.version主要操作 string 总结 前言
一、 springboot 版本 spring-boot.version2.3.5.RELEASE/spring-boot.versiondependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion${spring-boot.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement二、引入 redis 依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency三、增加配置文件
spring:redis:cluster:nodes: 192.168.162.235:6379,192.168.162.235:6380,192.168.162.235:6381,192.168.162.235:6382,192.168.162.235:6383,192.168.162.235:6384max-redirects: 3pool:#最大空闲连接max-idle: 8#最小空闲连接min-idle: 0#最大连接数-1表示是没有限制max-active: 8#最大阻塞等待时间-1表示没有限制max-wait: -1#连接超时时间毫秒timeout: 60000commandTimeout: 5000password: 123456connectionTimeout: 60000四、增加配置类
1、 RedissonConfig
Configuration
public class RedissonConfig {Value(${spring.redis.cluster.nodes})private String[] nodes;Value(${spring.redis.password})private String password;Value(${spring.redis.connectionTimeout})private int connectionTimeout;Bean(destroyMethod shutdown)public RedissonClient redissonClient() {Config config new Config();ClusterServersConfig clusterServersConfig config.useClusterServers();clusterServersConfig.addNodeAddress(Stream.of(nodes).map((node) - redis:// node).toArray(String[]::new)).setConnectTimeout(connectionTimeout);if (StringUtils.isNotBlank(password)) {clusterServersConfig.setPassword(password);}return Redisson.create(config);}
}2、RedisConfig
Configuration
public class RedisConfig {BeanConditionalOnMissingBean(name redisTemplate)public RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, Object template new RedisTemplate();// 配置连接工厂template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值默认使用JDK的序列化方式Jackson2JsonRedisSerializer jacksonSeial new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();// 指定要序列化的域field,get和set,以及修饰符范围ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型类必须是非final修饰的final修饰的类比如String,Integer等会跑出异常// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jacksonSeial.setObjectMapper(om);// 值采用json序列化template.setValueSerializer(jacksonSeial);//使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());// 设置hash key 和value序列化模式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(jacksonSeial);template.afterPropertiesSet();return template;}BeanConditionalOnMissingBean(StringRedisTemplate.class)public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {StringRedisTemplate template new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}
}五、增加操作类主要操作 string
Setter
Service
public class StringRedisTemplateClient {Resourceprivate StringRedisTemplate stringRedisTemplate;/*** 已废弃不兼容低版本的spring-data-redis*/Deprecatedpublic Boolean expire(String key, Integer seconds) {Boolean succeedstringRedisTemplate.expire(key, Duration.ofSeconds(seconds));return succeed;}/*** 单独key的过期时间*/public Boolean expire(String key, long timeout, TimeUnit unit) {Boolean succeedstringRedisTemplate.expire(key, timeout,unit);return succeed;}//get,set,setex,需要调用方自己序列化valuepublic String get(String key) {String valuestringRedisTemplate.opsForValue().get(key);return value;}public void set(String key, String value, long timeout, TimeUnit unit) {stringRedisTemplate.opsForValue().set(key, value, timeout, unit);}public void set(String key, String value) {stringRedisTemplate.opsForValue().set(key, value);}//对象或泛型版的 set,setex,get。不用自己手动序列化public void set(String key, Object value) {String jsonStr JSONObject.toJSONString(value);set(key,jsonStr);}public void setex(String key, String value, Integer seconds) {stringRedisTemplate.opsForValue().set(key, value, Duration.ofSeconds(seconds));}public void setex(String key, String value, Long seconds) {stringRedisTemplate.opsForValue().set(key, value, Duration.ofSeconds(seconds));}public void setex(String key, Object value, Integer seconds) {String jsonStr JSONObject.toJSONString(value);setex(key,jsonStr,seconds);}public long ttl(String key) {Long expiresIn stringRedisTemplate.opsForValue().getOperations().getExpire(key, TimeUnit.SECONDS);return expiresIn;}public T T get(String key, ClassT clazz) {String jsonStr get(key);return JSONObject.parseObject(jsonStr, clazz);}public T ListT getList(String key, ClassT clazz) {String jsonStr get(key);return JSONObject.parseArray(jsonStr, clazz);}/*** 检查某个key是否存在* param key* return*/public boolean exist(String key) {String value get(key);return StringUtils.isNotBlank(value);}public Boolean delete(String key) {Boolean succeedstringRedisTemplate.delete(key);return succeed;}public Long incr(String key) {Long increment stringRedisTemplate.opsForValue().increment(key);return increment;}
}总结