Commit c1306e15 authored by Matthieu Dorier's avatar Matthieu Dorier

added removal of keys from ProductCache

parent 7a7bb9d3
...@@ -17,26 +17,36 @@ struct ProductCacheImpl { ...@@ -17,26 +17,36 @@ struct ProductCacheImpl {
mutable tl::rwlock m_lock; mutable tl::rwlock m_lock;
std::unordered_map<std::string, std::string> m_map; std::unordered_map<std::string, std::string> m_map;
bool m_erase_on_load = false;
bool loadRawProduct(const ProductID& product_id, std::string& data) const { bool loadRawProduct(const ProductID& product_id, std::string& data) {
m_lock.rdlock(); if(!m_erase_on_load) m_lock.rdlock();
else m_lock.wrlock();
auto it = m_map.find(product_id.m_key); auto it = m_map.find(product_id.m_key);
auto found = it != m_map.end(); auto found = it != m_map.end();
if(found) { if(found) {
data = it->second; if(!m_erase_on_load) {
data = it->second;
} else {
data = std::move(it->second);
m_map.erase(it);
}
} }
m_lock.unlock(); m_lock.unlock();
return found; return found;
} }
bool loadRawProduct(const ProductID& product_id, char* value, size_t* vsize) const { bool loadRawProduct(const ProductID& product_id, char* value, size_t* vsize) {
m_lock.rdlock(); if(!m_erase_on_load) m_lock.rdlock();
else m_lock.wrlock();
auto it = m_map.find(product_id.m_key); auto it = m_map.find(product_id.m_key);
auto found = it != m_map.end(); auto found = it != m_map.end();
if(found) { if(found) {
auto& data = it->second; auto& data = it->second;
*vsize = data.size(); *vsize = data.size();
if(*vsize) std::memcpy(value, data.data(), *vsize); if(*vsize) std::memcpy(value, data.data(), *vsize);
if(m_erase_on_load)
m_map.erase(it);
} }
m_lock.unlock(); m_lock.unlock();
return found; return found;
...@@ -44,14 +54,14 @@ struct ProductCacheImpl { ...@@ -44,14 +54,14 @@ struct ProductCacheImpl {
bool loadRawProduct(const ItemDescriptor& id, bool loadRawProduct(const ItemDescriptor& id,
const std::string& productName, const std::string& productName,
std::string& data) const { std::string& data) {
auto product_id = DataStoreImpl::buildProductID(id, productName); auto product_id = DataStoreImpl::buildProductID(id, productName);
return loadRawProduct(product_id, data); return loadRawProduct(product_id, data);
} }
bool loadRawProduct(const ItemDescriptor& id, bool loadRawProduct(const ItemDescriptor& id,
const std::string& productName, const std::string& productName,
char* value, size_t* vsize) const { char* value, size_t* vsize) {
auto product_id = DataStoreImpl::buildProductID(id, productName); auto product_id = DataStoreImpl::buildProductID(id, productName);
return loadRawProduct(product_id, value, vsize); return loadRawProduct(product_id, value, vsize);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment