2020-07-11 21:23:48 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
void StorageCache::clear()
|
|
|
|
{
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
if (m_setkeys != nullptr)
|
|
|
|
m_setkeys->clear();
|
|
|
|
m_spstorage->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::cacheKey(sds key)
|
|
|
|
{
|
|
|
|
if (m_setkeys == nullptr)
|
|
|
|
return;
|
|
|
|
m_setkeys->insert(sdsimmutablestring(sdsdupshared(key)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::cacheKey(const char *rgch, size_t cch)
|
|
|
|
{
|
|
|
|
if (m_setkeys == nullptr)
|
|
|
|
return;
|
|
|
|
m_setkeys->insert(sdsimmutablestring(sdsnewlen(rgch, cch)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StorageCache::erase(sds key)
|
|
|
|
{
|
|
|
|
bool result = m_spstorage->erase(key, sdslen(key));
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
if (result && m_setkeys != nullptr)
|
|
|
|
{
|
|
|
|
auto itr = m_setkeys->find(sdsview(key));
|
|
|
|
serverAssert(itr != m_setkeys->end());
|
|
|
|
m_setkeys->erase(itr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::insert(sds key, const void *data, size_t cbdata, bool fOverwrite)
|
|
|
|
{
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
if (!fOverwrite && m_setkeys != nullptr)
|
|
|
|
{
|
|
|
|
cacheKey(key);
|
|
|
|
}
|
2020-08-09 23:36:20 +00:00
|
|
|
ul.unlock();
|
2020-07-11 21:23:48 +00:00
|
|
|
m_spstorage->insert(key, sdslen(key), (void*)data, cbdata, fOverwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
const StorageCache *StorageCache::clone()
|
|
|
|
{
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
// Clones never clone the cache
|
|
|
|
StorageCache *cacheNew = new StorageCache(const_cast<IStorage*>(m_spstorage->clone()));
|
|
|
|
return cacheNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::retrieve(sds key, IStorage::callbackSingle fn, sds *cachedKey) const
|
|
|
|
{
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
if (m_setkeys != nullptr)
|
|
|
|
{
|
|
|
|
auto itr = m_setkeys->find(sdsview(key));
|
|
|
|
if (itr == m_setkeys->end())
|
|
|
|
return; // Not found
|
|
|
|
if (cachedKey != nullptr)
|
|
|
|
*cachedKey = sdsdupshared(itr->get());
|
|
|
|
}
|
2020-08-09 23:36:20 +00:00
|
|
|
ul.unlock();
|
2020-07-11 21:23:48 +00:00
|
|
|
m_spstorage->retrieve(key, sdslen(key), fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StorageCache::count() const
|
|
|
|
{
|
2020-08-09 23:36:20 +00:00
|
|
|
std::unique_lock<fastlock> ul(m_lock);
|
2020-07-11 21:23:48 +00:00
|
|
|
size_t count = m_spstorage->count();
|
|
|
|
if (m_setkeys != nullptr)
|
|
|
|
serverAssert(count == m_setkeys->size());
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::beginWriteBatch() {
|
|
|
|
serverAssert(GlobalLocksAcquired()); // Otherwise we deadlock
|
|
|
|
m_spstorage->beginWriteBatch();
|
|
|
|
}
|