Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first class component cache #2126

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

reeganviljoen
Copy link
Collaborator

@reeganviljoen reeganviljoen commented Oct 8, 2024

closes #234

What are you trying to accomplish?

Add a caching mechanism that is true to the view_component ethos namely

  • caching that respects local state and does not have global state as a dependency
  • caching that can easily debugged with standard debugging tools like breakpoints

What approach did you choose and why?

I chose to add a caching macro that takes in method names that are to be used to compile a cache digest, that digest is then used to cache the contents of the call method

Anything you want to highlight for special attention from reviewers?

This is a first effort at getting first class cache for VIewComponent if this is accepted I plan to add more featires like support for procs, if options, etc

@reeganviljoen reeganviljoen changed the title add component controlled cache Add First class component cache Oct 8, 2024
@reeganviljoen reeganviljoen changed the title Add First class component cache Add first class component cache Oct 8, 2024
@reeganviljoen
Copy link
Collaborator Author

@joelhawksley @Spone @camertron I would appreciate any thoughts on this.

Copy link
Member

@joelhawksley joelhawksley left a comment

Choose a reason for hiding this comment

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

@reeganviljoen THANK YOU for taking a crack at building something functional to address the long-overdue need for caching in ViewComponent.

To be honest with you, I have not used caching in Rails nearly enough to feel like I can properly assess this PR in terms of how it would integrate with the existing mental model of caching in Rails. I'm happy to learn that context, but I won't have the space to do so for at least a couple of weeks.

@Spone @boardfish have y'all done much with Rails' caching? @reeganviljoen if you know of anyone else in the community who would be a good reviewer here, I'd be happy to have them.

@reeganviljoen
Copy link
Collaborator Author

@reeganviljoen THANK YOU for taking a crack at building something functional to address the long-overdue need for caching in ViewComponent.

To be honest with you, I have not used caching in Rails nearly enough to feel like I can properly assess this PR in terms of how it would integrate with the existing mental model of caching in Rails. I'm happy to learn that context, but I won't have the space to do so for at least a couple of weeks.

@Spone @boardfish have y'all done much with Rails' caching? @reeganviljoen if you know of anyone else in the community who would be a good reviewer here, I'd be happy to have them.

@joelhawksley I really aprreciate the honesty, I will see if I can find anyone with cache experience to take a crack at reviewing this ❤️

@boardfish
Copy link
Collaborator

@joelhawksley Afraid it's not something I've really interacted with directly until now, sorry! I'd be coming from a similar place. Great to see @reeganviljoen working at it, though ✨

lib/view_component/base.rb Outdated Show resolved Hide resolved
lib/view_component/base.rb Outdated Show resolved Hide resolved
lib/view_component/base.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@JWShuff JWShuff left a comment

Choose a reason for hiding this comment

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

Reegan, really excited to see this. I'm going to take a shot at adapting our gem spec suite to your fork and open a PR at your fork with those specs by way of giving us a place to start that gives us the most cache functionality we can get.

@@ -34,6 +35,7 @@ def config
include ViewComponent::Slotable
include ViewComponent::Translatable
include ViewComponent::WithContentHelper
include ViewComponent::Cacheable
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion, nitpick and nonblocking: Can we slide this in alphabetically above? I'd think it'd be ~ line 32

__vc_cache_dependencies.push(*args)
end

def inherited(child)
Copy link
Contributor

Choose a reason for hiding this comment

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

We took this for a spin in a few cases of our cached partials and view components that we are familiar with and largely it works for the base cases. This is working to correctly bust a cached VC when it changes or things in the render path 'above' (partials or VCs) it change.

What we aren't seeing is when a VC renders another VC or partial as a child that downstream changes of the cached VC pick up changes and handle it. I'm going to fork your branch and offer some test cases based on an approach we use in the vc fragment caching gem (what we're currently solving this with)

Effectively we want some way for a child partial or VC change (in either the .rb or template) to bust the cached parent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JWShuff like touch: true in rails ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

also big thanks for testing it, I can look at adding the touch true thing later this week

Copy link
Contributor

@JWShuff JWShuff left a comment

Choose a reason for hiding this comment

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

Added a hypothetical approach to an integration test for the behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

@reeganviljoen I am not a git-wizard, much to my eternal shame, but I set up a fork and adjusted the spec approach here to use the integration examples/controllers to assert the behavior. Feel free to take, leave, or otherwise.

JWShuff#1

There's a wider challenge around partial/template digesting, and on a quiet day I'll port our spec suite over to the VC style and get it implemented so we have all the permutations we know of that need to cache appropriately.

@reeganviljoen
Copy link
Collaborator Author

@JWShuff I investigated a few of your suggestions, added a few, thanks, and others I am struggling with
luckily I think these aren't required for the initial feature and are more nice to haves that can be implemented in another pr(child cache busting)

@reeganviljoen
Copy link
Collaborator Author

@joelhawksley with a live use case tested how do you feel about this now

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.

Components should play nicely with Rails caching mechanisms
9 participants