diff options
Diffstat (limited to 'sca-cpp/trunk/components/cache/mcache.hpp')
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.hpp | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/sca-cpp/trunk/components/cache/mcache.hpp b/sca-cpp/trunk/components/cache/mcache.hpp index aae298d80d..d1fe182946 100644 --- a/sca-cpp/trunk/components/cache/mcache.hpp +++ b/sca-cpp/trunk/components/cache/mcache.hpp @@ -35,9 +35,12 @@ #include "apr_network_io.h" #include <string> +#include <sstream> #include "list.hpp" #include "value.hpp" #include "monad.hpp" +#include "debug.hpp" +#include "../../modules/eval/eval.hpp" namespace tuscany { namespace cache { @@ -77,7 +80,7 @@ private: */ const failable<bool, std::string> init(const std::string& host, const int port) { apr_memcache_server_t *server; - const apr_status_t sc = apr_memcache_server_create(pool, host.c_str(), port, 0, 1, 1, 60, &server); + const apr_status_t sc = apr_memcache_server_create(pool, host.c_str(), (apr_port_t)port, 0, 1, 1, 60, &server); if (sc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not create server"); const apr_status_t as = apr_memcache_add_server(mc, server); @@ -92,21 +95,33 @@ private: * Post a new item to the cache. */ const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache) { - const std::string v(val); - const apr_status_t rc = apr_memcache_add(cache.mc, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + debug(key, "cache::post::key"); + debug(val, "cache::post::value"); + + const std::string ks(eval::writeValue(key)); + const std::string vs(eval::writeValue(val)); + const apr_status_t rc = apr_memcache_add(cache.mc, ks.c_str(), const_cast<char*>(vs.c_str()), vs.size(), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not add entry"); + + debug(true, "cache::post::result"); return true; } /** - * Update an item in the cache. + * Update an item in the cache. If the item doesn't exist it is added. */ const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache) { - const std::string v(val); - const apr_status_t rc = apr_memcache_replace(cache.mc, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + debug(key, "cache::put::key"); + debug(val, "cache::put::value"); + + const std::string ks(eval::writeValue(key)); + const std::string vs(eval::writeValue(val)); + const apr_status_t rc = apr_memcache_set(cache.mc, ks.c_str(), const_cast<char*>(vs.c_str()), vs.size(), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not add entry"); + + debug(true, "cache::put::result"); return true; } @@ -114,6 +129,10 @@ const failable<bool, std::string> put(const value& key, const value& val, const * Get an item from the cache. */ const failable<value, std::string> get(const value& key, const MemCached& cache) { + debug(key, "cache::get::key"); + + const std::string ks(eval::writeValue(key)); + apr_pool_t* vpool; const apr_status_t pc = apr_pool_create(&vpool, cache.pool); if (pc != APR_SUCCESS) @@ -121,14 +140,16 @@ const failable<value, std::string> 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, std::string(key).c_str(), &data, &size, NULL); + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, ks.c_str(), &data, &size, NULL); if (rc != APR_SUCCESS) { apr_pool_destroy(vpool); return mkfailure<value, std::string>("Could not get entry"); } - const value val(std::string(data, size)); + const value val(eval::readValue(std::string(data, size))); apr_pool_destroy(vpool); + + debug(val, "cache::get::result"); return val; } @@ -136,9 +157,15 @@ const failable<value, std::string> get(const value& key, const MemCached& cache) * Delete an item from the cache */ const failable<bool, std::string> del(const value& key, const MemCached& cache) { - const apr_status_t rc = apr_memcache_delete(cache.mc, std::string(key).c_str(), 0); + debug(key, "cache::delete::key"); + + std::ostringstream kos; + kos << key; + const apr_status_t rc = apr_memcache_delete(cache.mc, kos.str().c_str(), 0); if (rc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add entry"); + return mkfailure<bool, std::string>("Could not delete entry"); + + debug(true, "cache::delete::result"); return true; } |