Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,9 @@ def restart_privacy_request_from_failure(
)

# Automatically resubmit the request if the cache has expired

if (
not privacy_request.get_cached_identity_data()
not privacy_request.verify_cache_for_identity_data()
and privacy_request.status
not in [PrivacyRequestStatus.complete, PrivacyRequestStatus.pending]
):
Expand Down
41 changes: 38 additions & 3 deletions src/fides/api/models/privacy_request/privacy_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
)


IDENTITY_DATA_CACHE_KEY_PREFIX = "id-{{self.id}}-identity-*"
CUSTOM_PRIVACY_REQUEST_FIELDS_CACHE_KEY_PREFIX = (
"id-{{self.id}}-custom-privacy-request-field-*"
)


class PrivacyRequest(
IdentityVerificationMixin, DecryptedIdentityAutomatonMixin, Contextualizable, Base
): # pylint: disable=R0904,too-many-instance-attributes
Expand Down Expand Up @@ -682,12 +688,26 @@ def persist_masking_secrets(
},
)

def verify_cache_for_identity_data(self) -> bool:
"""Verifies if the identity data is cached for this request"""
prefix = IDENTITY_DATA_CACHE_KEY_PREFIX.format(id=self.id)
cache: FidesopsRedis = get_cache()
keys = cache.keys(prefix)
return len(keys) > 0

def get_cached_identity_data(self) -> Dict[str, Any]:
"""Retrieves any identity data pertaining to this request from the cache"""
prefix = f"id-{self.id}-identity-*"
result: Dict[str, Any] = {}
prefix = IDENTITY_DATA_CACHE_KEY_PREFIX.format(id=self.id)
cache: FidesopsRedis = get_cache()
keys = cache.keys(prefix)
result = {}

if not keys:
logger.debug(f"Cache miss for request {self.id}, falling back to DB")
identity = self.get_persisted_identity()
self.cache_identity(identity)
keys = cache.keys(prefix)

for key in keys:
value = cache.get(key)
if value:
Expand All @@ -705,10 +725,25 @@ def get_cached_identity_data(self) -> Dict[str, Any]:

def get_cached_custom_privacy_request_fields(self) -> Dict[str, Any]:
"""Retrieves any custom fields pertaining to this request from the cache"""
result: Dict[str, Any] = {}
prefix = f"id-{self.id}-custom-privacy-request-field-*"

cache: FidesopsRedis = get_cache()
keys = cache.keys(prefix)
result = {}

if not keys:
logger.debug(f"Cache miss for request {self.id}, falling back to DB")
custom_privacy_request_fields = (
self.get_persisted_custom_privacy_request_fields()
)
self.cache_custom_privacy_request_fields(
{
key: CustomPrivacyRequestFieldSchema(**value)
for key, value in custom_privacy_request_fields.items()
}
)
keys = cache.keys(prefix)

for key in keys:
value = cache.get(key)
if value:
Expand Down
Loading