-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path07_cas.py
35 lines (28 loc) · 922 Bytes
/
07_cas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
from couchbase import Couchbase
from couchbase.exceptions import CouchbaseError
cb = Couchbase.connect(bucket='default')
udata = {
'doctype': 'learn',
'username': 'jsmith',
'name': 'John Smith',
'email': '[email protected]',
'password': 'p4ssw0rd',
'logins': 0
}
rv = cb.set(udata['email'], udata)
print "Stored initial item with cas", rv.cas
rv = cb.get(udata['email'])
print "Retrieved item with cas", rv.cas
udata = rv.value
first_cas = rv.cas
print "Will update the document to generate a new CAS"
udata['logins'] += 1
rv = cb.replace(udata['email'], udata, cas=rv.cas)
print "Updated CAS is now", rv.cas
print "Will try a new operation with previous CAS"
try:
rv = cb.replace(udata['email'], udata, cas=first_cas)
except CouchbaseError as e:
print "Got error while using stale CAS:", e
print "Current login count is now", cb.get(udata['email']).value['logins']