Skip to content

Commit

Permalink
Always use CPE match caching
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier committed Jan 9, 2025
1 parent 9f83906 commit c55d123
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
10 changes: 1 addition & 9 deletions pontos/nvd/cpe_match/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def __init__(
token: Optional[str] = None,
timeout: Optional[Timeout] = DEFAULT_TIMEOUT_CONFIG,
rate_limit: bool = True,
cache_cpe_matches: bool = True,
) -> None:
"""
Create a new instance of the CPE API.
Expand All @@ -70,21 +69,14 @@ def __init__(
rolling 30 second window.
See https://nvd.nist.gov/developers/start-here#divRateLimits
Default: True.
cache_cpe_matches: If set to True (the default) the entries in the
lists of matching CPEs for each match string are cached and reused
to use less memory.
If set to False, a separate CPEMatch object is kept for each entry
to avoid possible side effects when modifying the data.
"""
super().__init__(
DEFAULT_NIST_NVD_CPE_MATCH_URL,
token=token,
timeout=timeout,
rate_limit=rate_limit,
)
self._cpe_match_cache: Optional[dict[str, Any]] = None
if cache_cpe_matches:
self._cpe_match_cache = {}
self._cpe_match_cache: dict[str, Any] = {}

def cpe_matches(
self,
Expand Down
15 changes: 6 additions & 9 deletions pontos/nvd/models/cpe_match_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional
from typing import Any, List, Optional
from uuid import UUID

from pontos.models import Model
Expand Down Expand Up @@ -59,8 +59,8 @@ class CPEMatchString(Model):
@classmethod
def from_dict_with_cache(
cls,
data: Dict[str, Any],
cpe_match_cache: Optional[Dict[str, CPEMatch]],
data: dict[str, Any],
cpe_match_cache: dict[str, CPEMatch],
):
"""
Create a CPEMatchString model from a dict, reusing
Expand All @@ -73,14 +73,11 @@ def from_dict_with_cache(
to not cache and reused CPE matches
"""
new_match_string = cls.from_dict(data)
if cpe_match_cache is None:
return new_match_string

for i, match in enumerate(new_match_string.matches):
if match.cpe_name_id in cpe_match_cache:
cached_match: CPEMatch = cpe_match_cache[match.cpe_name_id]
if cached_match.cpe_name == match.cpe_name:
new_match_string.matches[i] = cached_match
cached_match: Optional[CPEMatch] = cpe_match_cache.get(match.cpe_name_id)
if cached_match and cached_match.cpe_name == match.cpe_name:
new_match_string.matches[i] = cached_match
else:
cpe_match_cache[match.cpe_name_id] = match
return new_match_string

0 comments on commit c55d123

Please sign in to comment.