memcache_test.c
changeset 46 8a832c0e01ee
parent 44 03a7e064f833
child 48 1c67f512779b
equal deleted inserted replaced
45:10d514029c64 46:8a832c0e01ee
     7 #include "config.h"
     7 #include "config.h"
     8 #include "common.h"
     8 #include "common.h"
     9 
     9 
    10 static struct memcache *mc;
    10 static struct memcache *mc;
    11 static struct config_endpoint server_endpoint;
    11 static struct config_endpoint server_endpoint;
       
    12 static char *data_1 = "rei4quohV8Oocio1ua0co8ni4Ae1re4houcheixahchoh3ioghie0aShooShoh6Ahboequ9eiX5eashuu6Chu1quo"
       
    13                             "o0suph7cheiyai1ea0ooh7Aevoo4feihubupohDeephahwee2Ooz7chiediec7neit7keTh6xuheash8chaeKa5vi"
       
    14                             "ekooqu7ooj6Eezooroi6Nequ9ca2yi6iSoigh3loowaey9eiphaphaiJ0souy7wohpa9eXo5Ahu2sa";
       
    15 static char *data_2 = "iefaek7ighi5UpueThageish5ieshohyeil1raiceerahjahng5ui7vuzie9quu4dai5ar2aiXi5ieth4looweigi"
       
    16                             "e3fo5ieri1queengaiphuaghaic1xahvoo9joo6baiNaig8puCootheowah4moocohDoiquoh3quieka5ao3aeNg9"
       
    17                             "Aimei1soangu4Duch5pho5buu2ohzaich4chahz9iTh3Pei4beep1ongie6au1aafoosh2vierei5E";
    12 
    18 
    13 int _memcache_cb (struct memcache_req *req, void *arg) {
    19 void _memcache_cb (struct memcache_req *req, void *arg) {
    14     return 0;
    20     char *key = arg;
       
    21     const struct memcache_obj *obj;
       
    22     const struct memcache_buf *buf;
       
    23 
       
    24     INFO("[%s]: cmd=%15s state=%15s reply=%15s", key,
       
    25         memcache_command_str(memcache_req_cmd(req)),
       
    26         memcache_state_str(memcache_req_state(req)),
       
    27         memcache_reply_str(memcache_req_reply(req))
       
    28     );
       
    29     
       
    30     if ((obj = memcache_req_obj(req)))
       
    31         INFO("\tobj: flags=0x%04X exptime=%9zu bytes=%6zu cas=%llu", obj->flags, obj->exptime, obj->bytes, obj->cas);
       
    32 
       
    33     if ((buf = memcache_req_buf(req)))
       
    34         INFO("\tbuf: data=%p len=%6zu offset=%6zu", buf->data, buf->len, buf->offset);
       
    35     
       
    36     INFO("%s", "");
    15 }
    37 }
    16 
    38 
    17 void begin_test () {
    39 void begin_test () {
       
    40     struct memcache_key key_1, key_2;
       
    41     struct memcache_obj obj_1, obj_2;
       
    42     struct memcache_buf buf_1, buf_2;
       
    43 
    18     if ((mc = memcache_alloc(&_memcache_cb)) == NULL)
    44     if ((mc = memcache_alloc(&_memcache_cb)) == NULL)
    19         ERROR("memcache_alloc");
    45         ERROR("memcache_alloc");
    20     
    46     
    21     // fix up the endpoint
    47     // fix up the endpoint
    22     endpoint_init(&server_endpoint, 11211);
    48     endpoint_init(&server_endpoint, 11211);
    27     // add the server
    53     // add the server
    28     if (memcache_add_server(mc, &server_endpoint, 1))
    54     if (memcache_add_server(mc, &server_endpoint, 1))
    29         ERROR("memcache_add_server");
    55         ERROR("memcache_add_server");
    30    
    56    
    31     // add a request or two
    57     // add a request or two
       
    58     key_1.buf = "memcache_test_k1";
       
    59     key_2.buf = "memcache_test_k2";
       
    60     key_1.len = key_2.len = 0;
       
    61 
       
    62     obj_1.flags = 0x1A;
       
    63     obj_2.flags = 0x2B;
       
    64 
       
    65     obj_1.exptime = 0;
       
    66     obj_2.exptime = 3600;
       
    67 
       
    68     obj_1.bytes = strlen(data_1);
       
    69     obj_2.bytes = strlen(data_2);
       
    70 
       
    71     buf_1.data = data_1;
       
    72     buf_1.len = strlen(data_1);
       
    73     buf_1.offset = buf_1.len;
       
    74 
       
    75     buf_2.data = data_2;
       
    76     buf_2.len = strlen(data_2);
       
    77     buf_2.offset = buf_2.len;
       
    78 
       
    79     if (memcache_store(mc, MEMCACHE_CMD_STORE_SET, &key_1, &obj_1, &buf_1, key_1.buf))
       
    80         ERROR("memcache_store: key_1");
    32     
    81     
       
    82     if (memcache_store(mc, MEMCACHE_CMD_STORE_ADD, &key_2, &obj_2, &buf_2, key_2.buf))
       
    83         ERROR("memcache_store: key_2");
    33     
    84     
       
    85     if (memcache_fetch(mc, &key_1, key_1.buf))
       
    86         ERROR("memcache_fetch: key_1");
       
    87     
       
    88     if (memcache_fetch(mc, &key_2, key_2.buf))
       
    89         ERROR("memcache_fetch: key_2");
    34 
    90 
    35 error:
    91 error:
    36     return;
    92     return;
    37 }
    93 }
    38 
    94