diff --git a/src/barrier/generic_barrier.cpp b/src/barrier/generic_barrier.cpp index 0b2954e..48617f2 100644 --- a/src/barrier/generic_barrier.cpp +++ b/src/barrier/generic_barrier.cpp @@ -18,7 +18,7 @@ namespace zsy GenericBarrier::GenericBarrier(const BarrierProperties &config) : config(config) { - Application::eventManager->subscribe(config.sn + "/open", [this](const EventManager::Event&) + Application::eventManager->subscribe(config.sn + "/open", [this](const EventManager::Event &) { this->open(); }); @@ -48,6 +48,8 @@ namespace zsy Application::httpSvr->postEndpoint("/plateRecognize", [](const httplib::Request &req, httplib::Response &res) { + static std::unordered_map cache; + static std::mutex cacheMutex; try { auto body = req.body; @@ -75,6 +77,26 @@ namespace zsy res.set_content(R"({})", "application/json"); return; } + auto ms = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + auto key = serialno + license; + long long time = ms + 60000 * 5; + { + std::lock_guard lock(cacheMutex); + std::erase_if(cache, [time](const auto &pair) + { + return pair.second >= time; + }); + if (cache.contains(key)) + { + if (cache[key] < time) + { + res.set_content(R"({})", "application/json"); + LOGGER_INFO("重复的车牌识别结果:{} {}", serialno, license); + return; + } + } + cache[key] = ms; + } Application::threadPool->submit([imageFile,license, barrier = Application::deviceHolder->getBarrier(serialno)] {