springboot+quartz+redis⽤法,防⽌任务并发执⾏
  值得注意的是,创建项⽬时,最好要规范些,避免不必要的⿇烦,像config、model、service、util等包,最好创建在主包下⾯,也就是Application启动类的上⾯,因为springboot是⾃上⽽下进⾏扫描的,具体⽬录结构,请看最下⾯
在配置⽂件,添加如下配置
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--ObjectMapper-->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
创建RedisConfig类
package com.fig;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.tion.RedisConnectionFactory;
import org.RedisTemplate;
import org.dis.serializer.Jackson2JsonRedisSerializer;
import org.dis.serializer.StringRedisSerializer;
/**
* redis配置类
**/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
// 使⽤Jackson2JsonRedisSerialize 替换默认序列化(默认采⽤的是JDK序列化)
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//  RedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
// key采⽤String的序列化⽅式
template.setKeySerializer(jackson2JsonRedisSerializer);
// hash的key也采⽤String的序列化⽅式
template.setHashKeySerializer(jackson2JsonRedisSerializer);
// value序列化⽅式采⽤jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化⽅式采⽤jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
package com.howdy.demo.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import urrent.TimeUnit;
/**
* Redis⼯具类
*/
@Service
public class RedisUtil{
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// =============================common============================ /**
* 指定缓存失效时间
*
* @param key  键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/
**
* 根据key 获取过期时间
*
* @param key 键不能为null
* @return时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
Expire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
e.printStackTrace();
return false;
}
}
/
**
* 删除缓存
*
* @param key 可以传⼀个值或多个
*/
@SuppressWarnings("unchecked")
public void key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
// ============================String============================= /**
* 普通缓存获取
*
* @param key 键
* @return值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放⼊
*
* @param key  键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放⼊并设置时间
* @param key  键
* @param value 值
* @param time  时间(秒) time要⼤于0 如果time⼩于等于0 将设置⽆限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key  键
* @param delta 要增加⼏(⼤于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因⼦必须⼤于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key  键
* @param delta 要减少⼏(⼩于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {springframework依赖
throw new RuntimeException("递减因⼦必须⼤于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Map================================= /**
* HashGet
*
* @param key  键不能为null
* @param item 项不能为null
* @return值
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key  键
* @param map  对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) { try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。