瀏覽代碼

htable(k): Added 'updateexpire' parameter to the definition of an
htable. It permits one to change whether updating a value stored
in the htable resets its time until expiration.

Its default value is 1, to preserve existing behaviour. However, if
set to 0, updating a value will have no effect on how soon it will
expire.

Alex Balashov 13 年之前
父節點
當前提交
aa5a8463c8

+ 10 - 0
modules_k/htable/README

@@ -11,6 +11,12 @@ Elena-Ramona Modroiu
 
    <[email protected]>
 
+Edited by
+
+Alex Balashov
+
+   <[email protected]>
+
    Copyright © 2008-2011 http://www.asipto.com
      __________________________________________________________________
 
@@ -278,6 +284,10 @@ if(is_present_hf("Authorization"))
        table).
      * initval - the integer value to be returned instead of $null when a
        requested key is not set.
+     * updateexpire - if set to 1 (default), the time until expiration of
+       an item is reset when that item is updated. Certain uses of htable
+       may dictate that updates should not reset the expiration timeout,
+       however, in which case this attribute can be set to 0.
 
    Default value is NULL.
 

+ 5 - 0
modules_k/htable/doc/htable.xml

@@ -29,6 +29,11 @@
 		<surname>Modroiu</surname>
 		<email>[email protected]</email>
 	    </editor>
+	    <editor>
+		<firstname>Alex</firstname>
+		<surname>Balashov</surname>
+		<email>[email protected]</email>
+	    </editor>
 	</authorgroup>
 	<copyright>
 	    <year>2008-2011</year>

+ 5 - 0
modules_k/htable/doc/htable_admin.xml

@@ -267,6 +267,11 @@ if(is_present_hf("Authorization"))
 			instead of $null when a requested key is not set.
 		</para>
 		</listitem>
+		<listitem>
+		<para>
+			<emphasis>updateexpire</emphasis> - if set to 1 (default), the time until expiration of an item is reset when that item is updated.  Certain uses of htable may dictate that updates should not reset the expiration timeout, however, in which case this attribute can be set to 0.
+		</para>
+		</listitem>
 		</itemizedlist>
 		<para>
 		<emphasis>

+ 18 - 5
modules_k/htable/ht_api.c

@@ -217,7 +217,7 @@ ht_t* ht_get_table(str *name)
 }
 
 int ht_add_table(str *name, int autoexp, str *dbtable, int size, int dbmode,
-		int itype, int_str *ival)
+		int itype, int_str *ival, int updateexpire)
 {
 	unsigned int htid;
 	ht_t *ht;
@@ -252,6 +252,7 @@ int ht_add_table(str *name, int autoexp, str *dbtable, int size, int dbmode,
 	else ht->htsize = 1<<size;
 	ht->htid = htid;
 	ht->htexpire = autoexp;
+	ht->updateexpire = updateexpire;
 	ht->name = *name;
 	if(dbtable!=NULL && dbtable->len>0)
 		ht->dbtable = *dbtable;
@@ -387,7 +388,9 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 						it->value.s.len = val->s.len;
 						memcpy(it->value.s.s, val->s.s, val->s.len);
 						it->value.s.s[it->value.s.len] = '\0';
-						it->expire = now + ht->htexpire;
+						
+						if(ht->updateexpire)
+							it->expire = now + ht->htexpire;
 					} else {
 						/* new */
 						cell = ht_cell_new(name, type, val, hid);
@@ -411,7 +414,9 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 				} else {
 					it->flags &= ~AVP_VAL_STR;
 					it->value.n = val->n;
-					it->expire = now + ht->htexpire;
+
+					if(ht->updateexpire)
+						it->expire = now + ht->htexpire;
 				}
 				if(mode) lock_release(&ht->entries[idx].lock);
 				return 0;
@@ -438,7 +443,9 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 					ht_cell_free(it);
 				} else {
 					it->value.n = val->n;
-					it->expire = now + ht->htexpire;
+
+					if(ht->updateexpire)
+						it->expire = now + ht->htexpire;
 				}
 				if(mode) lock_release(&ht->entries[idx].lock);
 				return 0;
@@ -717,6 +724,7 @@ int ht_table_spec(char *spec)
 	unsigned int autoexpire = 0;
 	unsigned int size = 4;
 	unsigned int dbmode = 0;
+	unsigned int updateexpire = 1;
 	str in;
 	str tok;
 	param_t *pit=NULL;
@@ -768,11 +776,16 @@ int ht_table_spec(char *spec)
 			itype = PV_VAL_INT;
 			LM_DBG("htable [%.*s] - initval [%d]\n", name.len, name.s,
 					ival.n);
+		} else if(pit->name.len == 12 && strncmp(pit->name.s, "updateexpire", 12) == 0) {
+			if(str2int(&tok, &updateexpire) != 0)
+				goto error;
+
+			LM_DBG("htable [%.*s] - updateexpire [%u]\n", name.len, name.s, updateexpire); 
 		} else { goto error; }
 	}
 
 	return ht_add_table(&name, autoexpire, &dbtable, size, dbmode,
-			itype, &ival);
+			itype, &ival, updateexpire);
 
 error:
 	LM_ERR("invalid htable parameter [%.*s]\n", in.len, in.s);

+ 2 - 1
modules_k/htable/ht_api.h

@@ -57,6 +57,7 @@ typedef struct _ht
 	int dbmode;
 	int flags;
 	int_str initval;
+	int updateexpire;
 	unsigned int htsize;
 	ht_entry_t *entries;
 	struct _ht *next;
@@ -69,7 +70,7 @@ typedef struct _ht_pv {
 } ht_pv_t, *ht_pv_p;
 
 int ht_add_table(str *name, int autoexp, str *dbtable, int size, int dbmode,
-		int itype, int_str *ival);
+		int itype, int_str *ival, int updateexpire);
 int ht_init_tables(void);
 int ht_destroy(void);
 int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode);