ValueOperations<String, T> operation = redisTemplate.opsForValue();
时间: 2024-04-04 15:29:15 浏览: 9
这行代码是用来获取操作Redis中存储的值类型为泛型T的数据的操作对象,其中String表示存储数据的键的数据类型,T表示存储数据的值的数据类型。通过该操作对象可以实现对Redis中的值类型为泛型T的数据进行读写操作,例如获取数据、设置数据、递增递减等操作。这个操作对象是通过redisTemplate.opsForValue()方法获取的,redisTemplate是操作Redis的模板对象,opsForValue()方法返回的是针对值类型操作的操作对象。值得注意的是,如果操作的值类型不是泛型T,需要使用相应的操作对象进行操作。
相关问题
Map<String, Object> stringObjectMap = JSONUtils.jsonStrToMap(params); 怎么转成<String.String>
要将Map<String, Object>转换为Map<String, String>,你可以遍历原始的Map,然后将每个键值对中的值转换为String类型,并将其放入新的Map中。以下是示例代码:
```java
Map<String, Object> stringObjectMap = JSONUtils.jsonStrToMap(params);
Map<String, String> stringStringMap = new HashMap<>();
for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
String stringValue = String.valueOf(value);
stringStringMap.put(key, stringValue);
}
```
这样,你就可以将Map<String, Object>转换为Map<String, String>了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [map与json互转](https://blog.csdn.net/weixin_42005527/article/details/95659548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
代码优化 public String getApplyId() { ValueOperations<String,Long> value = redisTemplate.opsForValue(); LocalDateTime dateTime = LocalDateTime.now(); String date = dateTime.format(DateTimeFormatter.ofPattern("yyMMdd")); Long count = 0L; String append = "Reconciliation:AD" + date; String dateString = "AD" + date; if (value.get(append) == null) { // 获取数据库的值 LambdaQueryWrapper<ReconciliationApply> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(ReconciliationApply::getPurchaseCode, dateString); ReconciliationApply selectOne = applyMapper.selectOne(wrapper); if (selectOne == null) { value.setIfAbsent(append, count); redisTemplate.expire(append, 1, TimeUnit.DAYS); } else { String purchaseCode = selectOne.getPurchaseCode(); String substring = purchaseCode.substring(8, 13); value.setIfAbsent(append, Long.parseLong(substring)); redisTemplate.expire(append, 1, TimeUnit.DAYS); } } Long increment = value.increment(append, 1); String str = String.valueOf(increment); return getSequence(dateString, str); } /** * 格式化 key * * @param str * @return */ public static String getSequence(String dateString, String str) { StringBuilder stringJoiner = new StringBuilder(dateString); int len = str.length(); // 取决于业务规模 4 if (len == DEFAULT_LENGTH) { return stringJoiner.append(str).toString(); } if (str.length() > DEFAULT_LENGTH) { throw new RuntimeException("申请单号已使用完成,请明天再申请"); } int rest = DEFAULT_LENGTH - len; for (int i = 0; i < rest; i++) { stringJoiner.append("0"); } stringJoiner.append(str); return stringJoiner.toString(); }
可以考虑对代码进行如下优化:
1. 将 Redis 的操作放到单独的方法中,方便复用和维护。
2. 对于 Redis 中的值的获取和设置,可以使用 Redis 的 get/set 方法代替 opsForValue() 方法,简化代码。
3. 对于 Redis 中的键的格式,可以使用 String.format() 方法代替 StringBuilder,使代码更加简洁。
4. 对于日期和时间的格式化,可以使用 Java 8 中的 DateTimeFormatter 做进一步优化。
综上所述,代码可以优化为:
```
public String getApplyId() {
LocalDateTime dateTime = LocalDateTime.now();
String dateString = "AD" + dateTime.format(DateTimeFormatter.ofPattern("yyMMdd"));
String key = String.format("Reconciliation:%s", dateString);
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
String value = valueOps.get(key);
if (value == null) {
ReconciliationApply apply = applyMapper.selectOne(
new LambdaQueryWrapper<ReconciliationApply>().eq(ReconciliationApply::getPurchaseCode, dateString));
if (apply == null) {
valueOps.setIfAbsent(key, "0001");
} else {
String purchaseCode = apply.getPurchaseCode();
String suffix = purchaseCode.substring(8);
valueOps.setIfAbsent(key, suffix);
}
redisTemplate.expire(key, 1, TimeUnit.DAYS);
}
long sequence = valueOps.increment(key, 1);
String str = String.format("%04d", sequence);
return dateString + str;
}
```