SPARK-1948 Implement SecurityCache
This commit is contained in:
1 parent
4af4af8677
commit
8a7174378a
4 files changed
+155
-1
No files matched your search
@@ -0,0 +1,104 @@
|
||||
#include "SecurityCache.h"
|
||||
#include "SecurityDB.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
#include <mutex>
|
||||
|
||||
/*
|
||||
todo:
|
||||
0 - All new code uses SecurityKey
|
||||
1 - Implement SecurityCache. It is a simple structure
|
||||
2 - MDConsumer has a SecurityCache
|
||||
3 - scan_securities inserts securities into SecurityCache
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
struct SecurityCacheImpl {
|
||||
std::mutex cache_mutex;
|
||||
std::unordered_map<SecurityKey, SecurityPtr> cache;
|
||||
tbb::concurrent_queue<SecurityPtr> update_queue;
|
||||
void update_db(const SecurityPtr&);
|
||||
};
|
||||
|
||||
SecurityPtr load_from_security_db(SecurityKey key) {
|
||||
return db::find_security(key);
|
||||
}
|
||||
|
||||
void SecurityCacheImpl::update_db(const SecurityPtr& security) {
|
||||
// Batch updates to the database for performance
|
||||
// algo:
|
||||
// push item into queue
|
||||
// post work to dispatcher with delay
|
||||
// When dispatcher services request drain update_queue.
|
||||
// This will cause one update per security but most will be no-op
|
||||
// This is a little simpler than running a timer and trying to stop it after security discovery is done
|
||||
update_queue.push(security);
|
||||
utils::Dispatcher::background().post([this]() {
|
||||
if (!update_queue.empty()) {
|
||||
std::vector<SecurityPtr> work;
|
||||
SecurityPtr temp;
|
||||
while(update_queue.try_pop(temp)) { work.push_back(temp); }
|
||||
if (!work.empty()) {
|
||||
db::insert_or_replace(work);
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
|
||||
SecurityCache::SecurityCache() : _me(new SecurityCacheImpl()) {}
|
||||
SecurityCache::~SecurityCache() {}
|
||||
|
||||
SecurityPtr SecurityCache::try_get_security(SecurityKey key) const {
|
||||
std::lock_guard<std::mutex> lock(_me->cache_mutex);
|
||||
auto iter = _me->cache.find(key);
|
||||
if (iter != _me->cache.end()) {
|
||||
return iter->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SecurityPtr SecurityCache::get_security(SecurityKey key) {
|
||||
std::unique_lock<std::mutex> lock(_me->cache_mutex);
|
||||
auto iter = _me->cache.find(key);
|
||||
if (iter != _me->cache.end()) {
|
||||
return iter->second;
|
||||
}
|
||||
else {
|
||||
// unlock for db read
|
||||
lock.unlock();
|
||||
auto security = load_from_security_db(key);
|
||||
if (security) {
|
||||
return add_db_security(security);
|
||||
}
|
||||
return security;
|
||||
}
|
||||
}
|
||||
|
||||
SecurityPtr SecurityCache::add_db_security(const SecurityPtr& security) {
|
||||
bts_assert(security);
|
||||
std::lock_guard<std::mutex> lock(_me->cache_mutex);
|
||||
SecurityKey key(security->id());
|
||||
auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security));
|
||||
// If result was inserted result.first->second is equal to security
|
||||
// If the value was already present it must be a recently inserted 'live' security and we should use it
|
||||
return result.first->second;
|
||||
}
|
||||
bool SecurityCache::add_live_security(const SecurityPtr& security) {
|
||||
bts_assert(security);
|
||||
std::lock_guard<std::mutex> lock(_me->cache_mutex);
|
||||
SecurityKey key(security->id());
|
||||
// Insert but don't overwrite existing
|
||||
auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security));
|
||||
_me->update_db(security);
|
||||
return result.second;
|
||||
}
|
||||
|
||||
}}//ns
|
||||
Reference in new issue
Block a user