Hello all,

With help from this group I managed to write this code.
I want to use this in a patch in production , so I dont want to end up
doing something daft.
( I havent any experience with memcached )

--- The basic idea is , If key exists in memcahce increment value..
else create key with value 1

Can someone please review the code and see if this is OK

Compile with
cc -I/usr/include/libmemcached/  /usr/lib/libmemcached.so  memc.c   -o
memc

Run
./memc  testkey

-------------------------
#include <memcached.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

int main(int argc , char* argv[]){
  memcached_st *memc;
  memcached_return_t rc;
  char  *value;
  uint32_t flags;
  size_t value_length;
  uint64_t number_value;
  int n;

  n=strlen(argv[1]);
  printf("Key = %s (%d)\n",argv[1],n);
  memc= memcached_create(NULL);
  rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
  value= memcached_get(memc, argv[1], n, &value_length, &flags, &rc);
  if(value){
    printf("Value = %s\n",value);
    memcached_increment(memc,argv[1],n,1,&number_value);
  } else{
   printf("Value = 0\n");
   memcached_set(memc, argv[1], n, "1", 1, 0, 0);
  }

  free(value);  /* Do I need to free anything else ?*/
  return(0);
}

Reply via email to