Set and read expires as expire interval. Properly copy parameters when an item is found in cache.

git-svn-id: http://voip.null.ro/svn/yate@4412 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2011-05-27 08:33:33 +00:00
parent f96a0c40e6
commit 5139165952
2 changed files with 27 additions and 16 deletions

View File

@ -35,7 +35,7 @@
; Database query examples assume a 'lnp' table with the following fields:
; - id TEXT The called number
; - routing TEXT Routing number (may be empty)
; - expires NUMERIC Expire time (EPOCH) in seconds
; - timeout TIMESTAMP Expire time
; enable: boolean: Enable LNP cache
; Defaults to no
@ -71,19 +71,19 @@
; query_loadcache: string: Database query used to load the LNP cache when created
; This parameter is applied on reload
;query_loadcache=SELECT * FROM lnp
;query_loadcache=SELECT FLOOR(EXTRACT('EPOCH' FROM (timeout - CURRENT_TIMESTAMP))) AS expires,* FROM lnp
; query_loaditem: string: Database query used to load an item when created
; This parameter is applied on reload
;query_loaditem=SELECT * FROM lnp WHERE id='${id}'
;query_loaditem=SELECT FLOOR(EXTRACT('EPOCH' FROM (timeout - CURRENT_TIMESTAMP))) AS expires,* FROM lnp WHERE id='${id}'
; query_save: string: Database query used to save an item
; This parameter is applied on reload
;query_save=INSERT INTO lnp(id,routing,expires) VALUES('${id}','${routing}',${expires})
;query_save=INSERT INTO lnp(id,routing,timeout) VALUES('${id}','${routing}',CURRENT_TIMESTAMP + INTERVAL '${expires} s')
; query_expire: string: Database query used to expire items
; This parameter is applied on reload
;query_expire=DELETE FROM lnp WHERE expires <= ${time}
;query_expire=DELETE FROM lnp WHERE CURRENT_TIMESTAMP >= timeout
[cnam]
@ -91,7 +91,7 @@
; Database query examples assume a 'cnam' table with the following fields:
; - id TEXT The caller number
; - callername TEXT Caller's name (may be empty)
; - expires NUMERIC Expire time (EPOCH) in seconds
; - timeout TIMESTAMP Expire time
; enable: boolean: Enable CNAM cache
; Defaults to no
@ -121,16 +121,16 @@
; query_loadcache: string: Database query used to load the CNAM cache when created
; This parameter is applied on reload
;query_loadcache=SELECT * FROM cnam
;query_loadcache=SELECT FLOOR(EXTRACT('EPOCH' FROM (timeout - CURRENT_TIMESTAMP))) AS expires,* FROM cnam
; query_loaditem: string: Database query used to load an item when created
; This parameter is applied on reload
;query_loaditem=SELECT * FROM cnam WHERE id='${id}'
;query_loaditem=SELECT FLOOR(EXTRACT('EPOCH' FROM (timeout - CURRENT_TIMESTAMP))) AS expires,* FROM cnam WHERE id='${id}'
; query_save: string: Database query used to save an item
; This parameter is applied on reload
;query_save=INSERT INTO cnam(id,callername,expires) VALUES('${id}','${callername}',${expires})
;query_save=INSERT INTO cnam(id,callername,timeout) VALUES('${id}','${callername}',CURRENT_TIMESTAMP + INTERVAL '${expires} s')
; query_expire: string: Database query used to expire items
; This parameter is applied on reload
;query_expire=DELETE FROM cnam WHERE expires <= ${time}
;query_expire=DELETE FROM cnam WHERE CURRENT_TIMESTAMP >= timeout

View File

@ -83,7 +83,7 @@ public:
// Expire entries
void expire(const Time& time);
// Copy params from cache item. Return true if found
bool copyParams(const String& id, NamedList& list, const String& copy);
bool copyParams(const String& id, NamedList& list, const String* cpParams);
// Add an item to the cache. Remove an existing one
// Set dbSave=false when loading from database to avoid saving it again
void add(const String& id, const NamedList& params, const String* cpParams,
@ -277,7 +277,7 @@ Cache::Cache(const String& name, int size, const NamedList& params)
}
// Copy params from cache item. Return true if found
bool Cache::copyParams(const String& id, NamedList& list, const String& copy)
bool Cache::copyParams(const String& id, NamedList& list, const String* cpParams)
{
lock();
CacheItem* item = find(id);
@ -308,7 +308,7 @@ bool Cache::copyParams(const String& id, NamedList& list, const String& copy)
m_name.c_str(),id.c_str(),TelEngine::c_safe(error),this);
}
if (item) {
list.copyParams(*item,copy);
list.copyParams(*item,!cpParams ? m_copyParams : *cpParams);
dumpItem(*this,*item,"found in cache");
}
unlock();
@ -421,8 +421,19 @@ CacheItem* Cache::addUnsafe(const String& id, const NamedList& params, const Str
if (list)
list = list->skipNull();
u_int64_t expires = 0;
if (!dbSave)
expires = (u_int64_t)safeValue(params.getIntValue("expires")) * 1000000;
if (!dbSave) {
String* exp = params.getParam("expires");
if (exp) {
int tmp = (int)exp->toInteger();
if (tmp > 0)
expires = Time::now() + tmp * 1000000;
else {
XDebug(&__plugin,DebugAll,"Cache(%s) item '%s' already expired [%p]",
m_name.c_str(),id.c_str(),this);
return 0;
}
}
}
if (!expires)
expires = Time::now() + m_cacheTtl;
// Search for insert/add point and existing item
@ -464,7 +475,7 @@ CacheItem* Cache::addUnsafe(const String& id, const NamedList& params, const Str
String query = m_querySave;
NamedList p(*item);
p.setParam("id",item->toString());
p.setParam("expires",String((unsigned int)(expires / 1000000)));
p.setParam("expires",String((unsigned int)(m_cacheTtl / 1000000)));
p.replaceParams(query);
Message* m = new Message("database");
m->addParam("account",m_account);