iipsrv
0.9.9
|
00001 // Simple Wrapper to libMemcached 00002 00003 /* IIP Image Server 00004 00005 Copyright (C) 2010 Ruven Pillay. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 00023 00024 #ifndef _MEMCACHED_H 00025 #define _MEMCACHED_H 00026 00027 #include <string> 00028 #include <libmemcached/memcached.h> 00029 00030 00031 00033 00034 class Memcache { 00035 00036 00037 private: 00038 00040 memcached_st *_memc; 00041 00043 memcached_return_t _rc; 00044 00046 memcached_server_st *_servers; 00047 00049 time_t _timeout; 00050 00052 size_t _length; 00053 00055 bool _connected; 00056 00057 00058 public: 00059 00061 00064 Memcache( const std::string& servernames = "localhost", unsigned int timeout = 3600 ) { 00065 00066 // Set our timeout 00067 _timeout = timeout; 00068 00069 // Create our memcached object 00070 _memc = memcached_create(NULL); 00071 00072 // Create a list of servers 00073 _servers = memcached_servers_parse( servernames.c_str() ); 00074 00075 // Try to set some memcached behaviour settings for performance. For example, 00076 // using the binary protocol, non-blocking IO, and no reply for add commands 00077 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1 ); 00078 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1 ); 00079 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1 ); 00080 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NOREPLY, 1 ); 00081 00082 // Connect to the servers 00083 _rc = memcached_server_push( _memc, _servers ); 00084 if(_rc == MEMCACHED_SUCCESS ) _connected = true; 00085 else _connected = false; 00086 00087 if( memcached_server_count(_memc) > 0 ) _connected = true; 00088 else _connected = false; 00089 }; 00090 00091 00093 ~Memcache() { 00094 // Disconnect from our servers and free our memcached structure 00095 if( _servers ) memcached_server_free(_servers); 00096 if( _memc ) memcached_free(_memc); 00097 } 00098 00099 00101 00105 void store( const std::string& key, void* data, unsigned int length ){ 00106 00107 if( !_connected ) return; 00108 00109 std::string k = "iipsrv::" + key; 00110 _rc = memcached_set( _memc, k.c_str(), k.length(), 00111 (char*) data, length, 00112 _timeout, 0 ); 00113 } 00114 00115 00117 00120 char* retrieve( const std::string& key ){ 00121 00122 if( !_connected ) return NULL; 00123 00124 uint32_t flags; 00125 std::string k = "iipsrv::" + key; 00126 return memcached_get( _memc, k.c_str(), k.length(), &_length, &flags, &_rc ); 00127 } 00128 00129 00131 const char* error(){ 00132 return memcached_strerror( _memc, _rc ); 00133 }; 00134 00135 00137 unsigned int length(){ return _length; }; 00138 00139 00141 bool connected(){ return _connected; }; 00142 00143 00144 }; 00145 00146 00147 00148 #endif