[asterisk-commits] snuffy: branch snuffy/func_memcache r229746 - in /team/snuffy/func_memcache: ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Nov 12 13:18:30 CST 2009


Author: snuffy
Date: Thu Nov 12 13:18:27 2009
New Revision: 229746

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=229746
Log:
Memcache GO NOW

Added:
    team/snuffy/func_memcache/configs/func_memcache.conf.sample   (with props)
    team/snuffy/func_memcache/funcs/func_memcache.c   (with props)

Added: team/snuffy/func_memcache/configs/func_memcache.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/snuffy/func_memcache/configs/func_memcache.conf.sample?view=auto&rev=229746
==============================================================================
--- team/snuffy/func_memcache/configs/func_memcache.conf.sample (added)
+++ team/snuffy/func_memcache/configs/func_memcache.conf.sample Thu Nov 12 13:18:27 2009
@@ -1,0 +1,34 @@
+[general]
+; Default Transport Protocol, either tcp or udp
+;transportprotocol=tcp
+;
+; Default Memcache Protocol is text
+; Doing multiple gets, or high usage you may see improvement using binary protocol.
+; binaryprotocol=no
+;
+; Hash Method, only consistant for now
+; distrubitehash=consistant
+; 
+;
+;
+;
+; Mirrored Cache
+; Allows for copying values to two servers.
+; Replicas are only of use if you use the mirror option.
+; replicas=2
+;[mirror]
+;server=localhost:11211
+;mirrorserver=localhost:11212
+;binaryprotocol=yes
+;replicas=2
+;
+;
+
+[testmem]
+server=127.0.0.1:21000
+binaryprotocol=yes
+
+[manycaches]
+server=abc.123.com:13333
+server=def.456.com
+server=ghi.789.com

Propchange: team/snuffy/func_memcache/configs/func_memcache.conf.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/snuffy/func_memcache/configs/func_memcache.conf.sample
------------------------------------------------------------------------------
    svn:executable = *

Propchange: team/snuffy/func_memcache/configs/func_memcache.conf.sample
------------------------------------------------------------------------------
    svn:keywords = snuffy 11/12/2009 1 226648

Propchange: team/snuffy/func_memcache/configs/func_memcache.conf.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/snuffy/func_memcache/funcs/func_memcache.c
URL: http://svnview.digium.com/svn/asterisk/team/snuffy/func_memcache/funcs/func_memcache.c?view=auto&rev=229746
==============================================================================
--- team/snuffy/func_memcache/funcs/func_memcache.c (added)
+++ team/snuffy/func_memcache/funcs/func_memcache.c Thu Nov 12 13:18:27 2009
@@ -1,0 +1,667 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2005-2006, Russell Bryant <russelb at clemson.edu> 
+ *
+ * func_db.c adapted from the old app_db.c, copyright by the following people 
+ * Copyright (C) 2005, Mark Spencer <markster at digium.com>
+ * Copyright (C) 2003, Jefferson Noxon <jeff at debian.org>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Functions for interaction with the Asterisk database
+ *
+ * \author Russell Bryant <russelb at clemson.edu>
+ *
+ * \ingroup functions
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 212000 $")
+
+#include <regex.h>
+#include <libmemcached/memcached.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/config.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+#include "asterisk/cli.h"
+#include "asterisk/astobj2.h"
+
+/*** DOCUMENTATION
+	<function name="MEMCACHE" language="en_US">
+		<synopsis>
+			Read from or write to the Asterisk database.
+		</synopsis>
+		<syntax argsep=",">
+			<parameter name="server" required="true" />
+			<parameter name="key" required="true" />
+			<parameter name="expiry" required="false">
+		   		<para>Available only for sets</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>This function will read from or write a value to the memcache server.  On a
+			read, this function returns the corresponding value from the memcache, or blank
+			if it does not exist.  Reading a memcache value will also set the variables
+			MEMCACHE_RESULT.  If you wish to find out if an entry exists, use the MEMCACHE_EXISTS
+			function.</para>
+		</description>
+		<see-also>
+			<ref type="function">MEMCACHE_DELETE</ref>
+			<ref type="function">MEMCACHE_EXISTS</ref>
+		</see-also>
+	</function>
+	<function name="MEMCACHE_EXISTS" language="en_US">
+		<synopsis>
+			Check to see if a key exists on a memcache server.
+		</synopsis>
+		<syntax argsep=",">
+			<parameter name="server" required="true" />
+			<parameter name="key" required="true" />
+		</syntax>
+		<description>
+			<para>This function will check to see if a key exists a memcache server. 
+			If it exists, the function will return <literal>1</literal>. If not,
+			it will return <literal>0</literal>.  Checking for existence of a key will
+			also set the variable MEMCACHE_RESULT to the key's value if it exists.</para>
+		</description>
+		<see-also>
+			<ref type="function">MEMCACHE</ref>
+		</see-also>
+	</function>
+	<function name="MEMCACHE_DELETE" language="en_US">
+		<synopsis>
+			Return a value from the memcache and deletes it.
+		</synopsis>
+		<syntax argsep=",">
+			<parameter name="server" required="true" />
+			<parameter name="key" required="true" />
+			<parameter name="expiry" required="false" />
+		</syntax>
+		<description>
+			<para>This function will retrieve a value from the memcache server
+			and then remove that key from the memcache. <variable>MEMCACHE_RESULT</variable>
+			will be set to the key's value if it exists.</para>
+		</description>
+		<see-also>
+			<ref type="function">MEMCACHE</ref>
+		</see-also>
+	</function>
+ ***/
+static char *config = "func_memcache.conf";
+
+struct memcache_con {
+	char name[80];
+	int protocol; /* 0= udp 1=tcp*/
+	int transport; /* 0=text 1=bin */
+	int mirror;
+	unsigned int flags; /* bitmasking instead? */
+	char hash; /* consistant (cluster) or other */
+	memcached_st *conn;
+	memcached_server_st *servers;
+	memcached_return rc;
+	AST_RWLIST_ENTRY(memcache_con) list;
+	AST_LIST_HEAD(_serv_list, memc_servers) serv_list;
+};	
+
+struct memc_servers {
+	char name[80];
+	int port;
+	AST_LIST_ENTRY(memc_servers) list;
+};
+
+static AST_RWLIST_HEAD_STATIC(memcaches, memcache_con);
+
+#define MEMC_DEF_PORT 11211
+/* 
+static int fn_memcache_mget(const char *family, const char *key, char *buf, size_t len)
+{
+	struct memcache_con *memc;
+	char *value;
+
+	value = memcached_get(memc->conn, key, sizeof(key), &len, 0, &memc->rc);
+	ast_copy_string(buf, value, len);
+
+	//Free memory from libmemcached library
+	free(value);
+
+  memcached_return rc;
+  char *keys[]= {"fudge", "son", "food"};
+  size_t key_length[]= {5, 3, 4};
+  char return_key[MEMCACHED_MAX_KEY];
+  size_t return_key_length;
+  char *return_value;
+  size_t return_value_length;
+
+  rc= memcached_mget(memc, keys, key_length, 3);
+
+  x= 0;
+  while ((return_value= memcached_fetch(memc, return_key, &return_key_length, 
+                                        &return_value_length, &flags, &rc)))
+  {
+    free(return_value);
+    x++;
+  }
+
+	return 0;
+}
+*/
+
+static int fn_memcache_get(char *key, const char *server, char *buf, size_t len)
+{
+	struct memcache_con *memc;
+	char *value;
+	size_t ss,k_len;
+	uint32_t flags;
+
+	k_len = strlen(key);	
+
+	AST_RWLIST_RDLOCK(&memcaches);
+	AST_RWLIST_TRAVERSE(&memcaches, memc, list) {
+		if (!strcmp(memc->name, server)) {
+			value = memcached_get(memc->conn, key, k_len, &ss, &flags, &memc->rc);
+			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, 
+						memcached_strerror(memc->conn, memc->rc));
+			//ast_log(LOG_WARNING, "key:%s ks:%d ksz:%d\n", key, strlen(key), sizeof(key));
+		}
+	}
+	
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	if(value) {
+		ast_copy_string(buf, value, len);
+		//Free memory from libmemcached library
+		ast_free(value);
+	}
+
+	return 0;
+}
+static int fn_memcache_put(const char *key, const char *server, const char *expiry, const char *value)
+{
+	struct memcache_con *memc;
+
+	AST_RWLIST_RDLOCK(&memcaches);
+	AST_RWLIST_TRAVERSE(&memcaches, memc, list) {
+		if (!strcmp(memc->name, server)) {
+			if (expiry) {
+				long seconds;
+				seconds = atol(expiry);
+				memc->rc = memcached_set(memc->conn, key, strlen(key), value, sizeof(value), (time_t)seconds, (uint32_t)0);
+			} else {
+				memc->rc = memcached_set(memc->conn, key, strlen(key), value, sizeof(value), (time_t)0, (uint32_t)0);
+			}
+			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, memcached_strerror(memc->conn,memc->rc));
+		}
+	}
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	return 0;
+}
+
+static int fn_memcache_del(const char *key, const char *server, const char *expiry)
+{
+	struct memcache_con *memc;
+
+	AST_RWLIST_RDLOCK(&memcaches);
+	AST_RWLIST_TRAVERSE(&memcaches, memc, list) {
+			if (!strcmp(memc->name, server)) {
+					if (expiry) {
+							long seconds;
+							seconds = atol(expiry);
+							memc->rc = memcached_delete(memc->conn, key, strlen(key), 
+														(time_t)seconds);
+					} else {
+							memc->rc = memcached_delete(memc->conn, key, strlen(key), 
+														(time_t)0);
+					}
+					ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, 
+							memcached_strerror(memc->conn,memc->rc));
+			}
+	}
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	return 0;
+}
+
+static int function_memcache_read(struct ast_channel *chan, const char *cmd,
+			    char *parse, char *buf, size_t len)
+{
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(server);
+		AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "MEMCACHE requires an argument, MEMCACHE(<server>,<key>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "MEMCACHE requires an argument, MEMCACHE(<server>,<key>)\n");
+		return -1;
+	}
+
+	if (fn_memcache_get(args.key, args.server, buf, len)) {
+		ast_debug(1, "MEMCACHE: %s not found in cache.\n", args.key);
+		strcpy(buf,"0");
+	} else {
+		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
+		strcpy(buf,"1");
+	}
+	return 0;
+}
+
+static int function_memcache_write(struct ast_channel *chan, const char *cmd, char *parse,
+			     const char *value)
+{
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(server);
+		AST_APP_ARG(key);
+		AST_APP_ARG(expiry);
+	);
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "MEMCACHE requires an argument, MEMCACHED(key[,expiry[,server]])=value\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "MEMCACHE requires an argument, MEMCACHED(key[,expiry])=value\n");
+		return -1;
+	}
+
+	if (strlen(args.key) > MEMCACHED_MAX_KEY) {
+		ast_log(LOG_WARNING, "MEMCACHE key limit is %d characters\n", MEMCACHED_MAX_KEY);
+		return -1;
+	}	
+	if (args.expiry && args.expiry < 0) {
+		ast_log(LOG_WARNING, "MEMCACHE expiry must be 0 for infinite or >0\n");
+		return -1;
+	}	
+	if (fn_memcache_put(args.key, args.server, args.expiry, value)) {
+		ast_log(LOG_WARNING, "MEMCACHE: Error writing %s to server.\n", value);
+		return -1;
+	}
+	return 0;
+}
+
+static int function_memcache_exists(struct ast_channel *chan, const char *cmd,
+			    char *parse, char *buf, size_t len)
+{
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(server);
+		AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "MEMCACHE_EXISTS requires an argument, MEMCACHE_EXISTS(<server>,<key>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "MEMCACHE_EXISTS requires an argument, MEMCACHE_EXISTS(<server>,<key>)\n");
+		return -1;
+	}
+
+	if (fn_memcache_get(args.key, args.server, buf, len)) {
+		ast_debug(1, "MEMCACHE: %s not found in cache.\n", args.key);
+		strcpy(buf,"0");
+	} else {
+		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
+		strcpy(buf,"1");
+	}
+	return 0;
+}
+
+static int function_memcache_delete(struct ast_channel *chan, const char *cmd,
+			      char *parse, char *buf, size_t len)
+{
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(server);
+		AST_APP_ARG(key);
+		AST_APP_ARG(expiry);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>)\n");
+		return -1;
+	}
+	if (args.expiry && args.expiry < 0) {
+		ast_log(LOG_WARNING, "MEMCACHE expiry must be 0 for infinite or >0\n");
+		return -1;
+	}	
+
+	if (fn_memcache_get(args.key, args.server, buf, len)) {
+		ast_debug(1, "MEMCACHE_DELETE: %s/%s not found in database.\n", 
+				args.server, args.key);
+	} else {
+		if (fn_memcache_del(args.key, args.server, args.expiry)) {
+			ast_debug(1, "MEMCACHE_DELETE: %s/%s could not be deleted from the database\n", args.server, args.key);
+		}
+	}
+
+	pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
+
+	return 0;
+}
+
+
+static struct ast_custom_function memcache_function = {
+	.name = "MEMCACHE",
+	.read = function_memcache_read,
+	.write = function_memcache_write,
+};
+
+static struct ast_custom_function memcache_exists_function = {
+	.name = "MEMCACHE_EXISTS",
+	.read = function_memcache_exists,
+	.read_max = 2,
+};
+
+static struct ast_custom_function memcache_delete_function = {
+	.name = "MEMCACHE_DELETE",
+	.read = function_memcache_delete,
+};
+
+static int memc_create_client(char *cat, struct ast_variable *var, int debug)
+{
+	struct memcache_con *client = NULL;
+		
+	client = ast_calloc(1, sizeof(*client));
+	if (!client) {
+		ast_log(LOG_ERROR, "Out of memory!\n");
+		return 1;
+	}
+
+	client->protocol = 0;
+	client->transport = 0;
+
+	ast_copy_string(client->name, cat, sizeof(client->name));
+
+	while (var) {
+		if (!strcasecmp(var->name, "server")) {
+			/* Split server string name:port */
+			char *server = ast_strdupa(var->value);
+			char *cport = strchr(server, ':');
+			struct memc_servers *serv;
+			serv = ast_calloc(1, sizeof(*serv));
+			
+			if (cport) {
+				*cport++ = '\0';
+			}
+			
+			if (ast_strlen_zero(cport)) {
+				ast_log(LOG_WARNING,"Port was not greater than 0, using default port\n");
+			} else {
+				serv->port = atoi(cport);
+			}
+
+			ast_copy_string(serv->name, server, sizeof(serv->name));
+			AST_LIST_INSERT_HEAD(&client->serv_list, serv, list);
+
+		} else if (!strcasecmp(var->name, "transportprotocol")) {
+			if(!strcasecmp(var->value, "udp")) {
+				client->transport = 1;
+			}
+		} else if (!strcasecmp(var->name, "binaryprotocol")) {
+			if(!strcasecmp(var->value, "yes")) {
+				client->protocol = 1;
+			}
+		} else if (!strcasecmp(var->name, "distributehash")) {
+			client->hash=1;/* Fixed HASH ATM */
+			/*ast_copy_string(client->hash, var->value, sizeof(client->hash));*/
+		} else if (!strcasecmp(var->name, "type")) {
+			if (!strcmp(var->value, "mirror")) 
+				client->mirror = 1;	
+		} else {
+			ast_log(LOG_WARNING, "Unknown configuration option: %s\n", var->name);
+		}
+
+		var = var->next;
+	}	
+
+	client->conn = memcached_create(NULL);
+
+	//memcached_behavior_set(memcached_st *ptr, memcached_behavior flag, uint64_t data);
+	//MEMCACHED_BEHAVIOR_USE_UDP , MEMCACHED_BEHAVIOR_NO_BLOCK,  
+	//MEMCACHED_DISTRIBUTION_MODULA, MEMCACHED_DISTRIBUTION_CONSISTENT
+	//MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS = n
+
+	if(client->transport) {
+		memcached_behavior_set(client->conn, MEMCACHED_BEHAVIOR_USE_UDP, 1);
+	}
+
+	if(client->protocol) {
+		memcached_behavior_set(client->conn, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+		ast_log(LOG_WARNING,"Bin Proto Active: %d\n", client->protocol);
+	}
+
+	if(client->hash) {
+		memcached_behavior_set(client->conn, MEMCACHED_DISTRIBUTION_CONSISTENT, 1);
+	}
+
+	if (client->mirror) {
+		struct memc_servers *serv;
+		int first = 1;
+		int no_serv = 0;
+
+		AST_LIST_TRAVERSE(&client->serv_list, serv, list) {
+			if (first) {
+				client->servers = memcached_server_list_append(NULL, 
+					serv->name, serv->port, &client->rc);
+				first = 0;
+			} else {
+				client->servers = memcached_server_list_append(client->servers, 
+					serv->name, serv->port, &client->rc);
+			}
+			no_serv++;
+		}
+		memcached_behavior_set(client->conn, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, no_serv);
+		ast_log(LOG_WARNING,"No servers:%d\n", no_serv);
+	} else {
+		struct memc_servers *serv;
+		int first = 1;
+		int no_serv = 0;
+
+		AST_LIST_TRAVERSE(&client->serv_list, serv, list) {
+			if (first) {
+				client->servers = memcached_server_list_append(NULL, 
+					serv->name, serv->port, &client->rc);
+				first = 0;
+			} else {
+				client->servers = memcached_server_list_append(client->servers, 
+					serv->name, serv->port, &client->rc);
+			}
+			no_serv++;
+		}
+		ast_log(LOG_WARNING,"No servers:%d\n", no_serv);
+	}
+
+	client->rc = memcached_server_push(client->conn, client->servers);
+	ast_log(LOG_WARNING,"Push Error: %s\n", memcached_strerror(client->conn, client->rc));
+
+	AST_RWLIST_INSERT_HEAD(&memcaches, client, list);
+
+	return 0;
+}
+
+static char *handle_cli_memc_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct memcache_con *memc;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "memcache";
+		e->usage = "   server\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	ast_cli(a->fd, "Server\t\tIP\t\t\tMirror\t\tLast Access\n");
+
+	AST_RWLIST_RDLOCK(&memcaches);
+	AST_RWLIST_TRAVERSE(&memcaches, memc, list) {
+		struct memc_servers *serv;
+		AST_LIST_TRAVERSE(&memc->serv_list, serv, list) {
+			ast_cli(a->fd, "%s\t\t%s:%d\t\t%s\t\t%s\n", 
+				memc->name, serv->name, serv->port,
+				memc->mirror ? "yes" : "no", 
+				memcached_strerror(memc->conn, memc->rc));
+		}
+	}
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	return NULL;
+}
+
+static struct ast_cli_entry cli_memc_peers = AST_CLI_DEFINE(handle_cli_memc_peers, "Show memcache peers");
+
+static int load_module(void)
+{
+	int res = 0;
+	struct ast_config *cfg = NULL;
+	struct ast_variable *var = NULL;
+	char *cat;
+	int debug;
+	struct ast_flags config_flags = { 0 };
+
+	res |= ast_custom_function_register(&memcache_function);
+	res |= ast_custom_function_register(&memcache_delete_function);
+	res |= ast_custom_function_register(&memcache_exists_function);
+	res |= ast_cli_register(&cli_memc_peers);
+
+	cfg = ast_config_load(config, config_flags);
+
+	if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
+		ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config);
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	AST_RWLIST_WRLOCK(&memcaches);
+
+	cat = ast_category_browse(cfg, NULL);
+	while (cat) {
+		if (strcasecmp(cat, "general")) {
+			var = ast_variable_browse(cfg, cat);
+			memc_create_client(cat, var, debug);
+		}
+		cat = ast_category_browse(cfg, cat);
+	}
+	ast_config_destroy(cfg);
+
+	AST_RWLIST_UNLOCK(&memcaches);
+	return res;
+}
+
+static int unload_module(void)
+{
+	int res = 0;
+	struct memcache_con *client;
+
+	AST_RWLIST_WRLOCK(&memcaches);
+
+	while (!AST_RWLIST_EMPTY(&memcaches)) {
+		client = AST_RWLIST_REMOVE_HEAD(&memcaches, list);
+  		while (!AST_LIST_EMPTY(&client->serv_list)) {
+			struct memc_servers *serv;
+			serv = AST_LIST_REMOVE_HEAD(&client->serv_list, list);
+			ast_free(serv);
+		}
+		memcached_server_free(client->servers);
+		memcached_free(client->conn);
+		ast_free(client);
+	}
+
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	res |= ast_custom_function_unregister(&memcache_function);
+	res |= ast_custom_function_unregister(&memcache_delete_function);
+	res |= ast_custom_function_unregister(&memcache_exists_function);
+	res |= ast_cli_unregister(&cli_memc_peers);
+
+	return res;
+}
+
+static int reload_module(void)
+{
+	struct ast_config *cfg;
+	struct ast_variable *var = NULL;
+	struct memcache_con *oldmem;
+	char *cat;
+	int debug = 0;
+	struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
+
+	cfg = ast_config_load(config, config_flags);
+	if (cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
+		return 0;
+
+	AST_RWLIST_WRLOCK(&memcaches);
+
+	while (!AST_RWLIST_EMPTY(&memcaches)) {
+		oldmem = AST_RWLIST_REMOVE_HEAD(&memcaches, list);
+  		memcached_server_free(oldmem->servers);
+		memcached_free(oldmem->conn);
+	}
+
+	if (!cfg) {
+		ast_log(LOG_WARNING, "Unable to load config for func_memcache: %s\n", config);
+		return -1;
+	}
+	
+	cat = ast_category_browse(cfg, NULL);
+	while (cat) {
+		if (strcasecmp(cat, "general")) {
+			var = ast_variable_browse(cfg, cat);
+			memc_create_client(cat, var, debug);
+		}
+		cat = ast_category_browse(cfg, cat);
+	}
+
+	ast_config_destroy(cfg);
+
+	AST_RWLIST_UNLOCK(&memcaches);
+
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Memcache lookups",
+		.load = load_module,
+		.unload = unload_module,
+		.reload = reload_module,
+);

Propchange: team/snuffy/func_memcache/funcs/func_memcache.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/snuffy/func_memcache/funcs/func_memcache.c
------------------------------------------------------------------------------
    svn:executable = *

Propchange: team/snuffy/func_memcache/funcs/func_memcache.c
------------------------------------------------------------------------------
    svn:keywords = snuffy 11/12/2009 1 226648

Propchange: team/snuffy/func_memcache/funcs/func_memcache.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list