Skip to content

Conversation

@ruhz3
Copy link
Contributor

@ruhz3 ruhz3 commented Nov 11, 2025

Description

This PR changes the default value of the cache parameter in ComputedVar from True to False to align with the documented behavior at https://reflex.dev/docs/vars/computed-vars#cached-vars.

Background

According to the documentation:

  • @rx.var should recompute on every state change (cache=False behavior)
  • @rx.var(cache=True) should only recompute when dependencies change (cached behavior)

However, the current implementation has cache=True as the default in the computed_var() function and ComputedVar.__init__(), which contradicts the documentation.

Changes

Updated the default cache parameter to False.

Related Issues

This change makes the code consistent with the documentation. As a result, PR in reflex-web (which updates the documentation to state that the default is True) should be declined, as the code now correctly implements cache=False as the default behavior.

Breaking Change

Existing computed vars that relied on the default caching behavior will no longer be cached automatically. Users who want caching enabled must now explicitly set @rx.var(cache=True).

Migration Guide

If you have existing code using computed vars that depends on caching:

# Before (implicitly cached)
@rx.var
def my_computed_var(self) -> str:
    return expensive_computation()

# After (explicitly cache)
@rx.var(cache=True)
def my_computed_var(self) -> str:
    return expensive_computation()

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 11, 2025

Greptile Overview

Greptile Summary

This PR changes the default caching behavior for computed variables from True to False to align the implementation with Reflex's documented behavior. Previously, the ComputedVar class and computed_var() function defaulted to caching computed values, meaning they would only recompute when their dependencies changed. The documentation, however, states that @rx.var should recompute on every state change by default, with caching only enabled when explicitly requested via @rx.var(cache=True).

The change updates five locations in reflex/vars/base.py where the cache parameter default is changed from True to False. This ensures that computed variables now behave as documented: they recompute on every state change unless explicitly configured to cache. This is a fundamental behavioral change that affects how computed variables perform in Reflex applications, making them more predictable and consistent with user expectations based on the documentation.

Important Files Changed

Filename Score Overview
reflex/vars/base.py 4/5 Changed default cache parameter from True to False in ComputedVar.init() and all computed_var() function overloads

Confidence score: 4/5

  • This PR introduces a significant breaking change but is well-documented and necessary for consistency
  • Score reflects the breaking nature of the change and potential impact on existing applications that rely on implicit caching behavior
  • Pay close attention to the impact on existing applications using computed vars without explicit cache parameters

Sequence Diagram

sequenceDiagram
    participant User
    participant State
    participant ComputedVar
    participant Cache
    
    User->>State: "@rx.var decorator on method"
    State->>ComputedVar: "__init__(cache=False by default)"
    ComputedVar->>ComputedVar: "Set _cache = False"
    ComputedVar->>State: "Return ComputedVar instance"
    
    User->>State: "Access computed var property"
    State->>ComputedVar: "__get__(instance, owner)"
    
    alt cache=False (new default)
        ComputedVar->>ComputedVar: "Call fget() directly"
        ComputedVar->>ComputedVar: "No caching logic"
        ComputedVar->>State: "Return fresh computed value"
    else cache=True (explicit)
        ComputedVar->>Cache: "Check if cached value exists"
        alt Cache hit
            Cache->>ComputedVar: "Return cached value"
        else Cache miss
            ComputedVar->>ComputedVar: "Call fget()"
            ComputedVar->>Cache: "Store result in cache"
            ComputedVar->>State: "Return computed value"
        end
    end
    
    State->>User: "Return computed value"
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@ruhz3
Copy link
Contributor Author

ruhz3 commented Nov 11, 2025

reflex-dev/reflex-web#1687
Only one of the two PRs should be merged.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 11, 2025

CodSpeed Performance Report

Merging #5968 will not alter performance

Comparing ruhz3:main (6deb5ee) with main (3c3ddc2)

Summary

✅ 8 untouched

@masenf
Copy link
Collaborator

masenf commented Nov 11, 2025

the intention is for cache=True to be the default

@masenf masenf closed this Nov 11, 2025
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.

2 participants