我正在开发一个应用程序,主要使用视图来获取数据
我尝试做的是,存储文档,重定向并检查存储的文档是否可用(这是通过视图完成的,因为我不是在寻找文档密钥,而是在寻找不同的值)。
我在用什么
PHP(5.4.21)、Couchbase(2.1.1)和用于Couchbase的PHP SDK(1.1)。
那么发生了什么
我使用API函数set($id, $document)
存储文档,然后使用API函数view($designDocument, $viewName, $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
的默认行为是update_after
,那么触发两次索引更新就可以解决问题
事实并非如此。
其他值得注意的是
$this->couchbase->keyDurability($id,$res);
$this->couchbase->observe($id, $res);
我在用set
存储文档后直接使用了这些,分别使用,出于绝望,组合使用。也没成功。
我的假设是,这里的错误是,PHP SDK要么在某种程度上没有通过stale=false
参数,要么keyDurability没有完成它应该做的事情。当我在检查新创建的文档时也传递stale=false
时,当然(触发器和检查)都在同一个bucket和视图上工作。
或者我在没有注意到的情况下做了一些可怕的错误。
如果有人能给我指明正确的方向,并希望能解释出了什么问题,我会很高兴,因为我无法理解发生了什么。根据我的理解,任何东西都应该至少与keyDurability
和stale
一起工作。
更新视图的"问题"在于,它只使用持久化到磁盘的数据。您可以使用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毫秒。之后,触发视图将刷新数据。