Skip to content

Instantly share code, notes, and snippets.

@nkallen
Created November 11, 2008 17:58
Show Gist options
  • Save nkallen/23915 to your computer and use it in GitHub Desktop.
Save nkallen/23915 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pthread.h>
#include "config.h"
#include "memcached.h"
void make_thread_local_connection_key();
void memcached_destroy(void *connection);
void make_prototypical_connection();
void initialize_process();
static memcached_st *prototypical_connection = NULL;
static pthread_key_t thread_local_connection_key;
static pthread_once_t is_initialized = PTHREAD_ONCE_INIT;
memcached_st *twitter_connect() {
int return_code;
pthread_once(&is_initialized, initialize_process);
memcached_st *connection = (memcached_st *)pthread_getspecific(thread_local_connection_key);
if (!connection) {
connection = memcached_clone(NULL, prototypical_connection);
if (return_code = pthread_setspecific(thread_local_connection_key, (void *)connection)) {
fprintf(stderr, "failed to set thread specific key: %i.\n", return_code);
exit(1);
}
}
return connection;
}
void memcached_destroy(void *connection) {
memcached_free((memcached_st *)connection);
}
void initialize_process() {
make_thread_local_connection_key();
make_prototypical_connection();
}
void make_thread_local_connection_key() {
int return_code;
if (return_code = pthread_key_create(&thread_local_connection_key, memcached_destroy)) {
fprintf(stderr, "failed to create thread local key: %i.\n", return_code);
exit(1);
}
}
void make_prototypical_connection() {
memcached_server_st *servers = NULL;
memcached_return return_code;
struct memcached_config *config = read_memcached_config("/usr/local/etc/varnish/memcached.yml");
prototypical_connection = memcached_create(NULL);
if (prototypical_connection == NULL) {
fprintf(stderr, "failed to allocate memcache object.\n");
exit(1);
}
if (memcached_behavior_set(prototypical_connection, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1) != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set ketama behavior.\n");
exit(1);
}
if (memcached_behavior_set(prototypical_connection, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5) != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set md5 key hashing.\n");
exit(1);
}
if (memcached_behavior_set(prototypical_connection, MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY, 1) != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set always-prefix.\n");
exit(1);
}
if (memcached_callback_set(prototypical_connection, MEMCACHED_CALLBACK_PREFIX_KEY, config->prefix) != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set key prefix.\n");
exit(1);
}
return_code = memcached_server_push(prototypical_connection, memcached_servers_parse(config->server_list));
if (return_code == MEMCACHED_SUCCESS) {
fprintf(stderr, "Added servers (%s) successfully\n", config->server_list);
} else {
fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(prototypical_connection, return_code));
}
memcached_config_free(config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment