[sr-dev] git:master: memcached: add new parameter 'stringify', patch from Federico Cabiddu

Henning Westerholt hw at kamailio.org
Thu Jan 23 13:17:14 CET 2014


Module: sip-router
Branch: master
Commit: b9b83d251810f70545c72c2207b2864460572872
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b9b83d251810f70545c72c2207b2864460572872

Author: Henning Westerholt <hw at kamailio.org>
Committer: Henning Westerholt <hw at kamailio.org>
Date:   Thu Jan 23 13:14:02 2014 +0100

memcached: add new parameter 'stringify', patch from Federico Cabiddu
* add a new parameter 'stringify' to the memcached module to force the module
  to evaluate all returned values as strings. This is useful if other parts
  of the memcached infrastructure are not able to set the correct value flags
* patch from Federico Cabiddu, federico dot cabiddu at gmail dot com
* add documentation for the new parameter

---

 modules/memcached/README                  |   19 +++++++++++++++++++
 modules/memcached/doc/memcached_admin.xml |   24 ++++++++++++++++++++++++
 modules/memcached/mcd_var.c               |    8 ++++----
 modules/memcached/memcached.c             |   21 ++++++++++++---------
 modules/memcached/memcached.h             |    2 ++
 5 files changed, 61 insertions(+), 13 deletions(-)

diff --git a/modules/memcached/README b/modules/memcached/README
index 37e1d06..6abdfbc 100644
--- a/modules/memcached/README
+++ b/modules/memcached/README
@@ -27,6 +27,7 @@ Henning Westerholt
               4.3. mode (integer)
               4.4. timeout (integer)
               4.5. memory (integer)
+              4.6. stringify (integer)
 
         5.
 
@@ -43,6 +44,7 @@ Henning Westerholt
    1.7. Set mode parameter
    1.8. Set timeout parameter
    1.9. Set memory parameter
+   1.10. Set stringify parameter
 
 Chapter 1. Admin Guide
 
@@ -66,6 +68,7 @@ Chapter 1. Admin Guide
         4.3. mode (integer)
         4.4. timeout (integer)
         4.5. memory (integer)
+        4.6. stringify (integer)
 
    5.
 
@@ -179,6 +182,7 @@ xlog("stored value is $mct(test)"); # will return <null>
    4.3. mode (integer)
    4.4. timeout (integer)
    4.5. memory (integer)
+   4.6. stringify (integer)
 
 4.1. servers (str)
 
@@ -258,6 +262,21 @@ modparam("memcached", "timeout", 10000)
 modparam("memcached", "memory", 1)
 ...
 
+4.6. stringify (integer)
+
+   The string mode for the memcached module. By default the module checks
+   the flags for each returned value from the memcached library to decide
+   to evaluate it as string or numerical value. If you need
+   interoperability with existing applications that are not able to set
+   this flag, you can force the module to evaluate all values as strings.
+
+   Default value is “0” (don't force string values).
+
+   Example 1.10. Set stringify parameter
+...
+modparam("memcached", "stringify", 1)
+...
+
    5.1. Exported pseudo-variables
 
 5.1. Exported pseudo-variables
diff --git a/modules/memcached/doc/memcached_admin.xml b/modules/memcached/doc/memcached_admin.xml
index 15ff6f5..f0ddd3f 100644
--- a/modules/memcached/doc/memcached_admin.xml
+++ b/modules/memcached/doc/memcached_admin.xml
@@ -267,6 +267,30 @@ modparam("memcached", "memory", 1)
 			</programlisting>
 		</example>
 	</section>
+	<section id="memcached.p.stringify">
+		<title><varname>stringify</varname> (integer)</title>
+		<para>
+			The string mode for the memcached module. By default the module
+			checks the flags for each returned value from the memcached library
+			to decide to evaluate it as string or numerical value. If you need
+			interoperability with existing applications that are not able to
+			set this flag, you can force the module to evaluate all values as
+			strings.
+		</para>
+		<para>
+			<emphasis>
+				Default value is <quote>0</quote> (don't force string values).
+			</emphasis>
+		</para>
+		<example>
+			<title>Set <varname>stringify</varname> parameter</title>
+			<programlisting format="linespecific">
+...
+modparam("memcached", "stringify", 1)
+...
+			</programlisting>
+		</example>
+	</section>
 	</section>
 	<section>
 		<section>
diff --git a/modules/memcached/mcd_var.c b/modules/memcached/mcd_var.c
index a5bdf25..849a668 100644
--- a/modules/memcached/mcd_var.c
+++ b/modules/memcached/mcd_var.c
@@ -45,12 +45,12 @@ static inline int pv_mcd_key_expiry_split_str(str *data, str *key, unsigned int
 	str str_exp;
 	str_exp.s = NULL;
 	str_exp.len = 0;
-	
+
 	if (data == NULL || data->s == NULL || data->len <= 0) {
 		LM_ERR("invalid parameters\n");
 		return -1;
 	}
-	
+
 	p = data->s;
 	key->s = p;
 	key->len = 0;
@@ -208,7 +208,7 @@ int pv_get_mcd_value(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
 
 	trim_len(res_str.len, res_str.s, res_str);
 
-	if(return_flags&VAR_VAL_STR) {
+	if(return_flags&VAR_VAL_STR || mcd_stringify) {
 		res->rs.s = pv_get_buffer();
 		res->rs.len = pv_get_buffer_size();
 		if(res_str.len>=res->rs.len) {
@@ -314,7 +314,7 @@ static int pv_mcd_atomic_helper(struct sip_msg* msg, pv_param_t *param, int op,
 	char *return_value;
 	uint32_t return_flags;
 	memcached_return rc;
-	
+
 	if (!(val->flags&PV_VAL_INT)) {
 		LM_ERR("invalid value %.*s for atomic operation, strings not allowed\n",
 			val->rs.len, val->rs.s);
diff --git a/modules/memcached/memcached.c b/modules/memcached/memcached.c
index 0815d63..b3199d9 100644
--- a/modules/memcached/memcached.c
+++ b/modules/memcached/memcached.c
@@ -46,6 +46,8 @@ unsigned int mcd_mode = 0;
 unsigned int mcd_timeout = 5000;
 /*! Internal or system memory manager, default is system */
 unsigned int mcd_memory = 0;
+/*! stringify all values retrieved from memcached, default false */
+unsigned int mcd_stringify = 0;
 /*! memcached handle */
 struct memcached_st *memcached_h;
 /*! memcached server list */
@@ -77,11 +79,12 @@ static pv_export_t mod_pvs[] = {
  * Exported parameters
  */
 static param_export_t params[] = {
-	{"servers", STR_PARAM, &mcd_srv_str },
-	{"expire",  INT_PARAM, &mcd_expire },
-	{"timeout", INT_PARAM, &mcd_timeout },
-	{"mode",    INT_PARAM, &mcd_mode },
-	{"memory",  INT_PARAM, &mcd_memory },
+	{"servers",   STR_PARAM, &mcd_srv_str },
+	{"expire",    INT_PARAM, &mcd_expire },
+	{"timeout",   INT_PARAM, &mcd_timeout },
+	{"mode",      INT_PARAM, &mcd_mode },
+	{"memory",    INT_PARAM, &mcd_memory },
+	{"stringify", INT_PARAM, &mcd_stringify },
 	{0, 0, 0}
 };
 
@@ -239,7 +242,7 @@ static int mod_init(void) {
 		port = "11211";
 		len = strlen(mcd_srv_str) ;
 	}
-	
+
 	server = pkg_malloc(len);
 	if (server == NULL) {
 		PKG_MEM_ERROR;
@@ -280,7 +283,7 @@ static int mod_init(void) {
 	}
 
         servers = memcached_server_list_append(servers, server, atoi(port), &rc);
-	
+
 	if (memcached_behavior_set(memcached_h, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, mcd_timeout) != MEMCACHED_SUCCESS) {
 		LM_ERR("could not set server connection timeout\n");
 		return -1;
@@ -298,7 +301,7 @@ static int mod_init(void) {
 	/** \todo FIXME logic to handle connection errors on startup
 	memcached_server_cursor(memcached_h, (const memcached_server_fn*) &mcd_check_connection, NULL, 1);
 	*/
-	
+
 	LM_INFO("libmemcached version is %s\n", memcached_lib_version());
 	return 0;
 }
@@ -310,7 +313,7 @@ static int mod_init(void) {
 static void mod_destroy(void) {
 	if (servers != NULL)
 		memcached_server_list_free(servers);
-	
+
 	/* Crash on shutdown with internal memory manager, even if we disable the mm callbacks */
 	if (mcd_memory != 1 && memcached_h != NULL)
 		        memcached_free(memcached_h);
diff --git a/modules/memcached/memcached.h b/modules/memcached/memcached.h
index 407d39f..cd35f5d 100644
--- a/modules/memcached/memcached.h
+++ b/modules/memcached/memcached.h
@@ -38,6 +38,8 @@ extern unsigned int mcd_mode;
 extern unsigned int mcd_timeout;
 /*! Internal or system memory manager */
 extern unsigned int mcd_memory;
+/*! stringify all values retrieved from memcached */
+extern unsigned int mcd_stringify;
 /*! memcached handle */
 extern struct memcached_st* memcached_h;
 /*! memcached server list */




More information about the sr-dev mailing list