From 4d1a0164290d3599b8f7c0611dca788b20eac874 Mon Sep 17 00:00:00 2001 From: Andre Iwers Date: Thu, 18 Jan 2024 02:33:16 +0000 Subject: [PATCH] HTTP Libraries: Improved cache performance --- common/http_lib/http_lib_connection.cpp | 16 ++++++++++------ eeschema/sch_io/http_lib/sch_io_http_lib.cpp | 6 +++--- eeschema/sch_io/http_lib/sch_io_http_lib.h | 2 +- include/http_lib/http_lib_connection.h | 3 ++- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/common/http_lib/http_lib_connection.cpp b/common/http_lib/http_lib_connection.cpp index 04fa97e186..0f52c6deee 100644 --- a/common/http_lib/http_lib_connection.cpp +++ b/common/http_lib/http_lib_connection.cpp @@ -178,12 +178,16 @@ bool HTTP_LIB_CONNECTION::SelectOne( const std::string& aPartID, HTTP_LIB_PART& return false; } - // if the same part is selected again and cache has not expired use cached part - // instead to minimise http requests. - if( m_cached_part.id == aPartID && ( std::difftime( std::time( nullptr ), m_cached_part.lastCached ) < m_timeout ) ) + // Check if there is already a part in our cache, if not fetch it + if( m_cachedParts.find( aPartID ) != m_cachedParts.end() ) { - aFetchedPart = m_cached_part; - return true; + // check if it's outdated, if so re-fetch + if( std::difftime( std::time( nullptr ), m_cachedParts[aPartID].lastCached ) < m_timeout ) + { + aFetchedPart = m_cachedParts[aPartID]; + return true; + } + } std::string res = ""; @@ -290,7 +294,7 @@ bool HTTP_LIB_CONNECTION::SelectOne( const std::string& aPartID, HTTP_LIB_PART& return false; } - m_cached_part = aFetchedPart; + m_cachedParts[aFetchedPart.id] = aFetchedPart; return true; } diff --git a/eeschema/sch_io/http_lib/sch_io_http_lib.cpp b/eeschema/sch_io/http_lib/sch_io_http_lib.cpp index 8386ffd0b1..7ca9a6db9e 100644 --- a/eeschema/sch_io/http_lib/sch_io_http_lib.cpp +++ b/eeschema/sch_io/http_lib/sch_io_http_lib.cpp @@ -70,7 +70,7 @@ void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector& aSymbolList, && aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() ); // clear buffer - m_cachedParts.clear(); + m_cachedCategoryParts.clear(); for(const HTTP_LIB_CATEGORY& category : m_conn->getCategories() ) { @@ -89,7 +89,7 @@ void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector& aSymbolList, } // cache information for later use in LoadSymbol() - m_cachedParts.emplace( category.id, found_parts ); + m_cachedCategoryParts.emplace( category.id, found_parts ); for( const HTTP_LIB_PART& part : found_parts ) { @@ -147,7 +147,7 @@ LIB_SYMBOL* SCH_IO_HTTP_LIB::LoadSymbol( const wxString& aLibraryPath, } // get the matching query ID - for( const HTTP_LIB_PART& part : m_cachedParts[foundCategory->id] ) + for( const HTTP_LIB_PART& part : m_cachedCategoryParts[foundCategory->id] ) { if( part.id == std::get<0>( relations ) ) { diff --git a/eeschema/sch_io/http_lib/sch_io_http_lib.h b/eeschema/sch_io/http_lib/sch_io_http_lib.h index 357b537bbb..87add0dfee 100644 --- a/eeschema/sch_io/http_lib/sch_io_http_lib.h +++ b/eeschema/sch_io/http_lib/sch_io_http_lib.h @@ -100,7 +100,7 @@ private: SYMBOL_LIB_TABLE* m_libTable; - std::map> m_cachedParts; + std::map> m_cachedCategoryParts; /// Generally will be null if no valid connection is established std::unique_ptr m_conn; diff --git a/include/http_lib/http_lib_connection.h b/include/http_lib/http_lib_connection.h index 59bc5caf51..342aeffc6a 100644 --- a/include/http_lib/http_lib_connection.h +++ b/include/http_lib/http_lib_connection.h @@ -94,7 +94,8 @@ private: HTTP_LIB_SOURCE_TYPE m_sourceType; - HTTP_LIB_PART m_cached_part; + // part.id part + std::map m_cachedParts; // part.name part.id category.id std::map> m_cache;