Cache: Allow entries to be explicitly deleted

This commit is contained in:
Johan Wikman
2016-11-22 18:41:10 +02:00
parent 2ecd5f3340
commit e9029b183e
4 changed files with 53 additions and 1 deletions

View File

@ -123,6 +123,17 @@ typedef struct cache_storage_api
cache_result_t (*putValue)(CACHE_STORAGE* storage,
const char* key,
const GWBUF* value);
/**
* Delete a value from the cache.
*
* @param storage Pointer to a CACHE_STORAGE.
* @param key A key generated with getKey.
* @return CACHE_RESULT_OK if item was successfully deleted. Note that
* CACHE_RESULT_OK may be returned also if the entry was not present.
*/
cache_result_t (*delValue)(CACHE_STORAGE* storage,
const char* key);
} CACHE_STORAGE_API;
#define CACHE_STORAGE_ENTRY_POINT "CacheGetStorageAPI"

View File

@ -476,3 +476,14 @@ cache_result_t RocksDBStorage::putValue(const char* pKey, const GWBUF* pValue)
return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR;
}
cache_result_t RocksDBStorage::delValue(const char* pKey)
{
ss_dassert(pKey);
rocksdb::Slice key(pKey, ROCKSDB_KEY_LENGTH);
rocksdb::Status status = m_sDb->Delete(writeOptions(), key);
return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR;
}

View File

@ -32,6 +32,7 @@ public:
cache_result_t getKey(const char* zDefaultDB, const GWBUF* pQuery, char* pKey);
cache_result_t getValue(const char* pKey, uint32_t flags, GWBUF** ppResult);
cache_result_t putValue(const char* pKey, const GWBUF* pValue);
cache_result_t delValue(const char* pKey);
private:
RocksDBStorage(std::unique_ptr<rocksdb::DBWithTTL>& sDb,

View File

@ -147,6 +147,34 @@ cache_result_t putValue(CACHE_STORAGE* pStorage,
return result;
}
cache_result_t delValue(CACHE_STORAGE* pStorage,
const char* pKey)
{
ss_dassert(pStorage);
ss_dassert(pKey);
cache_result_t result = CACHE_RESULT_ERROR;
try
{
result = reinterpret_cast<RocksDBStorage*>(pStorage)->delValue(pKey);
}
catch (const std::bad_alloc&)
{
MXS_OOM();
}
catch (const std::exception& x)
{
MXS_ERROR("Standard exception caught: %s", x.what());
}
catch (...)
{
MXS_ERROR("Unknown exception caught.");
}
return result;
}
}
extern "C"
@ -161,7 +189,8 @@ CACHE_STORAGE_API* CacheGetStorageAPI()
freeInstance,
getKey,
getValue,
putValue
putValue,
delValue,
};
return &api;