diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 09:50:21 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 09:50:21 +0000 |
commit | 9e52793370e243c647f6df181402a827e1948c67 (patch) | |
tree | b45fc568df85769e4281a01b84f45bf141d4dabb /sca-cpp/trunk/components | |
parent | fe93d86e5572870b2e4004c7788da8320a28de3d (diff) |
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
Diffstat (limited to 'sca-cpp/trunk/components')
-rw-r--r-- | sca-cpp/trunk/components/cache/memcache-test.cpp | 4 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/memcache.composite | 1 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/memcache.cpp | 3 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/memcache.hpp | 41 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-start | 6 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-stop | 6 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-test | 8 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/server-test | 8 |
8 files changed, 61 insertions, 16 deletions
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<string>("localhost:11211", "localhost:11212", "localhost:11213")); const value k = mklist<value>("a"); assert(hasContent(post(k, string("AAA"), ch))); @@ -59,7 +59,7 @@ struct getLoop { bool testGetPerf() { const value k = mklist<value>("c"); - MemCached ch("localhost", 11211); + MemCached ch(mklist<string>("localhost:11211", "localhost:11212", "localhost:11213")); assert(hasContent(post(k, string("CCC"), ch))); const lambda<bool()> 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 @@ <service name="memcache"> <t:binding.http uri="memcache"/> </service> + <property name="servers">localhost,localhost:11212,localhost:11213</property> </component> </composite> 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<value> start(unused const list<value>& params) { // Connect to memcached - memcache::MemCached& ch = *(new (gc_new<memcache::MemCached>()) memcache::MemCached("localhost", 11211)); + const value servers = ((lambda<value(list<value>)>)car(params))(list<value>()); + memcache::MemCached& ch = *(new (gc_new<memcache::MemCached>()) memcache::MemCached(tokenize(",", servers))); // Return the component implementation lambda function return value(lambda<value(const list<value>&)>(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<string>& 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<bool> del(const value& key, const MemCached& cache); /** - * Initialize the memcached context. + * Add servers to the memcached context. */ - const failable<bool> init(const string& host, const int port) { + const failable<bool> 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,9 +97,30 @@ private: return mkfailure<bool>("Could not add server"); return true; } + + const failable<bool> addServers(const list<string>& servers) { + if (isNil(servers)) + return true; + const list<string> toks = tokenize(":", car(servers)); + const failable<bool> 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<char*>(s); + for (; *c; c++) + if (*c == ' ') + *c = '\t'; + return s; +} + +/** * Post a new item to the cache. */ const failable<bool> post(const value& key, const value& val, const MemCached& cache) { @@ -102,7 +129,7 @@ const failable<bool> 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<char*>(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_add(cache.mc, nospaces(c_str(ks)), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool>("Could not add entry"); @@ -119,7 +146,7 @@ const failable<bool> 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<char*>(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_set(cache.mc, nospaces(c_str(ks)), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool>("Could not set entry"); @@ -141,7 +168,7 @@ const failable<value> 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<value>("Could not get entry"); @@ -161,7 +188,7 @@ const failable<bool> 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<bool>("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 |