gpt4 book ai didi

php - 从 PHP SDK 设置后 Couchbase View 未更新

转载 作者:可可西里 更新时间:2023-10-31 23:31:30 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我主要使用 View 来获取数据。
我尝试做的是存储文档,重定向并检查存储的文档是否可用(这是通过 View 完成的,因为我不是在寻找文档键而是在寻找不同的值)。

我在用什么?

Debian Wheezy 上的 PHP(5.4.21)、Couchbase(2.1.1) 和用于 Couchbase(1.1) 的 PHP SDK。

那么发生了什么?

我使用 API 函数 set($id, $document) 存储文档,然后使用 API 函数 view($designDocument, $viewName, $ 触发 View 更新options),其中 $options 至少包含 'stale'=>false,然后重定向到另一个页面,我在其中检查新添加的文档/或者只是想显示它。
但是新添加的文档并不总是显示或者不总是通过我的存在检查。

下面是我使用的更详细的代码:

public function store(AbstractDocument $document)
{
$result = $this->bucket->storeDocument($document->getId(),
json_encode($document));
$this->afterSave();

return $result;
}

public function storeDocument($id, $document)
{
if (!is_string($document)) {
$document = json_encode($document);
}
$res = $this->couchbase->set($id, $document);

return $res;
}

public function afterSave()
{
$this->bucket->triggerViewIndexing(
$this->getDesignDocumentName(),
Config::KEY_COUCHBASE_VIEW_USER_USERLIST
);
}

public function triggerViewIndexing($designDocument, $viewName)
{
$options = ['limit' => 1, 'stale' => false];
$res = $this->couchbase->view($designDocument, $viewName, $options);
}

如代码所示,我将 stale 参数设置为 false,以确保更新索引。
但它似乎不起作用。

在写这个问题之前,我查看了 Couchbase 论坛中的一些主题、Stackoverflow 上的帖子、适用于 Couchbase 的 PHP SDK 的文档以及 Couchbase 的一般文档。
所以我想我明白如何stale应该工作什么limitations好像有。
如果我假设 stale 有效,但仅当文档不再在内存中但正在写入磁盘(或已经写入)时,这是错误的,请随时纠正我。

由于我尝试做的事情不起作用,我尝试了不同的方法,在一些解释和文档中也提到了这些方法应该有助于实现我想要的结果。

例如,我想如果stale的默认行为是update_after,那么触发索引更新两次,就可以解决问题。
好吧,它没有。

其他值得注意的事情是

$this->couchbase->keyDurability($id,$res);
$this->couchbase->observe($id, $res);

我在使用 set 存储文档后直接使用了它们,单独使用它们,出于绝望而组合使用。也没有成功。

我假设这里的错误是,PHP SDK 要么没有通过 stale=false 参数和 keyDurability没有做它应该做的事。当我在检查新创建的文档时也传递 stale=false 时,当然两者(触发和检查)都在同一个存储桶和 View 上工作。

或者我在没有注意到的情况下做了一些可怕的错误。

如果有人能为我指出正确的方向并希望解释问题出在哪里,我会很高兴,因为我无法理解正在发生的事情。根据我的理解,一切都应该至少与 keyDurabilitystale 一起工作。

最佳答案

关于更新 View 的“问题”是,它只使用持久保存到磁盘的数据。您可以使用 keyDurability() 来检查是否发生了这种情况,但是您需要您刚刚编写的元素的 id 和 cas。

我会将您的 triggerViewIndexing 函数更改为如下所示。

/**
* @param $designDocument
* @param $viewName
* @param null $id
* @param null $cas
*/
public function triggerViewIndexing($designDocument, $viewName, $id = null, $cas = null)
{
if (!empty($id) && !empty($cas)) {
$details = [
'persist_to' => 1, //The number of nodes the document should be persisted to
'timeout' => 2000, // The max time to wait for durability
'interval' => 100 //The interval between checking the state of the document
];
$this->couchbase->keyDurability($id, $cas, $details);
}

$options = ['limit' => 1, 'stale' => false];
$res = $this->couchbase->view($designDocument, $viewName, $options);
}

如果项目被写入磁盘,这将每 100 毫秒检查一次,最多 2000 毫秒。之后,触发 View 会刷新数据。

关于php - 从 PHP SDK 设置后 Couchbase View 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20103737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com