Skip to content

[Performance] Optimize cache key generation using hashed scopes #409

@cbcoutinho

Description

@cbcoutinho

Context

From PR #401 code review - Medium priority performance optimization.

Current Implementation

Cache keys are generated by joining sorted scopes with comma:

cache_key = f"{user_id}:background:{','.join(sorted(required_scopes))}"

Problem

For requests with many scopes, this creates long cache keys. While functional, hashing would be more efficient.

Proposed Solution

Use hash of sorted scopes instead:

import hashlib
scope_hash = hashlib.sha256(','.join(sorted(required_scopes)).encode()).hexdigest()[:16]
cache_key = f"{user_id}:background:{scope_hash}"

Benefits

  • Shorter cache keys (fixed 16 chars vs variable length)
  • Faster cache lookups with many scopes
  • Reduced memory footprint in cache storage

References

Priority

Medium - Performance optimization, no correctness impact

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions