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

Moving ContentBlock methods #1853

Merged
merged 1 commit into from
Dec 14, 2022
Merged

Conversation

jeremyf
Copy link
Contributor

@jeremyf jeremyf commented Nov 30, 2022

Prior to this commit there were two very similar chunks of code regarding ContentBlock.block_for type logic.

With this commit, those chunks of code are consolidated.

Why the consolidation? There's an opportunity to more aggressively cache those values.

At present, looking at the logs there are numerous singular queries of the ContentBlock structure. (See the details block below)

Through this consolidation we can now more
easily introduce a caching and invalidation layer.

One possible caching mechanism would be on initial query to load all named blocks into an in memory array; then when we update the block we invalidate the in-memory array.

The present logging does not provide insight into the cost of each of those individual queries but that is a lot of query calls (which ActiveRecord caches).

Content Blog Log Query
ContentBlock Load (0.6ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.1ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "headline_font"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_hover_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "footer_link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "footer_link_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "active_tabs_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.6ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.4ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "custom_css_block"], ["LIMIT", 1]]
Rendered shared/_appearance_styles.html.erb (52.2ms)

@samvera/hyku-code-reviewers

@jeremyf jeremyf force-pushed the jeremyf---extracting-common-logic branch from 9320fee to aa71d7e Compare November 30, 2022 20:42
Copy link
Collaborator

@kirkkwang kirkkwang left a comment

Choose a reason for hiding this comment

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

just the one extra space

describe '.block_for' do
subject { described_class.block_for(name: block_name, fallback_value: given_fallback_value) }

let(:block_name) { "blocky.mc.blockface" }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove extra space

let(:block_name) { "blocky.mc.blockface" }

looks like this getting flagged in CI

@jeremyf jeremyf force-pushed the jeremyf---extracting-common-logic branch from aa71d7e to 26b7051 Compare December 1, 2022 13:53
kirkkwang
kirkkwang previously approved these changes Dec 1, 2022
Prior to this commit there were two very similar chunks of code
regarding `ContentBlock.block_for` type logic.

With this commit, those chunks of code are consolidated.

Why the consolidation?  There's an opportunity to more aggressively
cache those values.

At present, looking at the logs there are numerous singular queries of
the ContentBlock structure.  (See the details block below)

Through this consolidation we can now more
easily introduce a caching and invalidation layer.

One possible caching mechanism would be on initial query to load all
named blocks into an in memory array; then when we update the block we
invalidate the in-memory array.

The present logging does not provide insight into the cost of each of
those individual queries but that is a lot of query calls (which
ActiveRecord caches).

<details>
<summary>Content Blog Log Query</summary>

```
ContentBlock Load (0.6ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.1ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "body_font"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "headline_font"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_hover_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "footer_link_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "footer_link_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "header_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_background_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "searchbar_text_hover_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "active_tabs_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "primary_button_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.6ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_text_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "default_button_border_color"], ["LIMIT", 1]]
ContentBlock Load (0.3ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.2ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_background_color"], ["LIMIT", 1]]
CACHE ContentBlock Load (0.0ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "facet_panel_text_color"], ["LIMIT", 1]]
ContentBlock Load (0.4ms)  SELECT  "content_blocks".* FROM "content_blocks" WHERE "content_blocks"."name" = $1 LIMIT $2  [["name", "custom_css_block"], ["LIMIT", 1]]
Rendered shared/_appearance_styles.html.erb (52.2ms)
```

</details>
@jeremyf jeremyf force-pushed the jeremyf---extracting-common-logic branch from ac18d2d to 47847c3 Compare December 14, 2022 15:45
@jeremyf jeremyf merged commit 88dae11 into main Dec 14, 2022
@jeremyf jeremyf deleted the jeremyf---extracting-common-logic branch December 14, 2022 18:54
@bkiahstroud bkiahstroud added the patch-ver for release notes label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch-ver for release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants