MXS-1486 When there is fresh data, update the cache entry

If something is SELECTed that should be cached for some, but not
for the current user, the cached entry it nevertheless updated.
That way the cached data will always be the last fetched value
and it is also possible to use this behaviour for explicitly
updating the cache entry.
This commit is contained in:
Johan Wikman
2017-10-24 09:41:23 +03:00
parent 555aa6d2c8
commit efeaecaef2
2 changed files with 66 additions and 71 deletions

View File

@ -290,11 +290,16 @@ int CacheFilterSession::routeQuery(GWBUF* pPacket)
if (should_consult_cache(pPacket)) if (should_consult_cache(pPacket))
{ {
if (m_pCache->should_store(m_zDefaultDb, pPacket)) if (m_pCache->should_store(m_zDefaultDb, pPacket))
{
cache_result_t result = m_pCache->get_key(m_zDefaultDb, pPacket, &m_key);
if (CACHE_RESULT_IS_OK(result))
{ {
if (m_pCache->should_use(m_pSession)) if (m_pCache->should_use(m_pSession))
{ {
uint32_t flags = CACHE_FLAGS_INCLUDE_STALE;
GWBUF* pResponse; GWBUF* pResponse;
cache_result_t result = get_cached_response(pPacket, &pResponse); result = m_pCache->get_value(m_key, flags, &pResponse);
if (CACHE_RESULT_IS_OK(result)) if (CACHE_RESULT_IS_OK(result))
{ {
@ -359,6 +364,23 @@ int CacheFilterSession::routeQuery(GWBUF* pPacket)
rv = dcb->func.write(dcb, pResponse); rv = dcb->func.write(dcb, pResponse);
} }
} }
else
{
// We will not use any value in the cache, but we will update
// the existing value.
if (log_decisions())
{
MXS_NOTICE("Unconditionally fetching data from the server, "
"refreshing cache entry.");
}
m_state = CACHE_EXPECTING_RESPONSE;
}
}
else
{
MXS_ERROR("Could not create cache key.");
m_state = CACHE_IGNORING_RESPONSE;
}
} }
else else
{ {
@ -775,31 +797,6 @@ void CacheFilterSession::reset_response_state()
m_res.offset = 0; m_res.offset = 0;
} }
/**
* Route a query via the cache.
*
* @param key A SELECT packet.
* @param value The result.
* @return True if the query was satisfied from the query.
*/
cache_result_t CacheFilterSession::get_cached_response(const GWBUF *pQuery, GWBUF **ppResponse)
{
cache_result_t result = m_pCache->get_key(m_zDefaultDb, pQuery, &m_key);
if (CACHE_RESULT_IS_OK(result))
{
uint32_t flags = CACHE_FLAGS_INCLUDE_STALE;
result = m_pCache->get_value(m_key, flags, ppResponse);
}
else
{
MXS_ERROR("Could not create cache key.");
}
return result;
}
/** /**
* Store the data. * Store the data.
* *

View File

@ -102,8 +102,6 @@ private:
void reset_response_state(); void reset_response_state();
cache_result_t get_cached_response(const GWBUF *pQuery, GWBUF **ppResponse);
bool log_decisions() const bool log_decisions() const
{ {
return m_pCache->config().debug & CACHE_DEBUG_DECISIONS ? true : false; return m_pCache->config().debug & CACHE_DEBUG_DECISIONS ? true : false;