Skip to content

Commit

Permalink
Merge pull request #4 from akquinet/caching
Browse files Browse the repository at this point in the history
Caching
  • Loading branch information
rwxd authored Nov 10, 2023
2 parents 62dc0a7 + 0d15472 commit 47b0066
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions powerdns_api_proxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def load_config(path: Optional[Path] = None) -> ProxyConfig:
return config


@lru_cache(maxsize=1000)
def token_defined(config: ProxyConfig, token: str) -> bool:
sha512 = hashlib.sha512()
sha512.update(token.encode())
Expand All @@ -57,6 +58,7 @@ def dependency_check_token_defined(
check_token_defined(load_config(), X_API_Key)


@lru_cache(maxsize=1000)
def get_environment_for_token(
config: ProxyConfig, token: str
) -> ProxyConfigEnvironment:
Expand Down
19 changes: 19 additions & 0 deletions powerdns_api_proxy/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import lru_cache
from typing import TypedDict

from fastapi import HTTPException
Expand Down Expand Up @@ -79,6 +80,16 @@ def __init__(self, **data):
# populate zones lookup
self._zones_lookup[zone.name] = zone

def __hash__(self):
return hash(
self.name
+ self.token_sha512
+ str(self.global_read_only)
+ str(self.global_search)
+ str(self.zones)
)

@lru_cache(maxsize=10000)
def get_zone_if_allowed(self, zone: str) -> ProxyConfigZone:
'''
Returns the zone config for the given zone name
Expand Down Expand Up @@ -122,6 +133,14 @@ def api_token_defined(cls, v):
raise ValueError('pdns_api_token must a non-empty string')
return v

def __hash__(self):
return hash(
self.pdns_api_url
+ self.pdns_api_token
+ str(self.pdns_api_verify_ssl)
+ str(self.environments)
)


class ResponseAllowed(BaseModel):
zones: list[ProxyConfigZone]
Expand Down

0 comments on commit 47b0066

Please sign in to comment.