package com.javabs.utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author /
*/
@Component
@SuppressWarnings({"unchecked", "all"})
public class RedisUtils {
private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
private RedisTemplate<Object, Object> redisTemplate;
@Value("${jwt.online-key}")
private String onlineKey;
public RedisUtils(RedisTemplate<Object, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
this.redisTemplate.setStringSerializer(new StringRedisSerializer());
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
return true;
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @param timeUnit 单位
*/
public boolean expire(String key, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
redisTemplate.expire(key, time, timeUnit);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据 key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(Object key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 查找匹配key
*
* @param pattern key
* @return /
*/
public List<String> scan(String pattern) {
ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
Cursor<byte[]> cursor = rc.scan(options);
List<String> result = new ArrayList<>();
while (cursor.hasNext()) {
result.add(new String(cursor.next()));
}
try {
RedisConnectionUtils.releaseConnection(rc, factory);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return result;
}
/**
* 分页查询 key
*
* @param patternKey key
* @param page 页码
* @param size 每页数目
* @return /
*/
public List<String> findKeysForPage(String patternKey, int page, int size) {
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
Cursor<byte[]> cursor = rc.scan(options);
List<String> result = new ArrayList<>(size);
int tmpIndex = 0;
int fromIndex = page * size;
int toIndex = page * size + size;
while (cursor.hasNext()) {
if (tmpIndex >= fromIndex && tmpIndex < toIndex) {
result.add(new String(cursor.next()));
tmpIndex++;
continue;
}
// 获取到满足条件的数据后,就可以退出了
if (tmpIndex >= toIndex) {
break;
}
tmpIndex++;
cursor.next();
}
try {
RedisConnectionUtils.releaseConnection(rc, factory);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return result;
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
public void del(String... keys) {
if (keys != null && keys.length > 0) {
if (keys.length == 1) {
boolean result = redisTemplate.delete(keys[0]);
log.debug("--------------------------------------------");
log.debug(new StringBuilder("删除缓存:").append(keys[0]).append(",结果:").append(result).toString());
log.debug("--------------------------------------------");
} else {
Set<Object> keySet = new HashSet<>();
for (String key : keys) {
if (redisTemplate.hasKey(key))
keySet.add(key);
}
long count = redisTemplate.delete(keySet);
log.debug("--------------------------------------------");
log.debug("成功删除缓存:" + keySet.toString());
log.debug("缓存删除数量:" + count + "个");
log.debug("--------------------------------------------");
}
}
}
// ============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 批量获取
*
* @param keys
* @return
*/
public List<Object> multiGet(List<String> keys) {
List list = redisTemplate.opsForValue().multiGet(Sets.newHashSet(keys));
List resultList = Lists.newArrayList();
Optional.ofNullable(list).ifPresent(e-> list.forEach(ele-> Optional.ofNullable(ele).ifPresent(resultList::add)));
return resultList;
}
/**
* 普通缓存放入
*
* @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) {
log.error(e.getMessage(), e);
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) {
log.error(e.getMessage(), e);
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间
* @param timeUnit 类型
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time, TimeUnit timeUnit) {
try
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
springboot+vue的程序 基于人脸识别的图书馆预约系统 除基础功能外,要有 1.签到签退,点击签到签退按钮进行人脸识别签到 2.违规处理,信誉分低于一定值就限制用户的预约功能 3.检举功能,用户可向管理员投递检举消息,管理员在审查过后可以扣除对应用户信誉分 4.暂离功能,用户签到之后可以设置暂离,在时间内需要回来重新签到 5.书籍显示,管理员可以录入书籍信息,显示哪本书在哪个阅览室(1,2,3号.......)选座的时候也根据不同的阅览室选取座位 6.能显示每日总预约量
资源推荐
资源详情
资源评论
收起资源包目录
323-基于人脸识别的图书馆预约系统springboot+vue(源码+数据库) (778个子文件)
MenuServiceImpl.class 21KB
RedisUtils.class 18KB
RoleServiceImpl.class 17KB
DeptServiceImpl.class 17KB
UserServiceImpl.class 17KB
GenUtil.class 13KB
UserController.class 12KB
SpringSecurityConfig.class 12KB
GeneratorServiceImpl.class 11KB
JobServiceImpl.class 11KB
FileUtil.class 11KB
LibraryYuyueController.class 10KB
DictServiceImpl.class 10KB
LibraryVolationServiceImpl.class 10KB
LibraryYuyueServiceImpl.class 10KB
QueryHelp.class 9KB
LibraryBookServiceImpl.class 9KB
MenuController.class 9KB
LibrarySeatServiceImpl.class 9KB
AuthorizationController.class 9KB
LibraryRoomServiceImpl.class 9KB
RoleController.class 9KB
OnlineUserService.class 9KB
HttpUtils.class 8KB
MonitorServiceImpl.class 8KB
StringUtils.class 8KB
DictDetailServiceImpl.class 8KB
LibraryYuyue.class 7KB
DeptController.class 7KB
DictMapperImpl.class 7KB
RoleMapperImpl.class 7KB
RedisConfig.class 7KB
UserLoginMapperImpl.class 7KB
LibraryVolation.class 7KB
UserMapperImpl.class 6KB
LibraryVolationController.class 6KB
GeneratorController.class 6KB
RsaUtils.class 6KB
SwaggerConfig.class 6KB
User.class 6KB
TokenProvider.class 6KB
GlobalExceptionHandler.class 6KB
LibraryYuyueDto.class 6KB
Menu.class 6KB
DictDetailController.class 5KB
MenuVo.class 5KB
LibraryVolationDto.class 5KB
LibrarySeatController.class 5KB
ConfigurerAdapter.class 5KB
Role.class 5KB
LibraryBook.class 5KB
UserDto.class 5KB
OnlineUserDto.class 5KB
DictController.class 5KB
LimitAspect.class 5KB
TokenFilter.class 5KB
SecurityProperties.class 5KB
ColumnInfo.class 5KB
LibraryRoomController.class 5KB
SpringContextHolder.class 5KB
MenuDto.class 5KB
UserQueryCriteria.class 5KB
JobController.class 5KB
LibraryBookController.class 4KB
LoginProperties.class 4KB
LibrarySeat.class 4KB
MenuMapperImpl.class 4KB
DictDetailMapperImpl.class 4KB
DataServiceImpl.class 4KB
FileProperties.class 4KB
DeptQueryCriteria.class 4KB
Dept.class 4KB
HttpUtil.class 4KB
LibraryBookDto.class 4KB
LoginCode.class 4KB
LibraryYuyueMapperImpl.class 4KB
DateUtil.class 4KB
LibraryVolationMapperImpl.class 4KB
JobMapperImpl.class 4KB
DeptMapperImpl.class 4KB
JwtUserDto.class 4KB
AccessToken.class 4KB
MenuQueryCriteria.class 4KB
DeptDto.class 4KB
TableInfo.class 3KB
GenConfig.class 3KB
LibraryBookMapperImpl.class 3KB
OnlineController.class 3KB
UserDetailsServiceImpl.class 3KB
SecurityUtils.class 3KB
LibraryRoom.class 3KB
LibrarySeatDto.class 3KB
LibrarySeatMapperImpl.class 3KB
EncryptUtils.class 3KB
BaseEntity.class 3KB
ApiError.class 3KB
AsyncTaskExecutePool.class 3KB
RoleSmallMapperImpl.class 3KB
LibraryRoomMapperImpl.class 3KB
SwaggerDataConfig$Page.class 3KB
共 778 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
- weixin_554586242024-04-17发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~
Java旅途
- 粉丝: 1w+
- 资源: 472
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功