diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-23 05:25:45 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-23 05:25:45 +0000 |
commit | e5d978186780787e8dad6681cca139486df93643 (patch) | |
tree | 85a837c0e230de4144743f090b2fe714cbb29fec /sca-cpp/trunk/modules/cache/memcached.hpp | |
parent | 585f81b9436e137c3ed784d9a91efa9e6e792e03 (diff) |
Refactored memcached support into an SCA component. Moved cache monad functions to kernel.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883250 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.hpp (renamed from sca-cpp/trunk/modules/cache/memcached.hpp) | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/sca-cpp/trunk/modules/cache/memcached.hpp b/sca-cpp/trunk/components/cache/mcache.hpp index 28bdc0fc89..aae298d80d 100644 --- a/sca-cpp/trunk/modules/cache/memcached.hpp +++ b/sca-cpp/trunk/components/cache/mcache.hpp @@ -19,8 +19,8 @@ /* $Rev$ $Date$ */ -#ifndef tuscany_memcached_hpp -#define tuscany_memcached_hpp +#ifndef tuscany_mcache_hpp +#define tuscany_mcache_hpp /** * Memcached access functions. @@ -40,55 +40,60 @@ #include "monad.hpp" namespace tuscany { -namespace memcached { +namespace cache { /** * Represents a memcached context. */ -class Cache { +class MemCached { public: - Cache() { + MemCached() { apr_pool_create(&pool, NULL); apr_memcache_create(pool, 1, 0, &mc); - } - ~Cache() { - apr_pool_destroy(pool); + init("localhost", 11211); } - operator apr_memcache_t*() const { - return mc; + MemCached(const std::string host, const int port) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + init(host, port); } - operator apr_pool_t*() const { - return pool; + ~MemCached() { + apr_pool_destroy(pool); } private: apr_pool_t* pool; apr_memcache_t* mc; -}; + friend const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache); + friend const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache); + friend const failable<value, std::string> get(const value& key, const MemCached& cache); + friend const failable<bool, std::string> del(const value& key, const MemCached& cache); + + /** + * Initialize the memcached context. + */ + 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); + if (sc != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not create server"); + const apr_status_t as = apr_memcache_add_server(mc, server); + if (as != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not add server"); + return true; + } -/** - * Add a server to the memcached context. - */ -const failable<bool, std::string> addServer(const std::string& host, const int port, const Cache& cache) { - apr_memcache_server_t *server; - const apr_status_t sc = apr_memcache_server_create(cache, host.c_str(), 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(cache, server); - if (as != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add server"); - return true; -} +}; /** * Post a new item to the cache. */ -const failable<bool, std::string> post(const value& key, const value& val, const Cache& 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, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + 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); if (rc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not add entry"); return true; @@ -97,9 +102,9 @@ const failable<bool, std::string> post(const value& key, const value& val, const /** * Update an item in the cache. */ -const failable<bool, std::string> put(const value& key, const value& val, const Cache& cache) { +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, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + 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); if (rc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not add entry"); return true; @@ -108,15 +113,15 @@ 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 Cache& cache) { +const failable<value, std::string> get(const value& key, const MemCached& cache) { apr_pool_t* vpool; - const apr_status_t pc = apr_pool_create(&vpool, cache); + const apr_status_t pc = apr_pool_create(&vpool, cache.pool); if (pc != APR_SUCCESS) return mkfailure<value, std::string>("Could not allocate memory"); char *data; apr_size_t size; - const apr_status_t rc = apr_memcache_getp(cache, cache, std::string(key).c_str(), &data, &size, NULL); + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, std::string(key).c_str(), &data, &size, NULL); if (rc != APR_SUCCESS) { apr_pool_destroy(vpool); return mkfailure<value, std::string>("Could not get entry"); @@ -130,8 +135,8 @@ const failable<value, std::string> get(const value& key, const Cache& cache) { /** * Delete an item from the cache */ -const failable<bool, std::string> del(const value& key, const Cache& cache) { - const apr_status_t rc = apr_memcache_delete(cache, std::string(key).c_str(), 0); +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); if (rc != APR_SUCCESS) return mkfailure<bool, std::string>("Could not add entry"); return true; @@ -140,4 +145,4 @@ const failable<bool, std::string> del(const value& key, const Cache& cache) { } } -#endif /* tuscany_memcached_hpp */ +#endif /* tuscany_mcache_hpp */ |