在前两篇文章
strings
hashes
lists
sets
sorted sets
封锁一个IP地址
存储用户信息
模拟队列
自动排重
以某一个条件为权重,进行排序
– java后端项目结构
/**
* Redis工具类
*
* @author zhuhuix
* @date 2020-06-15
* @date 2020-06-17 增加队列操作方法
*/
@Component
@AllArgsConstructor
public class RedisUtils {
private RedisTemplate<Object, Object> redisTemplate;
...
/**
* 入队
*
* @param key 队列键值
* @param value 元素
* @return 添加数量
*/
public long leftPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 向队列头部添加全部集合元素
*
* @param key 队列键值
* @param list 集合
* @return 返回添加的数量
*/
public long leftPushAll(String key, List<Object> list) {
return redisTemplate.opsForList().leftPushAll(key, list);
}
/**
* 统计队列中所有元素数量
*
* @param key 队列键值
* @return 队列中元素数量
*/
public long size(String key){
return redisTemplate.opsForList().size(key);
}
/**
* 返回队列中从起始位置到结束位置的集合元素
*
* @param key 队列键值
* @param start 起始位置
* @param end 结束位置
* @return 返回集合
*/
public List<Object> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 出队
*
* @param key 队列键值
* @return 元素
*/
public Object rightPop(String key){
return redisTemplate.opsForList().rightPop(key);
}
/**
* 弹出队列最新元素
*
* @param key 队列键值
* @return 元素
*/
public Object leftPop(String key) {
return redisTemplate.opsForList().leftPop(key);
}
/**
* 删除队列所有元素
*
* @param key 队列键值
*/
public void deleteAll(String key){
redisTemplate.opsForList().trim(key,0,0);
redisTemplate.opsForList().leftPop(key);
}
...
}
/**
* 微信小程序CRM实现类
*
* @author zhuhuix
* @date 2020-04-20
* @date 2020-06-17 将最新的上传信息推入Redis队列
*/
@Slf4j
@AllArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WxMiniCrmImpl implements WxMiniCrm {
...
@Override
@Transactional(rollbackFor = Exception.class)
public Result<CrmIndex> uploadCrmIndex(String json, String openId, String realName, MultipartFile multipartFile) {
try {
JSONObject jsonObject = JSONObject.parseObject(json);
String createTime = jsonObject.getString("create");
String employeeCode = jsonObject.getString("employeeCode");
String customerCode = jsonObject.getString("customerCode");
String customerName = jsonObject.getString("customerName");
String type = jsonObject.getString("type");
if (StringUtils.isEmpty(createTime) || StringUtils.isEmpty(employeeCode) || StringUtils.isEmpty(customerCode)
|| StringUtils.isEmpty(customerName) || StringUtils.isEmpty(type)) {
throw new RuntimeException("上传信息中缺少关键资料");
}
UploadFile uploadFile = uploadFileTool.upload(openId, realName, multipartFile);
if (uploadFile == null) {
throw new RuntimeException("上传文件失败!");
}
CrmIndex crmIndex = new CrmIndex();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
crmIndex.setCreateTime(Timestamp.valueOf(LocalDateTime.parse(createTime, dateTimeFormatter)));
crmIndex.setEmployeeCode(employeeCode);
crmIndex.setCustomerCode(customerCode);
crmIndex.setCustomerName(customerName);
crmIndex.setType(type);
crmIndex.setJson(json);
crmIndex.setOpenId(openId);
crmIndex.setPath(uploadFile.getPath());
// 将最新10条上传的信息放入redis缓存
if (redisUtils.size(Constant.REDIS_UPLOAD_QUEUE_NAME) >= Constant.REDIS_UPLOAD_QUEUE_COUNT) {
log.warn(Constant.REDIS_UPLOAD_QUEUE_NAME.concat("队列已满,移除最旧上传信息:") + redisUtils.rightPop(Constant.REDIS_UPLOAD_QUEUE_NAME));
}
log.info(Constant.REDIS_UPLOAD_QUEUE_NAME.concat("队列增加上传信息:").concat(crmIndex.toString()));
redisUtils.leftPush(Constant.REDIS_UPLOAD_QUEUE_NAME, crmIndex);
return new Result<CrmIndex>().ok(crmIndexRepository.save(crmIndex));
} catch (JSONException ex) {
throw new RuntimeException("json转换失败:" + ex.getMessage());
}
}
...
}
文件上传的原理与实现可参考该文章《SpringBoot实现微信小程序文件上传的完整案例》
Redis项目应用场景与实例(三)后端源码
Redis项目应用场景与实例(三)前端源码
原网址: 访问
创建于: 2021-02-04 14:25:23
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论