Skip to content

Commit

Permalink
optimise code
Browse files Browse the repository at this point in the history
  • Loading branch information
Halcyon666 committed Mar 11, 2024
1 parent dca816f commit 6525ad9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/main/java/whalefall/mvc/RequestTestController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package whalefall.mvc;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* RequestBody与RequestParam测试
*
Expand Down Expand Up @@ -45,7 +45,7 @@ public String myTestController1(@RequestBody User user) {
}

/**
* 以较复杂的Team对象接收前端传过来的json数据 (SpringMVC会智能的将符合要求的数据装配进该Teamr对象中)
* 以较复杂的Team对象接收前端传过来的json数据 (SpringMVC会智能的将符合要求的数据装配进该Team对象中)
* 注:如果后端@RequestBody后的对象,持有了集合等,当前端向传参 令该对象持有的该集合为空时,json字符串中,
* 对应位置应该形如"teamMembers":[]这么写;即:传递的json字符串中必须要有key,否者请求会出错
*
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/whalefall/redis/controller/RedisController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package whalefall.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -10,29 +12,43 @@
* @Date: 2021/9/4 19:44
*/
@RestController
@Slf4j
public class RedisController {
private static int cnt = 0;
private static Object object = new Object();

@Autowired
private RedisTemplate<String, String> redisTemplate;

/**
* http://localhost:8080/get?key=a
*/
@RequestMapping("/get")
public String get(String key) {
Boolean flag = redisTemplate.opsForValue().setIfAbsent("lock", "lock");
if (!flag) return "error";
Boolean flag = getValueOperations().setIfAbsent("lock", "lock");
if (Boolean.FALSE.equals(flag)) {
return "error";
}

try {
cnt++;
System.out.println("get " + key + "第" + cnt + "次");
}finally {
log.info("lock has gotten");
} finally {
redisTemplate.delete("lock");
}
return redisTemplate.opsForValue().get(key);
return getValueOperations().get(key);
}

private ValueOperations<String, String> getValueOperations() {
return redisTemplate.opsForValue();
}

/**
* http://localhost:8080/put?key=a
*/
@RequestMapping("/put")
public void put(String key) {
redisTemplate.opsForValue().set(key, key);
getValueOperations().set(key, key);
}

}
12 changes: 6 additions & 6 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ spring:
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.3.162:7001
- 192.168.3.162:7002
- 192.168.3.162:7003
- 192.168.3.162:7004
- 192.168.3.162:7005
- 192.168.3.162:7006
- 192.168.3.163:7001
- 192.168.3.163:7002
- 192.168.3.163:7003
- 192.168.3.163:7004
- 192.168.3.163:7005
- 192.168.3.163:7006
lettuce:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
Expand Down
25 changes: 17 additions & 8 deletions src/test/java/whalefall/RedisTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package whalefall;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@SpringBootTest
public class RedisTest implements ApplicationContextAware {
Expand All @@ -22,17 +24,24 @@ public class RedisTest implements ApplicationContextAware {
@Autowired
private List<RedisTemplate> redisTemplateList;

@Test
// @Test
public void test() {
// redisTemplateList.forEach(ele -> System.err.println(ele));
//
// redisTemplate.opsForValue().set("name", "admin");
// String name = redisTemplate.opsForValue().get("name");
// System.err.println(name); //输出admin

redisTemplate.opsForValue().set("name", "admin");
Set<String> keys = showAllKeys();
Assertions.assertTrue(!CollectionUtils.isEmpty(keys), "keys is empty");
showAllKeys();
}

private Set<String> showAllKeys() {
Set<String> keys = redisTemplate.keys("*");
assert keys != null;
keys.forEach(System.out::println);
return keys;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
}

0 comments on commit 6525ad9

Please sign in to comment.