解决合并冲突
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DateTimeUtils {
|
||||
|
||||
/**
|
||||
* 判断时间是否重叠
|
||||
*
|
||||
* @param start1 开始时间1
|
||||
* @param end1 开始时间1
|
||||
* @param start2 开始时间2
|
||||
* @param end2 开始时间2
|
||||
* @return 是否重叠
|
||||
*/
|
||||
public static boolean isOverlap(Date start1, Date end1, Date start2, Date end2) {
|
||||
// 判断是否重叠:start1 < end2 && end1 > start2
|
||||
return start1.before(end2) && end1.after(start2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
/**
|
||||
* @ClassName IdCardAgeCalculator
|
||||
* @Description 根据身份证号 计算年纪
|
||||
* @Author raymond
|
||||
* @Date 2025/11/4 10:32
|
||||
* @Version 1.0
|
||||
**/
|
||||
public class IdCardAgeCalculator {
|
||||
/**
|
||||
* 根据身份证号计算年龄(支持15/18位)
|
||||
* @param idCard 身份证号
|
||||
* @return 年龄(若身份证号非法,返回-1)
|
||||
*/
|
||||
public static int calculateAge(String idCard) {
|
||||
// 1. 校验身份证号合法性(基础校验:长度、非空)
|
||||
if (!isValidIdCard(idCard)) {
|
||||
System.out.println("身份证号格式非法!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 2. 提取出生日期字符串(15位转18位格式)
|
||||
String birthDateStr = extractBirthDateStr(idCard);
|
||||
|
||||
// 3. 解析出生日期为LocalDate(处理格式异常)
|
||||
LocalDate birthDate;
|
||||
try {
|
||||
birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
} catch (DateTimeParseException e) {
|
||||
System.out.println("出生日期解析失败,身份证号可能非法!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 4. 计算年龄(当前日期 - 出生日期)
|
||||
LocalDate currentDate = LocalDate.now(); // 若需指定日期,替换为 LocalDate.of(2024, 5, 20)
|
||||
return calculateAgeBetweenDates(birthDate, currentDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基础校验身份证号(非空、长度15或18位、数字/最后一位X)
|
||||
*/
|
||||
private static boolean isValidIdCard(String idCard) {
|
||||
if (idCard == null || idCard.trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// 正则:15位纯数字,或18位数字(最后一位可大写X)
|
||||
String regex = "^(\\d{15}|\\d{17}([0-9]|X))$";
|
||||
return idCard.matches(regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从身份证号中提取出生日期字符串(统一转为yyyyMMdd格式)
|
||||
*/
|
||||
private static String extractBirthDateStr(String idCard) {
|
||||
String birthDateStr;
|
||||
if (idCard.length() == 18) {
|
||||
// 18位:第7-14位(索引6-13)
|
||||
birthDateStr = idCard.substring(6, 14);
|
||||
} else {
|
||||
// 15位:第7-12位(索引6-11),补前两位年份(19xx或20xx,这里简化为19xx,实际需根据规则判断)
|
||||
String year = "19" + idCard.substring(6, 8);
|
||||
String monthDay = idCard.substring(8, 12);
|
||||
birthDateStr = year + monthDay;
|
||||
}
|
||||
return birthDateStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据出生日期和当前日期计算年龄(处理未过生日的情况)
|
||||
*/
|
||||
private static int calculateAgeBetweenDates(LocalDate birthDate, LocalDate currentDate) {
|
||||
// 计算年份差
|
||||
Period period = Period.between(birthDate, currentDate);
|
||||
int age = period.getYears();
|
||||
|
||||
// 检查当前日期是否已超过当年的出生日期(未超过则年龄减1)
|
||||
LocalDate birthdayThisYear = birthDate.withYear(currentDate.getYear());
|
||||
if (currentDate.isBefore(birthdayThisYear)) {
|
||||
age--;
|
||||
}
|
||||
|
||||
// 防止年龄为负数(如出生日期在当前日期之后,理论上身份证号不会出现此情况)
|
||||
return Math.max(age, 0);
|
||||
}
|
||||
|
||||
// 测试示例
|
||||
public static void main(String[] args) {
|
||||
// 测试1:18位身份证(出生日期2000-01-01,当前日期2024-05-20 → 年龄24)
|
||||
String idCard18 = "110101200001011234";
|
||||
System.out.println("18位身份证年龄:" + calculateAge(idCard18));
|
||||
|
||||
// 测试2:15位身份证(出生日期2000-01-01 → 15位表示为000101,补全后20000101)
|
||||
String idCard15 = "110101000101123";
|
||||
System.out.println("15位身份证年龄:" + calculateAge(idCard15));
|
||||
|
||||
// 测试3:未过生日的情况(出生日期2000-06-01,当前日期2024-05-20 → 年龄23)
|
||||
String idCardNotBirthday = "110101200006011234";
|
||||
System.out.println("未过生日的年龄:" + calculateAge(idCardNotBirthday));
|
||||
|
||||
// 测试4:非法身份证号(长度错误)
|
||||
String idCardInvalid = "11010120000101123"; // 17位
|
||||
System.out.println("非法身份证年龄:" + calculateAge(idCardInvalid));
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,16 @@
|
||||
*/
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.openhis.common.constant.CommonConstants;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import com.openhis.common.constant.CommonConstants;
|
||||
|
||||
/**
|
||||
* 身份证号生成工具类
|
||||
*
|
||||
@@ -98,4 +99,74 @@ public class IdCardUtil {
|
||||
throw new IllegalArgumentException("无法解析生日日期: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据身份证号计算年龄
|
||||
*
|
||||
* @param idCard 身份证号码(15位或18位)
|
||||
* @return 计算得到的年龄(整数)
|
||||
* @throws IllegalArgumentException 如果身份证号格式无效或包含非法字符
|
||||
*/
|
||||
public static Integer calculateAgeFromIdCard(String idCard) {
|
||||
// 验证身份证号基本格式
|
||||
if (idCard == null || (idCard.length() != 15 && idCard.length() != 18)) {
|
||||
throw new IllegalArgumentException("身份证号码长度无效,必须是15位或18位");
|
||||
}
|
||||
|
||||
// 验证身份证号字符合法性(仅包含数字和X)
|
||||
if (!idCard.matches("^[0-9Xx]+$")) {
|
||||
throw new IllegalArgumentException("身份证号码包含非法字符");
|
||||
}
|
||||
|
||||
try {
|
||||
// 提取出生日期
|
||||
String birthYearStr, birthMonthStr, birthDayStr;
|
||||
|
||||
if (idCard.length() == 15) {
|
||||
// 15位身份证:7-8位为年份(后两位),9-10位为月份,11-12位为日期
|
||||
birthYearStr = "19" + idCard.substring(6, 8);
|
||||
birthMonthStr = idCard.substring(8, 10);
|
||||
birthDayStr = idCard.substring(10, 12);
|
||||
} else {
|
||||
// 18位身份证:7-10位为年份,11-12位为月份,13-14位为日期
|
||||
birthYearStr = idCard.substring(6, 10);
|
||||
birthMonthStr = idCard.substring(10, 12);
|
||||
birthDayStr = idCard.substring(12, 14);
|
||||
}
|
||||
|
||||
// 转换为整数
|
||||
int birthYear = Integer.parseInt(birthYearStr);
|
||||
int birthMonth = Integer.parseInt(birthMonthStr);
|
||||
int birthDay = Integer.parseInt(birthDayStr);
|
||||
|
||||
// 验证日期有效性
|
||||
if (birthMonth < 1 || birthMonth > 12 || birthDay < 1 || birthDay > 31) {
|
||||
throw new IllegalArgumentException("身份证中的出生日期无效");
|
||||
}
|
||||
|
||||
// 获取当前日期
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int currentYear = calendar.get(Calendar.YEAR);
|
||||
int currentMonth = calendar.get(Calendar.MONTH) + 1; // Calendar月份从0开始
|
||||
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
// 计算年龄
|
||||
int age = currentYear - birthYear;
|
||||
|
||||
// 判断是否已过生日
|
||||
if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay < birthDay)) {
|
||||
age--; // 未过生日,年龄减1
|
||||
}
|
||||
|
||||
// 验证年龄合理性
|
||||
if (age < 0 || age > 150) {
|
||||
throw new IllegalArgumentException("计算得到的年龄超出合理范围");
|
||||
}
|
||||
|
||||
return age;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("身份证号码中的日期部分格式错误", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.openhis.common.utils;
|
||||
|
||||
/**
|
||||
* Redis所有Keys
|
||||
*/
|
||||
public class RedisKeys {
|
||||
|
||||
|
||||
/**
|
||||
* 商品缓存
|
||||
* @param itemId
|
||||
* @return
|
||||
*/
|
||||
public static String getProductsKey(String itemId){
|
||||
return "products_change_price:item_" + itemId + "_key";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,612 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* redis 工具类
|
||||
* @Author Scott
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 指定缓存失效时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean expire(String key, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key 获取过期时间
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @return 时间(秒) 返回0代表为永久有效
|
||||
*/
|
||||
public long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断key是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
return redisTemplate.hasKey(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
*
|
||||
* @param key 可以传一个值 或多个
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void del(String... key) {
|
||||
if (key != null && key.length > 0) {
|
||||
if (key.length == 1) {
|
||||
redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
redisTemplate.delete((Collection<String>) 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 by 要增加几(大于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 by 要减少几(小于0)
|
||||
* @return
|
||||
*/
|
||||
public long decr(String key, long delta) {
|
||||
if (delta < 0) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除hash表中的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 可以使多个 不能为null
|
||||
*/
|
||||
public void hdel(String key, Object... item) {
|
||||
redisTemplate.opsForHash().delete(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断hash表中是否有该项的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 不能为null
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hHasKey(String key, String item) {
|
||||
return redisTemplate.opsForHash().hasKey(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要增加几(大于0)
|
||||
* @return
|
||||
*/
|
||||
public double hincr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, by);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递减
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要减少记(小于0)
|
||||
* @return
|
||||
*/
|
||||
public double hdecr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, -by);
|
||||
}
|
||||
|
||||
// ============================set=============================
|
||||
/**
|
||||
* 根据key获取Set中的所有值
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> sGet(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value从一个set中查询,是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean sHasKey(String key, Object value) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().isMember(key, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据放入set缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSet(String key, Object... values) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().add(key, values);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将set数据放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSetAndTime(String key, long time, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().add(key, values);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取set缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long sGetSetSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().size(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除值为value的
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().remove(key, values);
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// ===============================list=================================
|
||||
|
||||
/**
|
||||
* 获取list缓存的内容
|
||||
*
|
||||
* @param key 键
|
||||
* @param start 开始
|
||||
* @param end 结束 0 到 -1代表所有值
|
||||
* @return
|
||||
*/
|
||||
public List<Object> lGet(String key, long start, long end) {
|
||||
try {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取list缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long lGetListSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForList().size(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过索引 获取list中的值
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
||||
* @return
|
||||
*/
|
||||
public Object lGetIndex(String key, long index) {
|
||||
try {
|
||||
return redisTemplate.opsForList().index(key, index);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据索引修改list中的某条数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().set(key, index, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除N个值为value
|
||||
*
|
||||
* @param key 键
|
||||
* @param count 移除多少个
|
||||
* @param value 值
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long lRemove(String key, long count, Object value) {
|
||||
try {
|
||||
Long remove = redisTemplate.opsForList().remove(key, count, value);
|
||||
return remove;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定前缀的一系列key
|
||||
* 使用scan命令代替keys, Redis是单线程处理,keys命令在KEY数量较多时,
|
||||
* 操作效率极低【时间复杂度为O(N)】,该命令一旦执行会严重阻塞线上其它命令的正常请求
|
||||
* @param keyPrefix
|
||||
* @return
|
||||
*/
|
||||
private Set<String> keys(String keyPrefix) {
|
||||
String realKey = keyPrefix + "*";
|
||||
|
||||
try {
|
||||
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
|
||||
Set<String> binaryKeys = new HashSet<>();
|
||||
Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(realKey).count(Integer.MAX_VALUE).build());
|
||||
while (cursor.hasNext()) {
|
||||
binaryKeys.add(new String(cursor.next()));
|
||||
}
|
||||
|
||||
return binaryKeys;
|
||||
});
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定前缀的一系列key
|
||||
* @param keyPrefix
|
||||
*/
|
||||
public void removeAll(String keyPrefix) {
|
||||
try {
|
||||
Set<String> keys = keys(keyPrefix);
|
||||
redisTemplate.delete(keys);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user