Memcached.h
// Simple Wrapper to libMemcached
/* IIP Image Server
Copyright (C) 2010-2013 Ruven Pillay.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _MEMCACHED_H
#define _MEMCACHED_H
#include <string>
#include <libmemcached/memcached.h>
#ifdef LIBMEMCACHED_VERSION_STRING
typedef memcached_return memcached_return_t;
#endif
class Memcache {
private:
memcached_st *_memc;
memcached_return_t _rc;
memcached_server_st *_servers;
time_t _timeout;
size_t _length;
bool _connected;
public:
Memcache( const std::string& servernames = "localhost", unsigned int timeout = 3600 ) {
_length = 0;
// Set our timeout
_timeout = timeout;
// Create our memcached object
_memc = memcached_create(NULL);
// Create a list of servers
_servers = memcached_servers_parse( servernames.c_str() );
// Try to set some memcached behaviour settings for performance. For example,
// using the binary protocol, non-blocking IO, and no reply for add commands
_rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1 );
_rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1 );
_rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1 );
_rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NOREPLY, 1 );
// Connect to the servers
_rc = memcached_server_push( _memc, _servers );
if(_rc == MEMCACHED_SUCCESS ) _connected = true;
else _connected = false;
if( memcached_server_count(_memc) > 0 ) _connected = true;
else _connected = false;
};
~Memcache() {
// Disconnect from our servers and free our memcached structure
if( _servers ) memcached_server_free(_servers);
if( _memc ) memcached_free(_memc);
}
void store( const std::string& key, void* data, unsigned int length ){
if( !_connected ) return;
std::string k = "iipsrv::" + key;
_rc = memcached_set( _memc, k.c_str(), k.length(),
(char*) data, length,
_timeout, 0 );
}
char* retrieve( const std::string& key ){
if( !_connected ) return NULL;
uint32_t flags;
std::string k = "iipsrv::" + key;
return memcached_get( _memc, k.c_str(), k.length(), &_length, &flags, &_rc );
}
const char* error(){
return memcached_strerror( _memc, _rc );
};
unsigned int length(){ return _length; };
bool connected(){ return _connected; };
};
#endif
Memcache::store
void store(const std::string &key, void *data, unsigned int length)
Insert data into our cache.
Definition Memcached.h:109
Memcache::connected
bool connected()
Tell us whether we are connected to any memcached servers.
Definition Memcached.h:145
Memcache::retrieve
char * retrieve(const std::string &key)
Retrieve data from our cache.
Definition Memcached.h:124
Memcache::Memcache
Memcache(const std::string &servernames="localhost", unsigned int timeout=3600)
Constructor.
Definition Memcached.h:66
Memcache::error
const char * error()
Get error string.
Definition Memcached.h:135
Memcache::~Memcache
~Memcache()
Destructor.
Definition Memcached.h:97
Memcache::length
unsigned int length()
Return the number of bytes in the result.
Definition Memcached.h:141