forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expiration.py
49 lines (39 loc) · 1.21 KB
/
expiration.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from __future__ import print_function
from time import sleep
from couchbase.bucket import Bucket
from couchbase.exceptions import NotFoundError
cb = Bucket('couchbase://10.0.0.31/default')
print('Storing with an expiration of 2 seconds')
cb.upsert('docid', {'some': 'value'}, ttl=2)
print('Getting item back immediately')
print(cb.get('docid').value)
print('Sleeping for 4 seconds...')
sleep(4)
print('Getting key again...')
try:
cb.get('docid')
except NotFoundError:
print('Get failed because item has expired')
print('Storing item again (without expiry)')
cb.upsert('docid', {'some': 'value'})
print('Using get-and-touch to retrieve key and modify expiry')
rv = cb.get('docid', ttl=2)
print('Value is:', rv.value)
print('Sleeping for 4 seconds again')
sleep(4)
print('Getting key again (should fail)')
try:
cb.get('docid')
except NotFoundError:
print('Failed (not found)')
print('Storing key again...')
cb.upsert('docid', {'some': 'value'})
print('Using touch (without get). Setting expiry for 1 second')
cb.touch('docid', ttl=1)
print('Sleeping for 4 seconds...')
sleep(4)
print('Will try to get item again...')
try:
cb.get('docid')
except NotFoundError:
print('Get failed because key has expired')