|
| 1 | + |
| 2 | +/* Copyright (c) 2016, Stefan.Eilemann@epfl.ch |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU Lesser General Public License version 2.1 as published |
| 6 | + * by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 11 | + * details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Lesser General Public License |
| 14 | + * along with this library; if not, write to the Free Software Foundation, Inc., |
| 15 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifdef LUNCHBOX_USE_LIBMEMCACHED |
| 19 | +#include <libmemcached/memcached.h> |
| 20 | + |
| 21 | +namespace lunchbox |
| 22 | +{ |
| 23 | +namespace memcached |
| 24 | +{ |
| 25 | +namespace |
| 26 | +{ |
| 27 | +memcached_st* _getInstance( const servus::URI& uri ) |
| 28 | +{ |
| 29 | + const std::string& host = uri.getHost(); |
| 30 | + const int16_t port = uri.getPort() ? uri.getPort() : 11211; |
| 31 | + memcached_st* instance = memcached_create( 0 ); |
| 32 | + |
| 33 | + if( uri.getHost().empty( )) |
| 34 | + { |
| 35 | + const char* servers = ::getenv( "MEMCACHED_SERVERS" ); |
| 36 | + if( servers ) |
| 37 | + { |
| 38 | + instance->server_failure_limit = 10; |
| 39 | + std::string data = servers; |
| 40 | + while( !data.empty( )) |
| 41 | + { |
| 42 | + const size_t comma = data.find( ',' ); |
| 43 | + const std::string& server = data.substr( 0, comma ); |
| 44 | + |
| 45 | + const size_t colon = server.find( ':' ); |
| 46 | + if( colon == std::string::npos ) |
| 47 | + memcached_server_add( instance, server.c_str(), port ); |
| 48 | + else |
| 49 | + memcached_server_add( instance, |
| 50 | + server.substr( 0, colon ).c_str(), |
| 51 | + std::stoi( server.substr( colon+1 ))); |
| 52 | + |
| 53 | + if( comma == std::string::npos ) |
| 54 | + data.clear(); |
| 55 | + else |
| 56 | + data = data.substr( comma + 1 ); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + else |
| 61 | + memcached_server_add( instance, "127.0.0.1", port ); |
| 62 | + |
| 63 | + } |
| 64 | + else |
| 65 | + memcached_server_add( instance, host.c_str(), port ); |
| 66 | + |
| 67 | + return instance; |
| 68 | +} |
| 69 | +} |
| 70 | + |
| 71 | +// memcached has relative strict requirements on keys (no whitespace or control |
| 72 | +// characters, max length). We therefore hash incoming keys and use their string |
| 73 | +// representation. |
| 74 | + |
| 75 | +class PersistentMap : public detail::PersistentMap |
| 76 | +{ |
| 77 | +public: |
| 78 | + explicit PersistentMap( const servus::URI& uri ) |
| 79 | + : _instance( _getInstance( uri )) |
| 80 | + , _lastError( MEMCACHED_SUCCESS ) |
| 81 | + { |
| 82 | + if( !_instance ) |
| 83 | + throw std::runtime_error( std::string( "Open of " ) + |
| 84 | + std::to_string( uri ) + " failed" ); |
| 85 | + } |
| 86 | + |
| 87 | + virtual ~PersistentMap() { memcached_free( _instance ); } |
| 88 | + |
| 89 | + static bool handles( const servus::URI& uri ) |
| 90 | + { return uri.getScheme() == "memcached"; } |
| 91 | + |
| 92 | + bool insert( const std::string& key, const void* data, const size_t size ) |
| 93 | + final |
| 94 | + { |
| 95 | + const std::string& hash = servus::make_uint128( key ).getString(); |
| 96 | + const memcached_return_t ret = |
| 97 | + memcached_set( _instance, hash.c_str(), hash.length(), |
| 98 | + (const char*)data, size, (time_t)0, (uint32_t)0 ); |
| 99 | + |
| 100 | + if( ret != MEMCACHED_SUCCESS && _lastError != ret ) |
| 101 | + { |
| 102 | + LBWARN << "memcached_set failed: " |
| 103 | + << memcached_strerror( _instance, ret ) << std::endl; |
| 104 | + _lastError = ret; |
| 105 | + } |
| 106 | + |
| 107 | + return ret == MEMCACHED_SUCCESS; |
| 108 | + } |
| 109 | + |
| 110 | + std::string operator [] ( const std::string& key ) const final |
| 111 | + { |
| 112 | + const std::string& hash = servus::make_uint128( key ).getString(); |
| 113 | + size_t size = 0; |
| 114 | + uint32_t flags = 0; |
| 115 | + memcached_return_t ret = MEMCACHED_SUCCESS; |
| 116 | + char* data = memcached_get( _instance, hash.c_str(), hash.length(), |
| 117 | + &size, &flags, &ret ); |
| 118 | + std::string value; |
| 119 | + if( ret == MEMCACHED_SUCCESS ) |
| 120 | + { |
| 121 | + value.assign( data, data + size ); |
| 122 | + free( data ); |
| 123 | + } |
| 124 | + return value; |
| 125 | + } |
| 126 | + |
| 127 | + bool flush() final { /*NOP?*/ return true; } |
| 128 | + |
| 129 | +private: |
| 130 | + memcached_st* const _instance; |
| 131 | + memcached_return_t _lastError; |
| 132 | +}; |
| 133 | +} |
| 134 | +} |
| 135 | + |
| 136 | +#endif |
0 commit comments