OSDN Git Service

* Revoked apr_memcache and used libmemcached.
authorkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 9 May 2008 13:27:11 +0000 (13:27 +0000)
committerkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 9 May 2008 13:27:11 +0000 (13:27 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/branches/RELEASE_0_12_0@2628 1a406e8e-add9-4483-a2c8-d8cac5b7c224

Makefile.in
configure
configure.ac
include/chxj_memcache.h
mkinstalldirs [deleted file]
src/Makefile.am
src/Makefile.in
src/chxj_memcache.c

index c057c12..4539e22 100644 (file)
@@ -37,14 +37,14 @@ DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \
        $(srcdir)/Makefile.in $(top_srcdir)/configure \
        $(top_srcdir)/include/config.h.in AUTHORS COPYING ChangeLog \
        INSTALL NEWS TODO compile config.guess config.sub depcomp \
-       install-sh ltmain.sh missing mkinstalldirs
+       install-sh ltmain.sh missing
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
        $(ACLOCAL_M4)
 am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \
  configure.lineno config.status.lineno
-mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+mkinstalldirs = $(install_sh) -d
 CONFIG_HEADER = $(top_builddir)/include/config.h
 CONFIG_CLEAN_FILES =
 SOURCES =
index eb3050c..742f58f 100755 (executable)
--- a/configure
+++ b/configure
@@ -1508,8 +1508,8 @@ Optional Packages:
   --with-apr-config=ARG  Path of apr-config program file.
   --with-apu-config=ARG  Path of apu-config program file.
   --with-pcre-config=ARG  Path of pcre-config program file.
-  --with-apr-memcache-header Path of "apr_memcache.h"
-  --with-apr-memcache-lib-dir Path of "libapr_memcache.so"
+  --with-memcached-header Path of "memcached.h"
+  --with-memcached-lib-dir Path of "libmemcached.so"
   --with-mysql-header=ARG  Path of "mysql.h"
   --with-mysql-lib-dir=ARG Path of libmysqlclient
 
@@ -19441,38 +19441,38 @@ if test "${enable_memcache_cookie+set}" = set; then
 fi
 
 
-# Check whether --with-apr-memcache-header was given.
-if test "${with_apr_memcache_header+set}" = set; then
-  withval=$with_apr_memcache_header;
+# Check whether --with-memcached-header was given.
+if test "${with_memcached_header+set}" = set; then
+  withval=$with_memcached_header;
 fi
 
 
-# Check whether --with-apr-memcache-lib-dir was given.
-if test "${with_apr_memcache_lib_dir+set}" = set; then
-  withval=$with_apr_memcache_lib_dir;
+# Check whether --with-memcached-lib-dir was given.
+if test "${with_memcached_lib_dir+set}" = set; then
+  withval=$with_memcached_lib_dir;
 fi
 
 if test "x$enable_memcache_cookie" = "xyes" ; then
-        if test "x$with_apr_memcache_header" = "x" -o "x$with_apr_memcache_header" = "xyes" ; then
+        if test "x${with_memcached_header}" = "x" -o "x${with_memcached_header}" = "xyes" ; then
     { { echo "$as_me:$LINENO: error:
-Please specify the --with-apr-memcache-header And, please specify the place of the \"apr_memcache.h\" header file directory.
+Please specify the --with-memcached-header And, please specify the place of the \"memcached.h\" header file directory.
 " >&5
 echo "$as_me: error:
-Please specify the --with-apr-memcache-header And, please specify the place of the \"apr_memcache.h\" header file directory.
+Please specify the --with-memcached-header And, please specify the place of the \"memcached.h\" header file directory.
 " >&2;}
    { (exit 1); exit 1; }; }
   fi
-  if test "x$with_apr_memcache_lib_dir" = "x" -o "x$with_apr_memcache_lib_dir" = "xyes" ; then
+  if test "x${with_memcached_lib_dir}" = "x" -o "x${with_memcached_lib_dir}" = "xyes" ; then
     { { echo "$as_me:$LINENO: error:
-Please specify the --with-apr-memcache-lib-dir And, please specify the place of apr-memcache library directory.
+Please specify the --with-memcached-lib-dir And, please specify the place of libmemcached library directory.
 " >&5
 echo "$as_me: error:
-Please specify the --with-apr-memcache-lib-dir And, please specify the place of apr-memcache library directory.
+Please specify the --with-memcached-lib-dir And, please specify the place of libmemcached library directory.
 " >&2;}
    { (exit 1); exit 1; }; }
   fi
-  MEMCACHE_COOKIE_DEFS="-DUSE_MEMCACHE_COOKIE -I${with_apr_memcache_header}"
-  MEMCACHE_COOKIE_LIBS="-L${with_apr_memcache_lib_dir} -lapr_memcache"
+  MEMCACHE_COOKIE_DEFS="-DUSE_MEMCACHE_COOKIE -I${with_memcached_header} "
+  MEMCACHE_COOKIE_LIBS="-L${with_memcached_lib_dir} -lmemcached "
 else
         MEMCACHE_COOKIE_DEFS=""
   MEMCACHE_COOKIE_LIBS=""
@@ -19942,7 +19942,7 @@ LIBS="${LIBS} `${WAND_CONFIG} --ldflags` `${WAND_CONFIG} --libs | sed -e s/-ldps
 LIBS="${LIBS} `${APR_CONFIG} --ldflags` `${APR_CONFIG} --libs` "
 LIBS="${LIBS} `${APU_CONFIG} --ldflags` `${APU_CONFIG} --libs` "
 LIBS="${LIBS} ${MYSQL_COOKIE_LIBS}"
-LIBS="${LIBS} ${MEMCACHE_COOKIE_LIBS}"
+LIBS="${LIBS} ${MEMCACHE_COOKIE_LIBS} -lmemcache"
 CFLAGS="${CFLAGS} `${WAND_CONFIG} --cppflags` `${WAND_CONFIG} --cflags`"
 CFLAGS="${CFLAGS} `${APR_CONFIG} --includes` `${APR_CONFIG} --cflags` `${APR_CONFIG} --cppflags`"
 CFLAGS="${CFLAGS} `${APU_CONFIG} --includes`"
index 41e5513..d91b23b 100644 (file)
@@ -93,25 +93,25 @@ dnl
 dnl SAVE COOKIE to memcached Mode
 dnl
 dnl ==================================================
-AC_ARG_ENABLE(memcache-cookie, [  --enable-memcache-cookie Please specify it when you save Cookie in memcached. ])
-AC_ARG_WITH(apr-memcache-header,   [  --with-apr-memcache-header Path of "apr_memcache.h"])
-AC_ARG_WITH(apr-memcache-lib-dir,   [  --with-apr-memcache-lib-dir Path of "libapr_memcache.so"])
+AC_ARG_ENABLE(memcache-cookie,   [  --enable-memcache-cookie Please specify it when you save Cookie in memcached. ])
+AC_ARG_WITH(memcached-header,    [  --with-memcached-header Path of "memcached.h"])
+AC_ARG_WITH(memcached-lib-dir,   [  --with-memcached-lib-dir Path of "libmemcached.so"])
 if test "x$enable_memcache_cookie" = "xyes" ; then
   dnl ======================================
   dnl use MEMCACHE COOKIE
   dnl ======================================
-  if test "x$with_apr_memcache_header" = "x" -o "x$with_apr_memcache_header" = "xyes" ; then
+  if test "x${with_memcached_header}" = "x" -o "x${with_memcached_header}" = "xyes" ; then
     AC_MSG_ERROR([
-Please specify the --with-apr-memcache-header And, please specify the place of the "apr_memcache.h" header file directory. 
+Please specify the --with-memcached-header And, please specify the place of the "memcached.h" header file directory. 
 ])
   fi
-  if test "x$with_apr_memcache_lib_dir" = "x" -o "x$with_apr_memcache_lib_dir" = "xyes" ; then
+  if test "x${with_memcached_lib_dir}" = "x" -o "x${with_memcached_lib_dir}" = "xyes" ; then
     AC_MSG_ERROR([
-Please specify the --with-apr-memcache-lib-dir And, please specify the place of apr-memcache library directory. 
+Please specify the --with-memcached-lib-dir And, please specify the place of libmemcached library directory. 
 ])
   fi
-  MEMCACHE_COOKIE_DEFS="-DUSE_MEMCACHE_COOKIE -I${with_apr_memcache_header}"
-  MEMCACHE_COOKIE_LIBS="-L${with_apr_memcache_lib_dir} -lapr_memcache"
+  MEMCACHE_COOKIE_DEFS="-DUSE_MEMCACHE_COOKIE -I${with_memcached_header} "
+  MEMCACHE_COOKIE_LIBS="-L${with_memcached_lib_dir} -lmemcached "
 else
   dnl ======================================
   dnl no use MEMCACHE COOKIE
@@ -240,7 +240,7 @@ LIBS="${LIBS} `${WAND_CONFIG} --ldflags` `${WAND_CONFIG} --libs | sed -e s/-ldps
 LIBS="${LIBS} `${APR_CONFIG} --ldflags` `${APR_CONFIG} --libs` "
 LIBS="${LIBS} `${APU_CONFIG} --ldflags` `${APU_CONFIG} --libs` "
 LIBS="${LIBS} ${MYSQL_COOKIE_LIBS}"
-LIBS="${LIBS} ${MEMCACHE_COOKIE_LIBS}"
+LIBS="${LIBS} ${MEMCACHE_COOKIE_LIBS} -lmemcache"
 CFLAGS="${CFLAGS} `${WAND_CONFIG} --cppflags` `${WAND_CONFIG} --cflags`"
 CFLAGS="${CFLAGS} `${APR_CONFIG} --includes` `${APR_CONFIG} --cflags` `${APR_CONFIG} --cppflags`"
 CFLAGS="${CFLAGS} `${APU_CONFIG} --includes`"
index f1357c6..aeee7af 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "mod_chxj.h"
 #include "apr.h"
-#include "apr_memcache.h"
 
 #define DEFAULT_MEMCACHE_HOST "localhost"
 
@@ -31,7 +30,7 @@ typedef struct {
 
 
 extern int chxj_memcache_init(request_rec *r, mod_chxj_config *m);
-extern int chxj_memcache_and_memcache_server_create(request_rec *r, mod_chxj_config *m, apr_memcache_server_t **memcache_server, apr_memcache_t **memcache);
+extern int chxj_memcache_and_memcache_server_create(request_rec *r, mod_chxj_config *m);
 extern int chxj_memcache_set_cookie(request_rec *r, mod_chxj_config *m, const char *cookie_id, const char *store_string);
 extern char *chxj_memcache_get_cookie(request_rec *r, mod_chxj_config *m, const char *cookie_id);
 extern int chxj_memcache_delete_cookie(request_rec *r, mod_chxj_config *m, const char *cookie_id);
diff --git a/mkinstalldirs b/mkinstalldirs
deleted file mode 100755 (executable)
index 6b3b5fc..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-#! /bin/sh
-# mkinstalldirs --- make directory hierarchy
-# Author: Noah Friedman <friedman@prep.ai.mit.edu>
-# Created: 1993-05-16
-# Public domain
-
-# $Id$
-
-errstatus=0
-
-for file
-do
-   set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
-   shift
-
-   pathcomp=
-   for d
-   do
-     pathcomp="$pathcomp$d"
-     case "$pathcomp" in
-       -* ) pathcomp=./$pathcomp ;;
-     esac
-
-     if test ! -d "$pathcomp"; then
-        echo "mkdir $pathcomp"
-
-        mkdir "$pathcomp" || lasterr=$?
-
-        if test ! -d "$pathcomp"; then
-         errstatus=$lasterr
-        fi
-     fi
-
-     pathcomp="$pathcomp/"
-   done
-done
-
-exit $errstatus
-
-# mkinstalldirs ends here
index 60da428..c49d14f 100644 (file)
@@ -54,7 +54,7 @@ clean:
        rm -f mod_chxj.so
 
 install: make_so
-       @@APXS@ -i -n "chxj" @PACKAGE_NAME@.so
+       @@APXS@ -i -n "chxj" @PACKAGE_NAME@.so 
 
 activate: make_so
        @@APXS@ -i -a -n "chxj" @PACKAGE_NAME@.so
index 7ebae29..3213f8c 100644 (file)
@@ -39,7 +39,7 @@ ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 am__aclocal_m4_deps = $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
        $(ACLOCAL_M4)
-mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+mkinstalldirs = $(install_sh) -d
 CONFIG_HEADER = $(top_builddir)/include/config.h
 CONFIG_CLEAN_FILES =
 am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
@@ -854,7 +854,7 @@ clean:
        rm -f mod_chxj.so
 
 install: make_so
-       @@APXS@ -i -n "chxj" @PACKAGE_NAME@.so
+       @@APXS@ -i -n "chxj" @PACKAGE_NAME@.so 
 
 activate: make_so
        @@APXS@ -i -a -n "chxj" @PACKAGE_NAME@.so
index a8efdc7..27c2058 100644 (file)
 
 #include <unistd.h>
 
-/* for apr-memcache */
-#include "apr_memcache.h"
+/* for memcache */
+#include <memcached.h>
 
 #define MEMCACHE_MIN_CONNECTION (0)
 #define MEMCACHE_SMAX_CONNECTION (1)
 #define MEMCACHE_MAX_CONNECTION (1)
-#define MEMCACHE_TTL_CONNECTION (600)
+#define MEMCACHE_CONNECTION_TIMEOUT (60)
+#define MEMCACHE_POLL_TIMEOUT       (60)
+#define MEMCACHE_RETRY_TIMEOUT      (60)
 
 #define MEMCACHE_LOCK_KEY "chxj::lock"
 #define MEMCACHE_WAIT_MICRO_SECOND (5000)
 #define DEFAULT_COOKIE_DB_NAME "chxj_cookie"
 #define DEFAULT_COOKIE_EXPIRE_DB_NAME "chxj_cookie_expire"
 
-/* The underlying apr_memcache system is thread safe. */
-static apr_memcache_t* mc = NULL;
+/* The underlying memcache system is thread safe. */
+static memcached_st *memc = NULL;
+static memcached_server_st *servers = NULL;
 
 static apr_status_t
-_memcache_cleanup(void *UNUSED(notused))
+_memcache_cleanup(void *UNUSED(unused))
 {
-  mc = NULL;
+  if (servers) {
+    memcached_server_list_free(servers);
+    servers = NULL;
+  }
+  if (memc) {
+    memcached_free(memc);
+    memc = NULL;
+  }
   return APR_SUCCESS;
 }
 
 int
 chxj_memcache_init(request_rec *r, mod_chxj_config *m)
 {
-  apr_memcache_server_t* st;
   DBG(r, "start chxj_memcache_init()");
-  if (! mc) {
-    if (!chxj_memcache_and_memcache_server_create(r, m, &st, &mc)) {
+  if (! memc) {
+    if (!chxj_memcache_and_memcache_server_create(r, m)) {
       ERR(r, "%s:%d end chxj_memcache_init() failed: chxj_memcache_and_memcache_server_create()", APLOG_MARK);
       return CHXJ_FALSE;
     }
-    apr_pool_cleanup_register(r->pool, (void *)NULL, _memcache_cleanup, _memcache_cleanup);
+    apr_pool_cleanup_register(r->pool, NULL, _memcache_cleanup, _memcache_cleanup);
   }
   DBG(r, "end chxj_memcache_init()");
   return CHXJ_TRUE;
 }
 
 int
-chxj_memcache_and_memcache_server_create(request_rec *r, mod_chxj_config *m, apr_memcache_server_t **memcache_server, apr_memcache_t **memcache)
+chxj_memcache_and_memcache_server_create(request_rec *r, mod_chxj_config *m)
 {
+  memcached_return rc;
   DBG(r, "start chxj_memcache_server_create()");
-  if (apr_memcache_server_create(r->pool, 
-                                 m->memcache.host,
-                                 m->memcache.port,
-                                 MEMCACHE_MIN_CONNECTION, 
-                                 MEMCACHE_SMAX_CONNECTION,
-                                 MEMCACHE_MAX_CONNECTION,
-                                 MEMCACHE_TTL_CONNECTION,
-                                 memcache_server) != APR_SUCCESS) {
-    ERR(r, "%s:%d end chxj_memcache_server_create() failed apr_memcache_server_create() host:[%s] port:[%d]", APLOG_MARK, m->memcache.host, m->memcache.port);
+  
+  memc = memcached_create(NULL);
+  if (! memc) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): failed allocation of memcached_st.", APLOG_MARK);
     return CHXJ_FALSE;
   }
-  DBG(r, "done create_server");
-  if (apr_memcache_create(r->pool, MEMCACHE_MAX_SERVER, MEMCACHE_FLAGS, memcache) != APR_SUCCESS) {
-    ERR(r, "%s:%d end chxj_memcache_server_create() failed apr_memcache_create()", APLOG_MARK);
+  servers = memcached_server_list_append(NULL, m->memcache.host, m->memcache.port, &rc);
+  if (servers == NULL || rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): host:[%s] port:[%d]: %s", APLOG_MARK, m->memcache.host, m->memcache.port, memcached_strerror(memc, rc));
     return CHXJ_FALSE;
   }
-  DBG(r, "done create memcache");
-  if (apr_memcache_add_server(*memcache, *memcache_server) != APR_SUCCESS) {
-    ERR(r, "%s:%d end chxj_memcache_server_create() failed apr_memcache_add_server()", APLOG_MARK);
+  rc = memcached_server_push(memc, servers);
+  if (rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): host:[%s] port:[%d]: %s\n", APLOG_MARK, m->memcache.host, m->memcache.port, memcached_strerror(memc, rc));
     return CHXJ_FALSE;
   }
-  DBG(r, "end chxj_memcache_server_create()");
 
+  rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, (uint64_t)MEMCACHE_POLL_TIMEOUT);
+  if (rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): memcached_behavior_set(MEMCACHED_BEHAVIOR_POLL_TIMEOUT): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return CHXJ_FALSE;
+  }
+  rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, (uint64_t)MEMCACHE_CONNECTION_TIMEOUT);
+  if (rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): memcached_behavior_set(MEMCACHED_BEHAVIOR_CONNECTION_TIMEOUT): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return CHXJ_FALSE;
+  }
+  rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, (uint64_t)MEMCACHE_RETRY_TIMEOUT);
+  if (rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_server_create(): memcached_behavior_set(MEMCACHED_BEHAVIOR_RETRY_TIMEOUT): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return CHXJ_FALSE;
+  }
+  
+  DBG(r, "end chxj_memcache_server_create()");
   return CHXJ_TRUE;
 }
 
@@ -113,20 +134,17 @@ chxj_memcache_and_memcache_server_create(request_rec *r, mod_chxj_config *m, apr
 int
 chxj_memcache_set_cookie(request_rec *r, mod_chxj_config *m, const char *cookie_id, const char *store_string)
 {
-  apr_status_t ret;
-  apr_uint32_t timeout = (apr_uint32_t) ((m->cookie_timeout) ? m->cookie_timeout : DEFAULT_COOKIE_TIMEOUT);
+  memcached_return rc;
+  time_t timeout = (time_t) ((m->cookie_timeout) ? m->cookie_timeout : DEFAULT_COOKIE_TIMEOUT);
   DBG(r, "start chxj_memcache_set_cookie()");
 
-  do {
-    if ((ret = apr_memcache_set(mc, cookie_id, (char *)store_string, strlen(store_string), timeout, 0)) != APR_SUCCESS) {
-      if (ret == APR_EAGAIN) {
-        continue;
-      }
-      ERR(r, "%s:%d chxj_memcache_set_cookie() failed: apr_memcache_set() ret:[%d]", APLOG_MARK, ret);
-      return CHXJ_FALSE;
-    }
+  rc= memcached_set(memc, (char *)cookie_id, strlen(cookie_id),
+                    (char *)store_string, strlen(store_string),
+                    (time_t)timeout, (uint32_t)0);
+  if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED) {
+    ERR(r, "%s:%d end chxj_memcache_set_cookie(): failed memcache_set(): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return CHXJ_FALSE;
   }
-  while(0);
 
   DBG(r, "end chxj_memcache_set_cookie()");
   return CHXJ_TRUE;
@@ -139,14 +157,13 @@ chxj_memcache_reset_cookie(request_rec *r, mod_chxj_config *m, const char *cooki
   char *store_string;
   DBG(r, "start chxj_memcache_reset_cookie()");
 
-
   if (! (store_string = chxj_memcache_get_cookie(r, m, cookie_id))) {
-    ERR(r, "%s:%d chxj_memcache_reset_cookie() failed: chxj_memcache_get_cookie() cookie_id:[%s]", APLOG_MARK, cookie_id);
+    ERR(r, "%s:%d end chxj_memcache_reset_cookie() failed: chxj_memcache_get_cookie() cookie_id:[%s]", APLOG_MARK, cookie_id);
     return CHXJ_FALSE;
   }
 
   if (! chxj_memcache_set_cookie(r, m, cookie_id, store_string)) {
-    ERR(r, "%s:%d chxj_memcache_reset_cookie() failed: apr_memcache_set() cookie_id:[%s]", APLOG_MARK, cookie_id);
+    ERR(r, "%s:%d end chxj_memcache_reset_cookie() failed: chxj_memcache_set_cookie() cookie_id:[%s]", APLOG_MARK, cookie_id);
     return CHXJ_FALSE;
   }
 
@@ -159,49 +176,38 @@ char *
 chxj_memcache_get_cookie(request_rec *r, mod_chxj_config *UNUSED(m), const char *cookie_id)
 {
   char *load_string;
-  char *ret_value;
-  apr_size_t len;
-  apr_status_t ret;
+  char *ret;
+  size_t value_length;
+  uint32_t flags;
+  memcached_return rc;
+
   DBG(r, "start chxj_memcache_get_cookie()");
 
-  do {
-    if ((ret = apr_memcache_getp(mc, r->pool, cookie_id, &load_string, &len, 0)) != APR_SUCCESS) {
-      char errstr[255];
-      if (ret == APR_EAGAIN) {
-        continue;
-      }
-      ERR(r, "%s:%d chxj_memcache_get_cookie() failed: apr_memcache_get() cookie_id:[%s] ret:[%d:%s]", APLOG_MARK, 
-             cookie_id, ret, apr_strerror(ret,errstr,255));
-      return NULL;
-    }
+  load_string = memcached_get(memc, (char *)cookie_id, strlen(cookie_id), &value_length, &flags, &rc);
+  if (rc != MEMCACHED_SUCCESS) {
+    ERR(r, "%s:%d end chxj_memcache_get_cookie(): failed memcached_get(): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return NULL;
   }
-  while(0);
+  ret = apr_pstrdup(r->pool, load_string);
+  free(load_string);
   
-  ret_value = apr_palloc(r->pool, len+1);
-  memset(ret_value, 0, len+1);
-  memcpy(ret_value, load_string, len);
-
   DBG(r, "end chxj_memcache_get_cookie()");
-  return ret_value;
+  return ret;
 }
 
 
 int
 chxj_memcache_delete_cookie(request_rec *r, mod_chxj_config *UNUSED(m),  const char *cookie_id)
 {
-  apr_status_t ret;
+  memcached_return rc;
+
   DBG(r, "start chxj_memcache_delete_cookie()");
 
-  do {
-    if ((ret = apr_memcache_delete(mc, cookie_id, 0)) != APR_SUCCESS) {
-      if (ret == APR_EAGAIN) {
-        continue;
-      }
-      ERR(r, "%s:%d chxj_memcache_delete_cookie(): failed: apr_memcache_delete() cookie_id:[%s] ret:[%d]", APLOG_MARK, cookie_id, ret);
-      return CHXJ_FALSE;
-    }
+  rc = memcached_delete(memc, (char *)cookie_id, strlen(cookie_id), (time_t)0);
+  if (rc != MEMCACHED_SUCCESS && rc == MEMCACHED_BUFFERED) {
+    ERR(r,"%s:%d end chxj_memcache_delete_cookie(): failed memcached_delete(): %s", APLOG_MARK, memcached_strerror(memc, rc));
+    return CHXJ_FALSE;
   }
-  while(0);
 
   DBG(r, "end chxj_memcache_delete_cookie()");
   return CHXJ_TRUE;
@@ -213,6 +219,7 @@ int
 chxj_save_cookie_memcache(request_rec *r, mod_chxj_config *m, const char *cookie_id, const char *store_string)
 {
   DBG(r, "start chxj_save_cookie_memcache() cookie_id:[%s]", cookie_id);
+
   if (! chxj_memcache_init(r, m)) {
     ERR(r, "%s:%d end chxj_save_cookie_memcache(): Cannot create memcache server: cookie_id:[%s] store_string:[%s]", APLOG_MARK, cookie_id, store_string);
     return CHXJ_FALSE;
@@ -223,6 +230,7 @@ chxj_save_cookie_memcache(request_rec *r, mod_chxj_config *m, const char *cookie
         APLOG_MARK, m->memcache.host, m->memcache.port, cookie_id, store_string);
     return CHXJ_FALSE;
   }
+
   DBG(r, "stored DATA:[%s]", chxj_memcache_get_cookie(r, m, cookie_id));
   DBG(r, "end chxj_save_cookie_memcache() cookie_id:[%s]", cookie_id);
   return CHXJ_TRUE;
@@ -335,6 +343,7 @@ chxj_cookie_lock_memcache(request_rec *r, mod_chxj_config *m)
   int retry_count = 0;
   apr_uint32_t timeout = (apr_uint32_t) ((m->cookie_timeout) ? m->cookie_timeout : DEFAULT_COOKIE_TIMEOUT);
   apr_interval_time_t wait_time = MEMCACHE_WAIT_MICRO_SECOND;
+
   DBG(r, "start chxj_cookie_lock_memcache()");
 
   if (! chxj_memcache_init(r, m)) {
@@ -344,15 +353,17 @@ chxj_cookie_lock_memcache(request_rec *r, mod_chxj_config *m)
 
   apr_snprintf(baton, sizeof(baton)-1, "dummy");
   while(1) {
-    apr_status_t rv = apr_memcache_add(mc, MEMCACHE_LOCK_KEY, baton, strlen(baton), timeout, 0);
-    if (APR_SUCCESS == rv) {
+    memcached_return rc;
+    rc = memcached_add(memc, MEMCACHE_LOCK_KEY, sizeof(MEMCACHE_LOCK_KEY)-1, baton, strlen(baton), (time_t)timeout, (uint32_t)0);
+    if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED && rc != MEMCACHED_NOTSTORED) {
+      ERR(r, "%s:%d end chxj_cookie_lock_memcache(): failed memcached_add(): %s\n", APLOG_MARK, memcached_strerror(memc, rc));
+      return CHXJ_FALSE;
+    }
+    if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_STORED) {
       /* got lock */
       DBG(r, "got lock");
       break;
     }
-    if (rv == APR_EAGAIN) {
-      continue;
-    }
     retry_count++;
     if (retry_count >= MEMCACHE_LOCK_RETRY_COUNT) {
       ERR(r, "%s:%d end chxj_cookie_lock_memcache(): retry over.",APLOG_MARK);
@@ -367,30 +378,23 @@ chxj_cookie_lock_memcache(request_rec *r, mod_chxj_config *m)
 
 
 int
-chxj_cookie_unlock_memcache(request_rec *r, mod_chxj_config *UNUSED(m))
+chxj_cookie_unlock_memcache(request_rec *r, mod_chxj_config *m)
 {
-  apr_status_t ret;
   DBG(r, "start chxj_cookie_unlock_memcache()");
-  if (! mc) {
+  if (! memc) {
     ERR(r, "%s:%d end chxj_cookie_unlock_memcache(): runtime exception: programing failure.", APLOG_MARK);
     return CHXJ_FALSE;
   }
 
-  while(1) {
-    if ((ret = apr_memcache_delete(mc, MEMCACHE_LOCK_KEY, 0)) != APR_SUCCESS) {
-      char errstr[255];
-      if (ret == APR_EAGAIN) {
-        continue;
-      }
-      ERR(r, "%s:%d end chxj_cookie_unlock_memcache(): failed: apr_memcache_delete() (lock data) ret:[%d:%s]", APLOG_MARK,ret, apr_strerror(ret, errstr, 255));
-      return CHXJ_FALSE;
-    }
-    break;
+  if (! chxj_memcache_delete_cookie(r, m, MEMCACHE_LOCK_KEY)) {
+    ERR(r, "%s:%d end chxj_cookie_unlock_memcache(): failed: chxj_memcache_delete_cookie() (lock data)", APLOG_MARK);
+    return CHXJ_FALSE;
   }
   DBG(r, "end chxj_cookie_unlock_memcache()");
   return CHXJ_TRUE;
 }
 
+
 #endif
 /*
  * vim:ts=2 et