-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexpiration.cc
128 lines (103 loc) · 3.55 KB
/
expiration.cc
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <libcouchbase/couchbase.h>
#include <libcouchbase/api3.h>
#ifdef _WIN32
#include <windows.h>
#define sleep(s) Sleep(s / 1000)
#else
#include <unistd.h>
#endif
static void op_callback(lcb_t, int cbtype, const lcb_RESPBASE *resp)
{
if (resp->rc == LCB_SUCCESS) {
printf("Operation (type=%d) OK\n", cbtype);
} else {
printf("Operation (type=%d) Failed (%s)\n", cbtype, lcb_strerror(NULL, resp->rc));
}
if (cbtype == LCB_CALLBACK_GET) {
const lcb_RESPGET *rg = reinterpret_cast< const lcb_RESPGET * >(resp);
if (resp->rc == LCB_SUCCESS) {
printf("Got value %.*s\n", (int)rg->nvalue, (char *)rg->value);
}
}
}
static void store_key(lcb_t instance, const char *key, const char *value, unsigned exp = 0)
{
lcb_CMDSTORE scmd = {0};
LCB_CMD_SET_KEY(&scmd, key, strlen(key));
LCB_CMD_SET_VALUE(&scmd, value, strlen(value));
scmd.exptime = exp; // Only live for 2 seconds!
scmd.operation = LCB_SET;
lcb_sched_enter(instance);
lcb_store3(instance, NULL, &scmd);
lcb_sched_leave(instance);
lcb_wait(instance);
}
static void get_or_touch(lcb_t instance, const char *key, unsigned exp = 0, bool get = false)
{
union {
lcb_CMDBASE base;
lcb_CMDGET get;
lcb_CMDTOUCH touch;
} u;
memset(&u, 0, sizeof u);
LCB_CMD_SET_KEY(&u.base, key, strlen(key));
u.base.exptime = exp;
lcb_sched_enter(instance);
if (get) {
lcb_get3(instance, NULL, &u.get);
} else {
lcb_touch3(instance, NULL, &u.touch);
}
lcb_sched_leave(instance);
lcb_wait(instance);
}
int main(int, char **)
{
lcb_t instance;
lcb_create_st crst = {};
lcb_error_t rc;
crst.version = 3;
crst.v.v3.connstr = "couchbase://127.0.0.1/default";
crst.v.v3.username = "testuser";
crst.v.v3.passwd = "password";
rc = lcb_create(&instance, &crst);
rc = lcb_connect(instance);
lcb_wait(instance);
rc = lcb_get_bootstrap_status(instance);
if (rc != LCB_SUCCESS) {
printf("Unable to bootstrap cluster: %s\n", lcb_strerror_short(rc));
exit(1);
}
// You can actually use the same callback for multiple types of operations.
// The second parameter is the type of callback being invoked. You can
// then cast the return type to the more specific struct.
lcb_install_callback3(instance, LCB_CALLBACK_DEFAULT, op_callback);
const char *key = "docid";
const char *value = "{\"some\":\"value\"}";
// First store with an expiry of 1 second
printf("Storing with an expiration of 2\n");
store_key(instance, key, value, 2);
printf("Getting key immediately after store..\n");
get_or_touch(instance, key, 0, true);
printf("Sleeping for 4 seconds..\n");
sleep(4);
printf("Getting key again (should fail)\n");
get_or_touch(instance, key);
printf("Storing key again (without expiry)\n");
store_key(instance, key, value);
printf("Using get-and-touch to retrieve key and modify expiry\n");
get_or_touch(instance, key, 1, true);
printf("Sleeping for another 4 seconds\n");
sleep(4);
printf("Getting key again (should fail)\n");
get_or_touch(instance, key, 0, true);
printf("Storing key again (without expiry)\n");
store_key(instance, key, value);
printf("Touching key (without get). Setting expiry for 1 second\n");
get_or_touch(instance, key, 1, false);
printf("Sleeping for 4 seconds\n");
sleep(4);
printf("Getting again... (should fail)\n");
get_or_touch(instance, key, 0, true);
lcb_destroy(instance);
}