问题描述
对Coucbahse的写操作接受一个参数 cas
(创建和设置)。此外,任何非数据提取查询的返回结果对象都有 cas
属性。我google了一下,找不到一个很好的概念文章。
Write operations on Coucbahse accepts a parameter cas
(create and set). Also the return result object of any non-data fetching query has cas
property in it. I googled a bit and couldn't found a good conceptual article about it.
有人可以告诉我什么时候使用 CAS 以及如何操作?使用CAS 的常见工作流程应该是什么?
Could anyone tell me when to use CAS and how to do it? What should be the common work-flow of using CAS?
我的猜测是我们需要获取 CAS 第一次写操作,然后与下一次写操作一起传递。此外,我们需要使用result的 CAS 进行更新。
My guess is we need to fetch CAS for the first write operation and then pass it along with next write. Also we need to update it using result's CAS. Correct me if I am wrong.
推荐答案
CAS实际上代表检查和设置是一种乐观锁定的方法。 CAS值或ID与每个文档相关联,每当文档更改时更新 - 有点像修订ID。意图是,不是悲观地锁定文档(和相关的锁开销),你只是读取它的CAS值,然后只有在CAS匹配时才执行写入。
CAS actually stands for check-and-set, and is a method of optimistic locking. The CAS value or ID is associated with each document which is updated whenever the document changes - a bit like a revision ID. The intent is that instead of pessimistically locking a document (and the associated lock overhead) you just read it's CAS value, and then only perform the write if the CAS matches.
一般用例是:
- 读取现有文档,并获取当前的CAS(
get_with_cas
) - 为该文档准备新值,假设没有其他人修改了文档(因此导致CAS更改)。
- 使用
check_and_set
操作编写文档,提供(1)的CAS值。
- Read an existing document, and obtain it's current CAS (
get_with_cas
) - Prepare a new value for that document, assuming no-one else has modified the document (and hence caused the CAS to change).
- Write the document using the
check_and_set
operation, providing the CAS value from (1).
如果文档在(1)和(3)之间没有变化,步骤3将仅成功(执行写入) - 即没有其他用户在此期间修改它。通常如果(3)失败,您将重试整个序列( get_with_cas
,修改, check_and_set
)。
Step 3 will only succeed (perform the write) if the document is unchanged between (1) and (3) - i.e. no other user has modified it in the meantime. Typically if (3) does fail you would retry the whole sequence (get_with_cas
, modify, check_and_set
).
有关Couchbase开发人员指南中。
There's a much more detailed description of check-and-set in the Couchbase Developer Guide under Check and Set (CAS).
这篇关于什么是CAS在nosql和如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!