/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lanqiao.project.system.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author /
*/
@SuppressWarnings({"unchecked", "all"})
public class RedisUtils {
private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @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(String 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<String> keySet = new HashSet<>();
for (String key : keys) {
keySet.addAll(redisTemplate.keys(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(keys);
List resultList = new ArrayList();
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
没有合适的资源?快使用搜索试试~ 我知道了~
基于Java、Vue、TypeScript、HTML的聊天室设计源码
共471个文件
java:213个
vue:76个
ts:65个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 122 浏览量
2024-10-08
15:54:46
上传
评论
收藏 12.85MB ZIP 举报
温馨提示
本项目是一款全栈式聊天室源码,采用Java、Vue、TypeScript和HTML等技术栈开发,共计包含472个文件,其中Java源文件213个,Vue组件76个,TypeScript脚本65个,SVG图形35个,XML配置24个,Vue模板12个,Markdown文件5个,YAML配置5个,JSON数据5个,SCSS样式5个。该聊天室系统功能完善,界面友好,适用于各种在线交流场景。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java、Vue、TypeScript、HTML的聊天室设计源码 (471个子文件)
commitlint.config.cjs 4KB
.eslintrc.cjs 3KB
.prettierrc.cjs 2KB
.stylelintrc.cjs 1KB
commit-msg 38B
.env.development 268B
.editorconfig 487B
.eslintignore 116B
401.gif 159KB
.gitignore 506B
.gitignore 395B
.gitignore 175B
index.html 2KB
index1.html 230B
favicon.ico 4KB
RedisUtils.java 19KB
SysMenuServiceImpl.java 15KB
SysDeptServiceImpl.java 9KB
SysRoleServiceImpl.java 9KB
GlobalExceptionHandler.java 8KB
SysDictServiceImpl.java 8KB
SysUserServiceImpl.java 8KB
MinioOssService.java 7KB
UserImportListener.java 7KB
AuthServiceImpl.java 7KB
SysUserController.java 6KB
MyWebConfig.java 5KB
WebSocketConfig.java 5KB
SysConfigServiceImpl.java 5KB
SecurityConfig.java 5KB
JwtValidationFilter.java 4KB
ChatFriendServiceImpl.java 4KB
IPUtils.java 4KB
SysLogServiceImpl.java 4KB
ResultCode.java 4KB
SysRoleMenuServiceImpl.java 4KB
MyDataPermissionHandler.java 4KB
JwtUtils.java 4KB
AliyunOssService.java 3KB
LogAspect.java 3KB
SysRoleController.java 3KB
MQProducerService.java 3KB
SysMenuController.java 3KB
DuplicateSubmitAspect.java 3KB
WebMvcConfig.java 3KB
RedisCacheConfig.java 3KB
PermissionService.java 3KB
SysDictController.java 3KB
SecurityUtils.java 3KB
CaptchaValidationFilter.java 3KB
SysUserRoleServiceImpl.java 3KB
SysDeptController.java 3KB
SysUserDetails.java 2KB
ChatMessage.java 2KB
MQConsumerService.java 2KB
JavaTypeEnum.java 2KB
SysConfigController.java 2KB
IBaseEnum.java 2KB
ChatDynamicsServiceImpl.java 2KB
DateUtils.java 2KB
ChatMessageServiceImpl.java 2KB
SysUserService.java 2KB
ResponseUtils.java 2KB
SysRoleService.java 2KB
ChatGroupServiceImpl.java 2KB
ChatDynamicsDao.java 2KB
ChatFriendDao.java 2KB
ChatMessageDao.java 2KB
AuthController.java 2KB
SysMenu.java 2KB
ChatGroupDao.java 2KB
Result.java 2KB
ChatFriendController.java 2KB
ChatStarDao.java 2KB
SysLogController.java 2KB
WebsocketController.java 2KB
CaptchaProperties.java 2KB
XxlJobConfig.java 2KB
GenFieldConfigServiceImpl.java 2KB
ChatGroup.java 2KB
SymbolConstant.java 2KB
GenFieldConfig.java 2KB
SysMenuService.java 2KB
MybatisConfig.java 2KB
SysConfigService.java 2KB
SysLogMapper.java 2KB
ChatDynamics.java 2KB
SysDictService.java 2KB
RedisServiceImpl.java 2KB
SysUserMapper.java 2KB
FormTypeEnum.java 2KB
SysUserDetailsService.java 2KB
CaptchaConfig.java 2KB
CorsConfig.java 2KB
MyAuthenticationEntryPoint.java 1KB
ChatFriendService.java 1KB
QueryTypeEnum.java 1KB
SysDeptService.java 1KB
GenConfigConverter.java 1KB
RouteBO.java 1KB
共 471 条
- 1
- 2
- 3
- 4
- 5
资源评论
lsx202406
- 粉丝: 2386
- 资源: 5568
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功