docbrown/app/services/edge_cache
Jeremy Friesen 6e81773319
Favoring explicit class declaration (#17042)
In helping track down forem/forem#17041 I was looking at our cache
busting logic.  We were looking up a constant via the `.const_get` call
from a string already defined inline.  This refactor short-circuits
naming a string, capitalizing it, then looking in the class's registered
constants.

There's also a subtle bug in the original; When `provider` equals
"another_cache", the `#capitalize` method would return
`"Another_cache"`, whereas `#classify` will return `"AnotherCache"`.

Below are some benchmarks for the change.

```ruby
require "benchmark"

module EdgeCache
  class Bust
    def by_class
      Fastly
    end

    def by_const_get
      self.class.const_get("fastly".classify)
    end
  end
end

Benchmark.bmbm do |x|
  x.report("by_class") do
    1000.times do
      EdgeCache::Bust.new.by_class
    end
  end
  x.report("by_const_get") do
    1000.times do
      EdgeCache::Bust.new.by_const_get
    end
  end
end
```

```shell
> bin/rails runner /Users/jfriesen/git/forem/bench.rb

Rehearsal ------------------------------------------------
by_class       0.001239   0.000186   0.001425 (  0.001342)
by_const_get   0.011398   0.000136   0.011534 (  0.011538)
--------------------------------------- total: 0.012959sec

                   user     system      total        real
by_class       0.000973   0.000030   0.001003 (  0.000969)
by_const_get   0.011102   0.000032   0.011134 (  0.011104)
```
2022-03-29 13:52:11 -04:00
..
bust Remove fastly http purge feature flag and conditional behavior (#16903) 2022-03-18 09:10:43 -05:00
bust.rb Favoring explicit class declaration (#17042) 2022-03-29 13:52:11 -04:00
bust_article.rb Optimize Article query in EdgeCache::BustArticle (#14916) 2021-10-05 11:58:34 -04:00
bust_comment.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_commentable.rb Remove Elasticsearch ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ ✂️ (#13606) 2021-05-03 11:09:45 -04:00
bust_listings.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_organization.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_page.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_podcast.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_podcast_episode.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_sidebar.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_tag.rb Refactor cache purging (#12811) 2021-03-02 10:09:05 -05:00
bust_user.rb When busting user cache, also clear the api response (#13465) 2021-04-22 09:16:15 -05:00