124 lines
4.3 KiB
Java
124 lines
4.3 KiB
Java
package com.easypan.utils;
|
|
|
|
import com.easypan.entity.constants.Constants;
|
|
import com.easypan.entity.dto.DownloadFileDto;
|
|
import com.easypan.entity.dto.SysSettingsDto;
|
|
import com.easypan.entity.dto.UserSpaceDto;
|
|
import com.easypan.entity.po.FileInfo;
|
|
import com.easypan.entity.po.UserInfo;
|
|
import com.easypan.entity.query.FileInfoQuery;
|
|
import com.easypan.entity.query.UserInfoQuery;
|
|
import com.easypan.mappers.FileInfoMapper;
|
|
import com.easypan.mappers.UserInfoMapper;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
@Component
|
|
public class RedisComponent {
|
|
|
|
@Resource
|
|
private RedisUtils redisUtils;
|
|
|
|
@Resource
|
|
private FileInfoMapper<FileInfo, FileInfoQuery> fileInfoMapper;
|
|
|
|
@Resource
|
|
private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;
|
|
|
|
/**
|
|
* 获取系统设置
|
|
* 如果不存在就新建
|
|
*/
|
|
public SysSettingsDto getSysSettingsDto() {
|
|
SysSettingsDto sysSettingsDto = (SysSettingsDto) redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
|
|
if (sysSettingsDto == null) {
|
|
sysSettingsDto = new SysSettingsDto();
|
|
saveSysSettingsDto(sysSettingsDto);
|
|
}
|
|
return sysSettingsDto;
|
|
}
|
|
|
|
/**
|
|
* 保存设置
|
|
*
|
|
* @param sysSettingsDto
|
|
*/
|
|
public void saveSysSettingsDto(SysSettingsDto sysSettingsDto) {
|
|
redisUtils.set(Constants.REDIS_KEY_SYS_SETTING, sysSettingsDto);
|
|
}
|
|
|
|
/**
|
|
* 保存已使用的空间
|
|
*
|
|
* @param userId
|
|
*/
|
|
public void saveUserSpaceUse(String userId, UserSpaceDto userSpaceDto) {
|
|
redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId,
|
|
userSpaceDto, Constants.REDIS_KEY_EXPIRES_DAY);
|
|
}
|
|
|
|
/**
|
|
* 获取用户使用的空间
|
|
*/
|
|
public UserSpaceDto getUserSpaceUse(String userId) {
|
|
UserSpaceDto spaceDto = (UserSpaceDto) redisUtils.get(Constants.REDIS_KEY_USER_SPACE_USE + userId);
|
|
if (null == spaceDto) {
|
|
spaceDto = new UserSpaceDto();
|
|
Long useSpace = this.fileInfoMapper.selectUseSpace(userId);
|
|
spaceDto.setUseSpace(useSpace);
|
|
spaceDto.setTotalSpace(getSysSettingsDto().getUserInitUseSpace() * Constants.MB);
|
|
redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId, spaceDto, Constants.REDIS_KEY_EXPIRES_DAY);
|
|
}
|
|
return spaceDto;
|
|
}
|
|
|
|
//保存文件临时大小
|
|
public void saveFileTempSize(String userId, String fileId, Long fileSize) {
|
|
Long currentSize = getFileTempSize(userId, fileId);
|
|
redisUtils.setex(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId,
|
|
currentSize + fileSize, Constants.REDIS_KEY_EXPIRES_ONE_HOUR);
|
|
}
|
|
|
|
public void removeFileTempSize(String userId, String fileId) {
|
|
redisUtils.delete(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId);
|
|
}
|
|
|
|
public Long getFileTempSize(String userId, String fileId) {
|
|
// Long currentSize = getFileSizeFromRedis(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId);
|
|
// return currentSize;
|
|
String key = Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId;
|
|
Object sizeObj = redisUtils.get(key);
|
|
if (sizeObj == null) {
|
|
return 0L;
|
|
}
|
|
if (sizeObj instanceof Integer) {
|
|
return ((Integer) sizeObj).longValue();
|
|
} else if (sizeObj instanceof Long) {
|
|
return (Long) sizeObj;
|
|
}
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
public void saveDownloadCode(String code, DownloadFileDto downloadFileDto) {
|
|
redisUtils.setex(Constants.REDIS_KEY_DOWNLOAD + code,
|
|
downloadFileDto, Constants.REDIS_KEY_EXPIRES_FIVE_MIN);
|
|
}
|
|
|
|
public DownloadFileDto getDownloadCode(String code) {
|
|
return (DownloadFileDto) redisUtils.get(Constants.REDIS_KEY_DOWNLOAD + code);
|
|
}
|
|
public UserSpaceDto resetUserSpaceUse(String userId) {
|
|
UserSpaceDto spaceDto = new UserSpaceDto();
|
|
Long useSpace = this.fileInfoMapper.selectUseSpace(userId);
|
|
spaceDto.setUseSpace(useSpace);
|
|
|
|
UserInfo userInfo = this.userInfoMapper.selectByUserId(userId);
|
|
spaceDto.setTotalSpace(userInfo.getTotalSpace());
|
|
redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId, spaceDto, Constants.REDIS_KEY_EXPIRES_DAY);
|
|
return spaceDto;
|
|
}
|
|
}
|