我的任务是使用nginx实现微缓存策略,即缓存一些POST端点的响应几秒钟。
在http部分nginx.conf我有以下内容:
proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;
Run Code Online (Sandbox Code Playgroud)
然后我location有server:
location /my-url/ {
root dir;
client_max_body_size 50k;
proxy_cache cache;
proxy_cache_valid 10s;
proxy_cache_methods POST;
proxy_cache_key "$request_uri|$request_body";
proxy_ignore_headers Vary;
add_header X-Cached $upstream_cache_status;
proxy_pass http://my-upstream;
}
Run Code Online (Sandbox Code Playgroud)
该应用程序位于输出处,如果我理解正确的my-upstream话Cache-Control: max-age=10,应该使响应可缓存。
但是当我在短时间内(不到10秒)使用curl发出重复请求时
curl -v --data "a=b&c=d" https://my-host/my-url/1573
Run Code Online (Sandbox Code Playgroud)
它们全部到达后端(根据后端日志)。还有,X-Cached总是MISS。
请求和响应如下:
> POST /my-url/1573 HTTP/1.1
> Host: my-host
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 113
> Content-Type: application/x-www-form-urlencoded
>
* upload …Run Code Online (Sandbox Code Playgroud) 我有以下测试。
@Test
void withoutColon_fails() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+0400");
}
@Test
void withColon_ok() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+04:00");
}
Run Code Online (Sandbox Code Playgroud)
我试图解析的日期时间之间的唯一区别是,在第一种情况下,时区指定的小时和分钟之间没有冒号,而在第二种情况下有一个冒号。
第一个失败:
java.time.format.DateTimeParseException: Text '2019-01-24T12:10:58.036820+0400' could not be parsed, unparsed text found at index 29
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)
Run Code Online (Sandbox Code Playgroud)
根据 javadoc, parse(), through DateTimeFormatter.ISO_ZONED_DATE_TIME, 最终使用DateTimeFormatter.ISO_OFFSET_DATE_TIME, 反过来,我们可以读到它是
ISO 日期时间格式化程序,用于格式化或解析具有偏移量的日期时间,例如“2011-12-03T10:15:30+01:00”。
进一步遵循 javadoc 链接,我们会看到ZoneOffset.getId()它表示它接受:
Z - for UTC (ISO-8601)
+hh:mm or -hh:mm - if the seconds are zero (ISO-8601)
+hh:mm:ss or -hh:mm:ss - if the seconds are non-zero (not ISO-8601) …Run Code Online (Sandbox Code Playgroud) 我在部署 Web 应用程序时遇到问题。我使用 maven + spring security 并且服务器不断报告以下问题:
错误:com.sun.tools.javac.code.Symbol$CompletionFailure:找不到 org.springframework.security.ldap.DefaultSpringSecurityContextSource 的类文件
这是我的 pom 文件和依赖项列表:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.online_exchange</groupId>
<artifactId>online_exchange</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>online_exchange</name>
<properties>
<springframework.version>4.1.6.RELEASE</springframework.version>
<springsecurity.version>4.0.1.RELEASE</springsecurity.version>
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql.connector.version>5.1.31</mysql.connector.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> …Run Code Online (Sandbox Code Playgroud) 我有一个简单的回声控制器
@RestController
public class EchoController {
@GetMapping(path = "/param", produces = MediaType.TEXT_PLAIN_VALUE)
String echoParam(@RequestParam("p") String paramValue) {
return paramValue;
}
@GetMapping(path = "/path-variable/{val}", produces = MediaType.TEXT_PLAIN_VALUE)
String echoPathVariable(@PathVariable("val") String val) {
return val;
}
}
Run Code Online (Sandbox Code Playgroud)
它的一种方法与它所呈现的参数的值相呼应;第二个对通过 URI 提供的值执行相同的操作。
我有以下测试:
@Autowired
private WebTestClient webTestClient;
@Test
public void rawPlus_inQueryParam() {
String value = "1+1";
String response = getValueEchoedThroughQueryParam(value);
assertThat(response, is(equalTo(value)));
}
@Test
public void urlencodedPlus_inQueryParam() {
String value = "1%2B1";
String response = getValueEchoedThroughQueryParam(value);
assertThat(response, is(equalTo(value)));
}
private String getValueEchoedThroughQueryParam(String value) { …Run Code Online (Sandbox Code Playgroud) 我有以下工作配置.gitlab-ci.yml:
job1:
stage: test
services:
- name: mariadb
alias: mysql
entrypoint: [""]
command: [...]
script:
- ...
job2:
stage: test
services:
- name: mariadb
alias: mysql
entrypoint: [""]
command: [...]
script:
- ...
job3:
stage: test
services:
- name: mariadb
alias: mysql
entrypoint: [""]
command: [...]
script:
- ...
Run Code Online (Sandbox Code Playgroud)
services 所有3个工作的部分相同.
是否有可能避免这种重复?
I have something like this:
@RestController
@RequestMapping("/{id}")
public class MyController {
@GetMapping
public String get(@PathVariable String id) {
...
}
@PostMapping
public String post(@PathVariable String id, Payload payload) {
...
}
@GetMapping("/deeper/{id}")
public String getDeeper(@PathVariable String id) {
....
}
}
Run Code Online (Sandbox Code Playgroud)
This gives 3 mappings:
I would like the third of them to be just /deeper/{id} (GET).
Is it possible to do this leaving the method in the same controller and leaving that …
我有一个简单的接口及其实现:
interface Iface {
fun doSomething(s: String)
}
class IfaceImpl : Iface {
override fun doSomething(s: String) {
println("Doing the job, s = $s")
}
}
Run Code Online (Sandbox Code Playgroud)
此外,还有两个相同的(至少我看不出区别)调用处理程序,一个在 Java 中,一个在 Kotlin 中:
public class JavaHandler implements InvocationHandler {
private final Iface target;
public JavaHandler(Iface target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Java handler works");
return method.invoke(target, args);
}
}
class KotlinHandler(private val target: Iface) : InvocationHandler {
override fun invoke(proxy: …Run Code Online (Sandbox Code Playgroud) 我是编程的新手,我想问为什么在我的代码中我不需要在构造函数和方法中使用return函数?
另外为什么在使用yearPasses函数之后,年龄增加3而不是1?
为漫长的代码道歉
public class Person
{
private int age;
public Person(int initialAge)
{
// Add some more code to run some checks on initialAge
if (initialAge<0)
{
System.out.println("Age is not valid, setting age to 0.");
initialAge = 0;
age = initialAge;
}
else
{
age = initialAge;
}
}
public void amIOld()
{
if (age<13)
{
System.out.println("You are young.");
}
else if (age>=13 && age<18)
{
System.out.println("You are a teenager.");
}
else
{
System.out.println("You are old.");
}
}
public …Run Code Online (Sandbox Code Playgroud)