From 9e52793370e243c647f6df181402a827e1948c67 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Wed, 28 Jul 2010 09:50:21 +0000 Subject: Change sample to use a pool of three memcached servers. Add a property to the memcached component to list multiple memcached servers. Replace spaces by tabs in memcached keys as spaces are not allowed in keys. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@980010 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/cache/memcache-test.cpp | 4 +-- sca-cpp/trunk/components/cache/memcache.composite | 1 + sca-cpp/trunk/components/cache/memcache.cpp | 3 +- sca-cpp/trunk/components/cache/memcache.hpp | 41 +++++++++++++++++++---- sca-cpp/trunk/components/cache/memcached-start | 6 +++- sca-cpp/trunk/components/cache/memcached-stop | 6 +++- sca-cpp/trunk/components/cache/memcached-test | 8 +++-- sca-cpp/trunk/components/cache/server-test | 8 +++-- 8 files changed, 61 insertions(+), 16 deletions(-) (limited to 'sca-cpp/trunk/components') diff --git a/sca-cpp/trunk/components/cache/memcache-test.cpp b/sca-cpp/trunk/components/cache/memcache-test.cpp index 04c4b7d347..289037a179 100644 --- a/sca-cpp/trunk/components/cache/memcache-test.cpp +++ b/sca-cpp/trunk/components/cache/memcache-test.cpp @@ -33,7 +33,7 @@ namespace tuscany { namespace memcache { bool testMemCached() { - MemCached ch("localhost", 11211); + MemCached ch(mklist("localhost:11211", "localhost:11212", "localhost:11213")); const value k = mklist("a"); assert(hasContent(post(k, string("AAA"), ch))); @@ -59,7 +59,7 @@ struct getLoop { bool testGetPerf() { const value k = mklist("c"); - MemCached ch("localhost", 11211); + MemCached ch(mklist("localhost:11211", "localhost:11212", "localhost:11213")); assert(hasContent(post(k, string("CCC"), ch))); const lambda gl = getLoop(k, ch); diff --git a/sca-cpp/trunk/components/cache/memcache.composite b/sca-cpp/trunk/components/cache/memcache.composite index 654df6abbf..7c160172f0 100644 --- a/sca-cpp/trunk/components/cache/memcache.composite +++ b/sca-cpp/trunk/components/cache/memcache.composite @@ -27,6 +27,7 @@ + localhost,localhost:11212,localhost:11213 diff --git a/sca-cpp/trunk/components/cache/memcache.cpp b/sca-cpp/trunk/components/cache/memcache.cpp index 246b6137b3..ec61ae9a92 100644 --- a/sca-cpp/trunk/components/cache/memcache.cpp +++ b/sca-cpp/trunk/components/cache/memcache.cpp @@ -112,7 +112,8 @@ private: */ const failable start(unused const list& params) { // Connect to memcached - memcache::MemCached& ch = *(new (gc_new()) memcache::MemCached("localhost", 11211)); + const value servers = ((lambda)>)car(params))(list()); + memcache::MemCached& ch = *(new (gc_new()) memcache::MemCached(tokenize(",", servers))); // Return the component implementation lambda function return value(lambda&)>(applyCache(ch))); diff --git a/sca-cpp/trunk/components/cache/memcache.hpp b/sca-cpp/trunk/components/cache/memcache.hpp index d410b90767..213120891f 100644 --- a/sca-cpp/trunk/components/cache/memcache.hpp +++ b/sca-cpp/trunk/components/cache/memcache.hpp @@ -54,7 +54,13 @@ public: MemCached(const string host, const int port) : owner(true) { apr_pool_create(&pool, NULL); apr_memcache_create(pool, 1, 0, &mc); - init(host, port); + addServer(host, port); + } + + MemCached(const list& servers) : owner(true) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + addServers(servers); } MemCached(const MemCached& c) : owner(false) { @@ -79,9 +85,9 @@ private: friend const failable del(const value& key, const MemCached& cache); /** - * Initialize the memcached context. + * Add servers to the memcached context. */ - const failable init(const string& host, const int port) { + const failable addServer(const string& host, const int port) { apr_memcache_server_t *server; const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); if (sc != APR_SUCCESS) @@ -91,8 +97,29 @@ private: return mkfailure("Could not add server"); return true; } + + const failable addServers(const list& servers) { + if (isNil(servers)) + return true; + const list toks = tokenize(":", car(servers)); + const failable r = addServer(car(toks), isNil(cdr(toks))? 11211 : atoi(c_str(cadr(toks)))); + if (!hasContent(r)) + return r; + return addServers(cdr(servers)); + } }; +/** + * Replace spaces by tabs (as spaces are not allowed in memcached keys). + */ +const char* nospaces(const char* s) { + char* c = const_cast(s); + for (; *c; c++) + if (*c == ' ') + *c = '\t'; + return s; +} + /** * Post a new item to the cache. */ @@ -102,7 +129,7 @@ const failable post(const value& key, const value& val, const MemCached& c const string ks(scheme::writeValue(key)); const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_add(cache.mc, nospaces(c_str(ks)), const_cast(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure("Could not add entry"); @@ -119,7 +146,7 @@ const failable put(const value& key, const value& val, const MemCached& ca const string ks(scheme::writeValue(key)); const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_set(cache.mc, nospaces(c_str(ks)), const_cast(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure("Could not set entry"); @@ -141,7 +168,7 @@ const failable get(const value& key, const MemCached& cache) { char *data; apr_size_t size; - const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, nospaces(c_str(ks)), &data, &size, NULL); if (rc != APR_SUCCESS) { apr_pool_destroy(vpool); return mkfailure("Could not get entry"); @@ -161,7 +188,7 @@ const failable del(const value& key, const MemCached& cache) { debug(key, "memcache::delete::key"); const string ks(scheme::writeValue(key)); - const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); + const apr_status_t rc = apr_memcache_delete(cache.mc, nospaces(c_str(ks)), 0); if (rc != APR_SUCCESS) return mkfailure("Could not delete entry"); diff --git a/sca-cpp/trunk/components/cache/memcached-start b/sca-cpp/trunk/components/cache/memcached-start index b10d7f3fe8..a3cecc29bf 100755 --- a/sca-cpp/trunk/components/cache/memcached-start +++ b/sca-cpp/trunk/components/cache/memcached-start @@ -19,7 +19,11 @@ # Start memcached here=`readlink -f $0`; here=`dirname $here` +port=$1 +if [ "$port" = "" ]; then + port="11211" +fi memcached_prefix=`cat $here/memcached.prefix` -$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211 +$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p $port diff --git a/sca-cpp/trunk/components/cache/memcached-stop b/sca-cpp/trunk/components/cache/memcached-stop index 80801cdfbf..6522e1d36e 100755 --- a/sca-cpp/trunk/components/cache/memcached-stop +++ b/sca-cpp/trunk/components/cache/memcached-stop @@ -19,8 +19,12 @@ # Stop memcached here=`readlink -f $0`; here=`dirname $here` +port=$1 +if [ "$port" = "" ]; then + port="11211" +fi memcached_prefix=`cat $here/memcached.prefix` -mc="$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211" +mc="$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p $port" kill `ps -ef | grep -v grep | grep "${mc}" | awk '{ print $2 }'` diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test index 842e8d2030..993d63ba1b 100755 --- a/sca-cpp/trunk/components/cache/memcached-test +++ b/sca-cpp/trunk/components/cache/memcached-test @@ -18,7 +18,9 @@ # under the License. # Setup -./memcached-start +./memcached-start 11211 +./memcached-start 11212 +./memcached-start 11213 sleep 1 # Test @@ -26,5 +28,7 @@ sleep 1 rc=$? # Cleanup -./memcached-stop +./memcached-stop 11211 +./memcached-stop 11212 +./memcached-stop 11213 return $rc diff --git a/sca-cpp/trunk/components/cache/server-test b/sca-cpp/trunk/components/cache/server-test index 5c86b75ebc..598d4bce5b 100755 --- a/sca-cpp/trunk/components/cache/server-test +++ b/sca-cpp/trunk/components/cache/server-test @@ -26,7 +26,9 @@ SCAContribution `pwd`/ SCAComposite memcache.composite EOF -./memcached-start +./memcached-start 11211 +./memcached-start 11212 +./memcached-start 11213 ../../modules/http/httpd-start tmp sleep 2 @@ -36,6 +38,8 @@ rc=$? # Cleanup ../../modules/http/httpd-stop tmp -./memcached-stop +./memcached-stop 11211 +./memcached-stop 11212 +./memcached-stop 11213 sleep 2 return $rc -- cgit v1.2.3