Skip to content

Cache user positions per wallet#381

Open
silentgeckoaudit3801 wants to merge 5 commits into
Quantarq:mainfrom
silentgeckoaudit3801:perf/cache-user-positions-237
Open

Cache user positions per wallet#381
silentgeckoaudit3801 wants to merge 5 commits into
Quantarq:mainfrom
silentgeckoaudit3801:perf/cache-user-positions-237

Conversation

@silentgeckoaudit3801

Copy link
Copy Markdown

Closes #237

Summary

  • cache /api/user-positions/{wallet_id} responses for 60 seconds using wallet/start/limit scoped keys
  • add Redis pattern invalidation for user-position cache entries
  • invalidate the user-position cache when positions are created, opened, closed, or extra deposits are recorded

Validation

  • Static API validation: branch diff is 0 behind upstream main and changes only cache/user-position API wiring plus static coverage
  • GitHub Actions will run the project gates for this PR
  • Not run locally: project dependency/toolchain execution is intentionally avoided in this workspace



def test_user_positions_endpoint_uses_wallet_scoped_cache():
assert "USER_POSITIONS_CACHE_TTL = 60" in POSITION_SOURCE

def test_user_positions_endpoint_uses_wallet_scoped_cache():
assert "USER_POSITIONS_CACHE_TTL = 60" in POSITION_SOURCE
assert "def _user_positions_cache_key(wallet_id: str, start: int, limit: int)" in POSITION_SOURCE
def test_user_positions_endpoint_uses_wallet_scoped_cache():
assert "USER_POSITIONS_CACHE_TTL = 60" in POSITION_SOURCE
assert "def _user_positions_cache_key(wallet_id: str, start: int, limit: int)" in POSITION_SOURCE
assert 'return f"user_positions:{wallet_id}:{start}:{limit}"' in POSITION_SOURCE
assert "USER_POSITIONS_CACHE_TTL = 60" in POSITION_SOURCE
assert "def _user_positions_cache_key(wallet_id: str, start: int, limit: int)" in POSITION_SOURCE
assert 'return f"user_positions:{wallet_id}:{start}:{limit}"' in POSITION_SOURCE
assert "await get_cached_or_fetch(" in POSITION_SOURCE
assert "def _user_positions_cache_key(wallet_id: str, start: int, limit: int)" in POSITION_SOURCE
assert 'return f"user_positions:{wallet_id}:{start}:{limit}"' in POSITION_SOURCE
assert "await get_cached_or_fetch(" in POSITION_SOURCE
assert "ttl=USER_POSITIONS_CACHE_TTL" in POSITION_SOURCE
Comment thread quantara/web_app/tests/test_user_positions_cache_static.py Fixed


def test_cache_helper_supports_pattern_invalidation():
assert "async def delete_cache_pattern(pattern: str)" in CACHE_SOURCE

def test_cache_helper_supports_pattern_invalidation():
assert "async def delete_cache_pattern(pattern: str)" in CACHE_SOURCE
assert "await client.scan(cursor=cursor, match=pattern, count=100)" in CACHE_SOURCE
def test_cache_helper_supports_pattern_invalidation():
assert "async def delete_cache_pattern(pattern: str)" in CACHE_SOURCE
assert "await client.scan(cursor=cursor, match=pattern, count=100)" in CACHE_SOURCE
assert "await client.delete(*keys)" in CACHE_SOURCE
assert "async def delete_cache_pattern(pattern: str)" in CACHE_SOURCE
assert "await client.scan(cursor=cursor, match=pattern, count=100)" in CACHE_SOURCE
assert "await client.delete(*keys)" in CACHE_SOURCE
assert "Cache invalidation failed" in CACHE_SOURCE No newline at end of file
Comment thread quantara/web_app/tests/test_user_positions_cache_static.py Fixed
assert 'await delete_cache_pattern(f"user_positions:{wallet_id}:*")' in POSITION_SOURCE
assert "await _invalidate_user_positions_cache(form_data.wallet_id)" in POSITION_SOURCE
assert "wallet_id = _get_wallet_id_for_position(position_id)" in POSITION_SOURCE
assert "await _invalidate_user_positions_cache(wallet_id)" in POSITION_SOURCE
assert "await _invalidate_user_positions_cache(form_data.wallet_id)" in POSITION_SOURCE
assert "wallet_id = _get_wallet_id_for_position(position_id)" in POSITION_SOURCE
assert "await _invalidate_user_positions_cache(wallet_id)" in POSITION_SOURCE
assert "def _get_wallet_id_for_position_object(position: object | None)" in POSITION_SOURCE
Comment thread quantara/web_app/tests/test_user_positions_cache_static.py Fixed
assert "await _invalidate_user_positions_cache(wallet_id)" in POSITION_SOURCE
assert "def _get_wallet_id_for_position_object(position: object | None)" in POSITION_SOURCE
assert 'getattr(position, "user_id", None)' in POSITION_SOURCE
assert "_get_wallet_id_for_position_object(position)" in POSITION_SOURCE
assert "wallet_id = _get_wallet_id_for_position(position_id)" in POSITION_SOURCE
assert "await _invalidate_user_positions_cache(wallet_id)" in POSITION_SOURCE
assert "def _get_wallet_id_for_position_object(position: object | None)" in POSITION_SOURCE
assert 'getattr(position, "user_id", None)' in POSITION_SOURCE
Comment thread quantara/web_app/tests/test_user_positions_cache_static.py Fixed
assert "await _invalidate_user_positions_cache(wallet_id)" in POSITION_SOURCE
assert "def _get_wallet_id_for_position_object(position: object | None)" in POSITION_SOURCE
assert 'getattr(position, "user_id", None)' in POSITION_SOURCE
assert 'getattr(user, "wallet_id", None)' in POSITION_SOURCE
assert "def _get_wallet_id_for_position_object(position: object | None)" in POSITION_SOURCE
assert 'getattr(position, "user_id", None)' in POSITION_SOURCE
assert 'getattr(user, "wallet_id", None)' in POSITION_SOURCE
assert "if not isinstance(user_id, UUID):" in POSITION_SOURCE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache /api/user-positions 60s per wallet_id

2 participants