Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created August 25, 2024 07:57
Show Gist options
  • Save mortymacs/69d839262d5fdbbe26f6f83c42b3307a to your computer and use it in GitHub Desktop.
Save mortymacs/69d839262d5fdbbe26f6f83c42b3307a to your computer and use it in GitHub Desktop.
memcached sample C code
//compile: gcc a.c -lmemcached
#include <libmemcached/memcached.h>
#include <stdio.h>
#include <string.h>
int main()
{
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char *key = "product999";
char *value = "999";
char *fetch_value;
size_t value_length;
uint32_t flags;
memc = memcached_create(NULL);
servers = memcached_server_list_append(servers, "localhost", 11211, &rc);
rc = memcached_server_push(memc, servers);
if(rc == MEMCACHED_SUCCESS)
{
fprintf(stderr, "Added server successfully!\n");
}
fetch_value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);
if(rc == MEMCACHED_SUCCESS)
{
printf("The key %s value is %s", key, fetch_value);
free(fetch_value);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment