Thanks Lee for your reply. My code directly contact with memcached..
is there any restriction of key size?

following is my code..

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <string.h>
#include <stdlib.h>
//#include <err.h>
//#include <event.h>
//#include <evhttp.h>
//#include <memcached.h>
#include <libmemcached/memcached.h>
#include<iostream.h>

typedef struct {
    char* host;
    int port;
} MServer;
int NUM_OF_SERVERS = 0;
MServer memcache_servers[4];
memcached_st *tcp_client;

void add_mserver(char* host, int port) {
    memcache_servers[NUM_OF_SERVERS].host=(char*)malloc(strlen(host)
+1);

    strcpy(memcache_servers[NUM_OF_SERVERS].host, host);
    memcache_servers[NUM_OF_SERVERS].port = port;
    NUM_OF_SERVERS++;
}

void init_memcache_servers() {
    add_mserver("localhost", 11211);
    add_mserver("10.232.165.157", 11211);

    //Add to memcached client
    tcp_client = memcached_create(NULL);

    int i;
    for(i=0; i < NUM_OF_SERVERS; i++) {
        memcached_server_add(tcp_client,
                             memcache_servers[i].host,
                             memcache_servers[i].port);
    }
}

void memcache_handler(char * key, char * value)
{
 memcached_return rc;
 char *cached=NULL;
 size_t string_length;
 uint32_t flags;
 uint32_t hash_pre, hash_post;
 char sVal[1024]={0};

 cached = memcached_get(tcp_client, key, strlen(key),
          &string_length, &flags, &rc);
 if(!cached){
   cout<<"This string is not cached"<<endl;
   rc = memcached_set(tcp_client, key, strlen(key), value,
strlen(value),
//   rc = memcached_add(tcp_client, key, strlen(key), value,
strlen(value),
      (time_t)0, (uint32_t)0);
   if ( rc != MEMCACHED_SUCCESS ) {
      cout << " failed in setting value in memcache."<<endl;
   }
   else {
    cout <<"set error value :"<<rc<<endl;
/*
    hash_pre = memcached_hash_key(value, strlen(value));
    cout<<"Hash Pre :"<<hash_pre<<endl;
*/

    cached = memcached_get(tcp_client, key, strlen(key),
      &string_length, &flags, &rc);
    if(cached)
       cout<<"Successfully retriewed: "<<cached;
    else
    {
       cout<<"return code :"<<rc<<endl;
       cout<<"failed in retriewing value"<<endl<<endl;
    }

   }
 }else{
  cout<<"This string is cached";
 }
}

int main(int argc, char **argv) {

 cout << " TEst program"<<endl;
 init_memcache_servers();

 char sKey[256]={0};
 char sVal[1024]={0};

 while(1)
 {
  cout<<"Are u want to cache an element[yes/no]:";
  cin>>sVal;
  if(strcmp(sVal, "yes"))
    break;

  cout<<"Enter Key : ";
  cin>>sKey;
  cout<<"Enter Value :";
  cin>>sVal;

  memcache_handler(sKey,sVal);
 }
/*
 memcache_handler("prashu","chanda");
 memcache_handler("prashu","chanda");
 memcache_handler("prashu001","chanda");

 memcache_handler("prashu002","chanda");
 memcache_handler("prashu001","chanda");
*/
 return 0;
}

Reply via email to