iipsrv 1.2
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
Memcached.h
1// Simple Wrapper to libMemcached
2
3/* IIP Image Server
4
5 Copyright (C) 2010-2013 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 3 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 Foundation,
19 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*/
21
22#ifndef _MEMCACHED_H
23#define _MEMCACHED_H
24
25#include <string>
26
27// Need to undefine _WIN32 on Windows to avoid compilation problems with winsock2.h and ws2def.h
28// includes when using libmemcached-awesome
29#ifdef WIN32
30#undef _WIN32
31#endif
32#include <libmemcached/memcached.h>
33
34#ifdef LIBMEMCACHED_VERSION_STRING
35typedef memcached_return memcached_return_t;
36#endif
37
39
40class Memcache {
41
42
43 private:
44
46 memcached_st *_memc;
47
49 memcached_return_t _rc;
50
52 memcached_server_st *_servers;
53
55 time_t _timeout;
56
58 size_t _length;
59
61 bool _connected;
62
63
64 public:
65
67
70 Memcache( const std::string& servernames = "localhost", unsigned int timeout = 3600 ) {
71
72 _length = 0;
73
74 // Set our timeout
75 _timeout = timeout;
76
77 // Create our memcached object
78 _memc = memcached_create(NULL);
79
80 // Create a list of servers
81 _servers = memcached_servers_parse( servernames.c_str() );
82
83 // Try to set some memcached behaviour settings for performance. For example,
84 // using the binary protocol, non-blocking IO, and no reply for add commands
85 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1 );
86 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1 );
87 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1 );
88 _rc = memcached_behavior_set( _memc, MEMCACHED_BEHAVIOR_NOREPLY, 1 );
89
90 // Connect to the servers
91 _rc = memcached_server_push( _memc, _servers );
92 if(_rc == MEMCACHED_SUCCESS ) _connected = true;
93 else _connected = false;
94
95 if( memcached_server_count(_memc) > 0 ) _connected = true;
96 else _connected = false;
97 };
98
99
102 // Disconnect from our servers and free our memcached structure
103 if( _servers ) memcached_server_free(_servers);
104 if( _memc ) memcached_free(_memc);
105 }
106
107
109
113 void store( const std::string& key, void* data, unsigned int length ){
114
115 if( !_connected ) return;
116
117 std::string k = "iipsrv::" + key;
118 _rc = memcached_set( _memc, k.c_str(), k.length(),
119 (char*) data, length,
120 _timeout, 0 );
121 }
122
123
125
128 char* retrieve( const std::string& key ){
129
130 if( !_connected ) return NULL;
131
132 uint32_t flags;
133 std::string k = "iipsrv::" + key;
134 return memcached_get( _memc, k.c_str(), k.length(), &_length, &flags, &_rc );
135 }
136
137
139 const char* error(){
140 return memcached_strerror( _memc, _rc );
141 };
142
143
145 unsigned int length(){ return _length; };
146
147
149 bool connected(){ return _connected; };
150
151
152};
153
154
155
156#endif
Cache to store raw tile data.
Definition: Memcached.h:40
void store(const std::string &key, void *data, unsigned int length)
Insert data into our cache.
Definition: Memcached.h:113
bool connected()
Tell us whether we are connected to any memcached servers.
Definition: Memcached.h:149
char * retrieve(const std::string &key)
Retrieve data from our cache.
Definition: Memcached.h:128
Memcache(const std::string &servernames="localhost", unsigned int timeout=3600)
Constructor.
Definition: Memcached.h:70
const char * error()
Get error string.
Definition: Memcached.h:139
~Memcache()
Destructor.
Definition: Memcached.h:101
unsigned int length()
Return the number of bytes in the result.
Definition: Memcached.h:145