184
To illustrate how libvmemcache is used, Listing 10-12 shows how to create an
instance of vmemcache using default values. This example uses a temporary file on a
DAX-enabled file system and shows how a callback is registered after a cache miss for a
key “meow.”
Listing 10-12. vmemcache.c: An example program using libvmemcache
37 #include
38 #include
39 #include
40 #include
41
42 #define STR_AND_LEN(x) (x), strlen(x)
43
44 VMEMcache *cache;
45
46 void on_miss(VMEMcache *cache, const void *key,
47
size_t key_size, void *arg)
48 {
49
vmemcache_put(cache, STR_AND_LEN("meow"),
50
STR_AND_LEN("Cthulhu fthagn"));
51 }
52
53 void get(const char *key)
54 {
55
char buf[128];
56
ssize_t len = vmemcache_get(cache,
57
STR_AND_LEN(key), buf, sizeof(buf), 0, NULL);
58
if (len >= 0)
59
printf("%.*s\n", (int)len, buf);
60
else
61
printf("(key not found: %s)\n", key);
62 }
63
64 int main()
65 {
Chapter 10 Volatile Use of persistent MeMory
To illustrate how libvmemcache is used, Listing 10-12 shows how to create an
instance of vmemcache using default values. This example uses a temporary file on a
DAX-enabled file system and shows how a callback is registered after a cache miss for a
key “meow.”
Listing 10-12. vmemcache.c: An example program using libvmemcache
37 #include
38 #include
39 #include
40 #include
41
42 #define STR_AND_LEN(x) (x), strlen(x)
43
44 VMEMcache *cache;
45
46 void on_miss(VMEMcache *cache, const void *key,
47
size_t key_size, void *arg)
48 {
49
vmemcache_put(cache, STR_AND_LEN("meow"),
50
STR_AND_LEN("Cthulhu fthagn"));
51 }
52
53 void get(const char *key)
54 {
55
char buf[128];
56
ssize_t len = vmemcache_get(cache,
57
STR_AND_LEN(key), buf, sizeof(buf), 0, NULL);
58
if (len >= 0)
59
printf("%.*s\n", (int)len, buf);
60
else
61
printf("(key not found: %s)\n", key);
62 }
63
64 int main()
65 {
Chapter 10 Volatile Use of persistent MeMory
