iipsrv  0.9.9
Memcached.h
1 // Simple Wrapper to libMemcached
2 
3 /* IIP Image Server
4 
5  Copyright (C) 2010 Ruven Pillay.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 
23 
24 #ifndef _MEMCACHED_H
25 #define _MEMCACHED_H
26 
27 #include <string>
28 #include <libmemcached/memcached.h>
29 
30 
31 
33 
34 class Memcache {
35 
36 
37  private:
38 
40  memcached_st *_memc;
41 
43  memcached_return_t _rc;
44 
46  memcached_server_st *_servers;
47 
49  time_t _timeout;
50 
52  size_t _length;
53 
55  bool _connected;
56 
57 
58  public:
59 
61 
64  Memcache( const std::string& servernames = "localhost", unsigned int timeout = 3600 ) {
65 
66  // Set our timeout
67  _timeout = timeout;
68 
69  // Create our memcached object
70  _memc = memcached_create(NULL);
71 
72  // Create a list of servers
73  _servers = memcached_servers_parse( servernames.c_str() );
74 
75  // Try to set some memcached behaviour settings for performance. For example,
76  // using the binary protocol, non-blocking IO, and no reply for add commands
77  _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1 );
78  _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1 );
79  _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1 );
80  _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NOREPLY, 1 );
81 
82  // Connect to the servers
83  _rc = memcached_server_push( _memc, _servers );
84  if(_rc == MEMCACHED_SUCCESS ) _connected = true;
85  else _connected = false;
86 
87  if( memcached_server_count(_memc) > 0 ) _connected = true;
88  else _connected = false;
89  };
90 
91 
94  // Disconnect from our servers and free our memcached structure
95  if( _servers ) memcached_server_free(_servers);
96  if( _memc ) memcached_free(_memc);
97  }
98 
99 
101 
105  void store( const std::string& key, void* data, unsigned int length ){
106 
107  if( !_connected ) return;
108 
109  std::string k = "iipsrv::" + key;
110  _rc = memcached_set( _memc, k.c_str(), k.length(),
111  (char*) data, length,
112  _timeout, 0 );
113  }
114 
115 
117 
120  char* retrieve( const std::string& key ){
121 
122  if( !_connected ) return NULL;
123 
124  uint32_t flags;
125  std::string k = "iipsrv::" + key;
126  return memcached_get( _memc, k.c_str(), k.length(), &_length, &flags, &_rc );
127  }
128 
129 
131  const char* error(){
132  return memcached_strerror( _memc, _rc );
133  };
134 
135 
137  unsigned int length(){ return _length; };
138 
139 
141  bool connected(){ return _connected; };
142 
143 
144 };
145 
146 
147 
148 #endif
char * retrieve(const std::string &key)
Retrieve data from our cache.
Definition: Memcached.h:120
Memcache(const std::string &servernames="localhost", unsigned int timeout=3600)
Constructor.
Definition: Memcached.h:64
Cache to store raw tile data.
Definition: Memcached.h:34
void store(const std::string &key, void *data, unsigned int length)
Insert data into our cache.
Definition: Memcached.h:105
const char * error()
Get error string.
Definition: Memcached.h:131
bool connected()
Tell us whether we are connected to any memcached servers.
Definition: Memcached.h:141
unsigned int length()
Return the number of bytes in the result.
Definition: Memcached.h:137
~Memcache()
Destructor.
Definition: Memcached.h:93