GVKun编程网logo

为什么post后不能使用数据(React JS)(post为什么不能被缓存)

13

在本文中,您将会了解到关于为什么post后不能使用数据的新资讯,同时我们还将为您解释ReactJS的相关在本文中,我们将带你探索为什么post后不能使用数据的奥秘,分析ReactJS的特点,并给出一些

在本文中,您将会了解到关于为什么post后不能使用数据的新资讯,同时我们还将为您解释React JS的相关在本文中,我们将带你探索为什么post后不能使用数据的奥秘,分析React JS的特点,并给出一些关于c – 为什么我不能使用auto和std :: thread?、java – 为什么Postgres Replication Stream在单独的函数中使用时不起作用?、postgresql – 为什么Postgres在jsonb列上查找这么慢?、postgresql – 为什么在结构调用pg_ctl restart后需要睡眠的实用技巧。

本文目录一览:

为什么post后不能使用数据(React JS)(post为什么不能被缓存)

为什么post后不能使用数据(React JS)(post为什么不能被缓存)

.then(data => console.log(data.PathPDF))

您从 .then 回调返回的任何值都会成为结果承诺中的值。 console.log 返回 undefined,因此这就是您传入的第二个 .then 的值。

更改此设置以重新返回 data

.then(data => {
  console.log(data.PathPDF)
  return data;
})
.then(data => window.open(data.PathPDF,''_blank'',''noopener,noreferrer''));

或者只有一个.then

.then(data => {
  console.log(data.PathPDF);
  return window.open(data.PathPDF,noreferrer'');
})

c – 为什么我不能使用auto和std :: thread?

c – 为什么我不能使用auto和std :: thread?

我遇到了std :: thread的问题,因为它不接受采用自动指定参数的函数.以下是一些示例代码:
#include <iostream>
#include <vector>
#include <thread>

using namespace std;

void seev(const auto &v) // works fine with const vector<int> &v
{
    for (auto x : v)
        cout << x << ' ';
    cout << "\n\n";
}

int main()
{
    vector<int> v1 { 1,2,3,4,5 };
    thread t(seev,v1);
    t.join();
    return 0;
}

但是编译器说:

[Error] no matching function for call to 'std::thread::thread(<unresolved overloaded function type>,std::vector<int>&)'

为什么会这样?这是语言或GCC(4.9.2)的问题吗?

解决方法

将auto视为模板参数,然后您的函数如下所示:
template <class T>
void seev (const T &v) ...

如果没有明确的类型规范,C就无法生成模板.这就是你得到错误的原因.要解决问题(使用模板参数声明),您可以使用:

thread t (seev<decltype(v1)>,std::ref(v1));

java – 为什么Postgres Replication Stream在单独的函数中使用时不起作用?

java – 为什么Postgres Replication Stream在单独的函数中使用时不起作用?

我正在研究postgres复制流API.在努力工作时遇到了不寻常的行为.

当我使用复制槽在主块内写入整个代码时,一切正常.

public class Server implements Config {

public static void main(String[] args) {
    Properties prop = new Properties();
    prop.load(new FileInputStream(System.getProperty("prop")));

    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    String url = prop.getProperty("url");

    Properties props = new Properties();
    PGProperty.USER.set(props,user);
    PGProperty.PASSWORD.set(props,password);
    PGProperty.ASSUME_MIN_SERVER_VERSION.set(props,"9.4");
    PGProperty.REPLICATION.set(props,"database");
    PGProperty.PREFER_QUERY_MODE.set(props,"simple");

    Connection conn= null;
    PGConnection replicationConnection= null;
    PGReplicationStream stream = null;

        conn = DriverManager.getConnection(url,props);
        replicationConnection = conn.unwrap(PGConnection.class);
        stream = replicationConnection.getReplicationAPI().replicationStream().logical()
                .withSlotName("replication_slot")
                .withSlotOption("include-xids",true)
                .withSlotOption("include-timestamp","on")
                .withSlotOption("skip-empty-xacts",true)
                .withStatusInterval(20,TimeUnit.SECONDS).start();

    while (true) {
            ByteBuffer msg;
            try {
                msg = stream.readPending();         

            if (msg == null) {
                TimeUnit.MILLISECONDS.sleep(10L);
                continue;
            }

            int offset = msg.arrayOffset();
            byte[] source = msg.array();
            int length = source.length - offset;
            // convert byte buffer into string
            String data = new String(source,offset,length);

            // then convert it into bufferedreader
            BufferedReader reader = new BufferedReader(new StringReader(data));
            String line = reader.readLine();

            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            stream.setAppliedLSN(stream.getLastReceiveLSN());
            stream.setFlushedLSN(stream.getLastReceiveLSN());
        } catch (sqlException | IOException | InterruptedException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
        }
}

但是当我尝试使用这样的单独方法分离流逻辑时

public class Server implements Config {

public static void main(String[] args) {
    PGReplicationStream stream = getReplicationStream();
        while (true) {
            ByteBuffer msg;
            try {
                msg = stream.readPending();
    if (msg == null) {
                TimeUnit.MILLISECONDS.sleep(10L);
                continue;
            }

            int offset = msg.arrayOffset();
            byte[] source = msg.array();
            int length = source.length - offset;
            String data = new String(source,length);

            BufferedReader reader = new BufferedReader(new StringReader(data));
            String line = reader.readLine();

            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            stream.setAppliedLSN(stream.getLastReceiveLSN());
            stream.setFlushedLSN(stream.getLastReceiveLSN());
        } catch (sqlException | IOException | InterruptedException e) {
            e.printstacktrace();
        }
        }

}
public static PGReplicationStream getReplicationStream() {
    Properties prop = new Properties();
    try {
        prop.load(new FileInputStream(System.getProperty("prop")));
    } catch (IOException e1) {
        // Todo Auto-generated catch block
        e1.printstacktrace();
    }

    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    String url = prop.getProperty("url");

    Properties props = new Properties();
    PGProperty.USER.set(props,"simple");

    Connection conn= null;
    PGConnection replicationConnection= null;
    PGReplicationStream stream = null;
    try {
        conn = DriverManager.getConnection(url,TimeUnit.SECONDS).start();
    } catch (sqlException e) {
        e.printstacktrace();
    }
    return stream;
}

}

读完一些数据后,程序出错了

org.postgresql.util.PsqlException: Database connection Failed when reading from copy
at org.postgresql.core.v3.QueryExecutorImpl.readFromcopy(QueryExecutorImpl.java:1028)
at org.postgresql.core.v3.copyDualImpl.readFromcopy(copyDualImpl.java:41)
at org.postgresql.core.v3.replication.V3PGReplicationStream.receiveNextData(V3PGReplicationStream.java:155)
at org.postgresql.core.v3.replication.V3PGReplicationStream.readInternal(V3PGReplicationStream.java:124)
at org.postgresql.core.v3.replication.V3PGReplicationStream.readPending(V3PGReplicationStream.java:78)
at Server.main(Server.java:47)
Caused by: java.net.socketException: Socket closed
at java.net.socketInputStream.socketRead0(Native Method)
at java.net.socketInputStream.socketRead(SocketInputStream.java:116)
at java.net.socketInputStream.read(SocketInputStream.java:171)
at java.net.socketInputStream.read(SocketInputStream.java:141)
at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:191)
at org.postgresql.core.PGStream.receive(PGStream.java:495)
at org.postgresql.core.PGStream.receive(PGStream.java:479)
at org.postgresql.core.v3.QueryExecutorImpl.processcopyResults(QueryExecutorImpl.java:1161)
at org.postgresql.core.v3.QueryExecutorImpl.readFromcopy(QueryExecutorImpl.java:1026)
... 5 more

我认为两种方法之间没有任何区别.但该计划表现不同.有人可以解释,第二种方法有什么问题.

解决方法

我相信你的问题与连接.一旦你的函数返回它就会超出范围,并且垃圾收集器会收集并最终确定它.在最终确定中,连接已关闭,然后您的程序可能会失败.尝试在main方法中可用的范围内移动连接和其他所需的中间变量,然后重试.

postgresql – 为什么Postgres在jsonb列上查找这么慢?

postgresql – 为什么Postgres在jsonb列上查找这么慢?

我有一个表格目标,其中包含text []类型的marital_status列和另一个类型为jsonb的列数据.这两列的内容是相同的,只是采用不同的格式(仅用于演示目的).示例数据:

id |      marital_status      |                        data                       
----+--------------------------+---------------------------------------------------
  1 | null                     | {}
  2 | {widowed}                | {"marital_status": ["widowed"]}
  3 | {never_married,divorced} | {"marital_status": ["never_married","divorced"]}
...

表中随机组合中有超过690K条记录.

在text []列上查找

EXPLAIN ANALYZE SELECT marital_status
FROM targeting
WHERE marital_status @> ''{widowed}''::text[]

没有索引

通常需要< 900毫秒没有创建任何索引:

Seq Scan on targeting  (cost=0.00..172981.38 rows=159061 width=28) (actual time=0.017..840.084 rows=158877 loops=1)
  Filter: (marital_status @> ''{widowed}''::text[])
  Rows Removed by Filter: 452033
Planning time: 0.150 ms
Execution time: 845.731 ms

有索引

使用索引通常需要< 200毫秒(75%的改进):

CREATE INDEX targeting_marital_status_idx ON targeting ("marital_status");

结果:

Index Only Scan using targeting_marital_status_idx on targeting  (cost=0.42..23931.35 rows=159061 width=28) (actual time=3.528..143.848 rows=158877 loops=1)"
  Filter: (marital_status @> ''{widowed}''::text[])
  Rows Removed by Filter: 452033
  Heap Fetches: 0
Planning time: 0.217 ms
Execution time: 148.506 ms

在jsonb列上查找

EXPLAIN ANALYZE SELECT data
FROM targeting
WHERE (data -> ''marital_status'') @> ''["widowed"]''::jsonb

没有索引

通常需要< 5,700毫秒没有创建任何指数(慢6倍以上!):

Seq Scan on targeting  (cost=0.00..174508.65 rows=611 width=403) (actual time=0.095..5399.112 rows=158877 loops=1)
  Filter: ((data -> ''marital_status''::text) @> ''["widowed"]''::jsonb)
  Rows Removed by Filter: 452033
Planning time: 0.172 ms
Execution time: 5408.326 ms

有索引

对于索引,通常需要< 3,700毫秒(改善35%):

CREATE INDEX targeting_data_marital_status_idx ON targeting USING GIN ((data->''marital_status''));

结果:

Bitmap Heap Scan on targeting  (cost=144.73..2482.75 rows=611 width=403) (actual time=85.966..3694.834 rows=158877 loops=1)
  Recheck Cond: ((data -> ''marital_status''::text) @> ''["widowed"]''::jsonb)
  Rows Removed by Index Recheck: 201080
  Heap Blocks: exact=33723 lossy=53028
  ->  Bitmap Index Scan on targeting_data_marital_status_idx  (cost=0.00..144.58 rows=611 width=0) (actual time=78.851..78.851 rows=158877 loops=1)"
        Index Cond: ((data -> ''marital_status''::text) @> ''["widowed"]''::jsonb)
Planning time: 0.257 ms
Execution time: 3703.492 ms

问题

>为什么text []列的性能更高,即使不使用索引?
>为什么在jsonb列中添加索引只会使性能提高35%?
>在jsonb列上执行查找是否有更多的性能?

解决方法

似乎是一个简单的问题.基本上你问的是怎么来的,

CREATE TABLE foo ( id int,key1 text );

比快

CREATE TABLE bar ( id int,jsonb foo );

@Craig在评论中回答了这个问题

GIN indexing is generally less efficient than a b-tree,so that much is expected.

此模式中的空值也应该读取

SELECT jsonb_build_object(''marital_status'',ARRAY[null]);
     jsonb_build_object     
----------------------------
 {"marital_status": [null]}
(1 row)

并不是 {}. Postgresql采用了许多快捷方式来快速更新jsonb对象,并使索引节省空间.

如果这些都没有意义,请查看此伪表.

CREATE TABLE foo ( id int,x text,y text,z text )
CREATE INDEX ON foo(x);
CREATE INDEX ON foo(y);
CREATE INDEX ON foo(z);

这里我们有三个btree索引.我们来看一个类似的表..

CREATE TABLE bar ( id int,junk jsonb );
CREATE INDEX ON bar USING gin (junk);
INSERT INTO bar (id,junk) VALUES (1,$${"x": 10,"y": 42}$$);

对于像foo一样执行的条形图,我们需要两个btree,它们都将比我们拥有的单个GIN索引大得多.如果你这样做了

INSERT INTO bar (id,"y": 42,"z":3}$$);

我们必须在z上有另一个btree索引,这也将是巨大的.你可以看到我要去哪里. jsonb很棒,但索引和模式建模的复杂性并不与数据库并行.您不能只将数据库缩减为jsonb列,发出CREATE INDEX并期望获得相同的性能.

postgresql – 为什么在结构调用pg_ctl restart后需要睡眠

postgresql – 为什么在结构调用pg_ctl restart后需要睡眠

我正在使用Fabric初始化postgres服务器.我必须在命令的末尾添加一个“sleep 1”,或者postgres服务器处理die而没有解释或日志中的条目:
sudo('%(pgbin)s/pg_ctl -D %(pgdata)s -l /tmp/pg.log restart && sleep 1' % env,user='postgres')

也就是说,我在终端上看到了这个输出:

[dbserv] Executing task ‘setup_postgres’
[dbserv] run: /bin/bash -l -c “sudo -u postgres /usr/lib/postgresql/9.1/bin/pg_ctl -D /data/pg -l /tmp/pg.log restart && sleep 1”
[dbserv] out: waiting for server to shut down…. done
[dbserv] out: server stopped
[dbserv] out: server starting

没有&&睡眠1,/ tmp /pg.log中没有任何内容(虽然创建了文件),并且没有运行postgres进程.随着睡眠,一切正常.

(如果我直接在目标机器的命令行上执行相同的命令,它可以在没有睡眠的情况下正常工作.)

由于它的工作,它并不重要,但我还是要问:有人知道睡眠是允许发生的,为什么?

您可能还尝试使用 pty option将其设置为false,并查看它是否与结构处理伪ttys有关.

今天关于为什么post后不能使用数据React JS的介绍到此结束,谢谢您的阅读,有关c – 为什么我不能使用auto和std :: thread?、java – 为什么Postgres Replication Stream在单独的函数中使用时不起作用?、postgresql – 为什么Postgres在jsonb列上查找这么慢?、postgresql – 为什么在结构调用pg_ctl restart后需要睡眠等更多相关知识的信息可以在本站进行查询。

本文标签: