Skip to content

Instantly share code, notes, and snippets.

@posulliv
Created September 19, 2009 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posulliv/189520 to your computer and use it in GitHub Desktop.
Save posulliv/189520 to your computer and use it in GitHub Desktop.
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <libmemcached/memcached.hpp>
class MyCache
{
public:
static const uint32_t num_of_clients= 10;
static MyCache &singleton()
{
static MyCache instance;
return instance;
}
void set(const std::string &key,
const std::vector<char> &value)
{
time_t expiry= 0;
uint32_t flags= 0;
getCache()->set(key, value, expiry, flags);
}
std::vector<char> get(const std::string &key)
{
std::vector<char> ret_value;
getCache()->get(key, ret_value);
return ret_value;
}
void remove(const std::string &key)
{
getCache()->remove(key);
}
memcache::Memcache *getCache()
{
/*
* pick a random element from the vector of clients.
* Obviously, this is not very random but suffices as
* an example!
*/
uint32_t index= rand() % num_of_clients;
return clients[index];
}
private:
/*
* A vector of clients.
*/
std::vector<memcache::Memcache *> clients;
MyCache()
:
clients()
{
/* create clients and add them to the vector */
for (uint32_t i= 0; i < num_of_clients; i++)
{
memcache::Memcache *client=
new memcache::Memcache("127.0.0.1:11211");
clients.push_back(client);
}
}
~MyCache()
{
for_each(clients.begin(), clients.end(), DeletePtrs());
clients.clear();
}
MyCache(const MyCache&);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment