From 5b76f9c1feae4de424ef2b3133d562803b02d335 Mon Sep 17 00:00:00 2001 From: Steven Shi Date: Tue, 24 Jul 2018 00:43:02 -0400 Subject: [PATCH] cache purging request on query rule basis, issue #1013 --- include/query_cache.hpp | 5 +++-- lib/MySQL_Session.cpp | 4 +++- lib/Query_Cache.cpp | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/query_cache.hpp b/include/query_cache.hpp index f2b53271a..e03b8c942 100644 --- a/include/query_cache.hpp +++ b/include/query_cache.hpp @@ -23,6 +23,7 @@ struct __QC_entry_t { QC_entry_t *self; // pointer to itself uint32_t klen; // length of the key : FIXME: not sure if still relevant uint32_t length; // length of the value + unsigned long long create_ms; // when the entry was created, monotonic, millisecond granularity unsigned long long expire_ms; // when the entry will expire, monotonic , millisecond granularity unsigned long long access_ms; // when the entry was read last , monotonic , millisecond granularity uint32_t ref_count; // reference counter @@ -48,8 +49,8 @@ class Query_Cache { Query_Cache(); ~Query_Cache(); void print_version(); - bool set(uint64_t , const unsigned char *, uint32_t, unsigned char *, uint32_t, unsigned long long, unsigned long long); - unsigned char * get(uint64_t , const unsigned char *, const uint32_t, uint32_t *, unsigned long long); + bool set(uint64_t , const unsigned char *, uint32_t, unsigned char *, uint32_t, unsigned long long, unsigned long long, unsigned long long); + unsigned char * get(uint64_t , const unsigned char *, const uint32_t, uint32_t *, unsigned long long, unsigned long long); uint64_t flush(); SQLite3_result * SQL3_getStats(); }; diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 654dac0ae..344365019 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -4193,7 +4193,8 @@ bool MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C (const unsigned char *)CurrentQuery.QueryPointer , CurrentQuery.QueryLength , &resbuf , - thread->curtime/1000 + thread->curtime/1000 , + qpo->cache_ttl ); if (aa) { l_free(pkt->size,pkt->ptr); @@ -4433,6 +4434,7 @@ void MySQL_Session::MySQL_Result_to_MySQL_wire(MYSQL *mysql, MySQL_ResultSet *My aa , client_myds->resultset_length , thread->curtime/1000 , + thread->curtime/1000 , thread->curtime/1000 + qpo->cache_ttl ); l_free(client_myds->resultset_length,aa); diff --git a/lib/Query_Cache.cpp b/lib/Query_Cache.cpp index 50f8de437..4965c0dce 100644 --- a/lib/Query_Cache.cpp +++ b/lib/Query_Cache.cpp @@ -344,7 +344,7 @@ Query_Cache::~Query_Cache() { } }; -unsigned char * Query_Cache::get(uint64_t user_hash, const unsigned char *kp, const uint32_t kl, uint32_t *lv, unsigned long long curtime_ms) { +unsigned char * Query_Cache::get(uint64_t user_hash, const unsigned char *kp, const uint32_t kl, uint32_t *lv, unsigned long long curtime_ms, unsigned long long cache_ttl) { unsigned char *result=NULL; uint64_t hk=SpookyHash::Hash64(kp, kl, user_hash); @@ -354,7 +354,7 @@ unsigned char * Query_Cache::get(uint64_t user_hash, const unsigned char *kp, co if (entry!=NULL) { unsigned long long t=curtime_ms; - if (entry->expire_ms > t) { + if (entry->expire_ms > t && entry->create_ms + cache_ttl > t) { THR_UPDATE_CNT(__thr_cntGetOK,Glo_cntGetOK,1,1); THR_UPDATE_CNT(__thr_dataOUT,Glo_dataOUT,entry->length,1); result=(unsigned char *)malloc(entry->length); @@ -367,7 +367,7 @@ unsigned char * Query_Cache::get(uint64_t user_hash, const unsigned char *kp, co return result; } -bool Query_Cache::set(uint64_t user_hash, const unsigned char *kp, uint32_t kl, unsigned char *vp, uint32_t vl, unsigned long long curtime_ms, unsigned long long expire_ms) { +bool Query_Cache::set(uint64_t user_hash, const unsigned char *kp, uint32_t kl, unsigned char *vp, uint32_t vl, unsigned long long create_ms, unsigned long long curtime_ms, unsigned long long expire_ms) { QC_entry_t *entry = (QC_entry_t *)malloc(sizeof(QC_entry_t)); entry->klen=kl; entry->length=vl; @@ -376,6 +376,7 @@ bool Query_Cache::set(uint64_t user_hash, const unsigned char *kp, uint32_t kl, entry->value=(char *)malloc(vl); memcpy(entry->value,vp,vl); entry->self=entry; + entry->create_ms=create_ms; entry->access_ms=curtime_ms; entry->expire_ms=expire_ms; uint64_t hk=SpookyHash::Hash64(kp, kl, user_hash);