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)
```
* Extract repeated calls to cache_bust.call to iteration over list
This mirrors what the EdgeCache::BustUser service looks like.
It might make sense to have cached_article_paths be a method accepting
a block and yielding paths, and including the collections cache paths
as well.
* Move article path information into a separate method
BustArticle.call now only knows to bust cache for each path associated
with the article, the information about which paths to clear is moved
to a separate `paths_for` method.
I may have gone overboard on this.
* Remove extra blank line
* When busting user cache, also clear the api response
This should fix#13293 by clearing both the UI copy and the api copy of the
user's page.
* Update spec to include the api path
* This change abstracts the DatadogStatsClient into a ForemStatsClient.
The purpose of this abstraction is to set the foundation for a subsequent PR that will allow one to use New Relic for recording Forem stats, instead of Datadog, if there is a New Relic configuration found.
This specific change creates an abstraction layer that can be built upon, without changing any actual default behavior. All specs still pass.
* Use delegate instead of explicit methods.
* Delegate instead of explicit methods.
* Fix the error.
* Refactor according to the suggestions in the comments.
* Ooops. Stats work better when all of the code is committed.
* Removing the alias of count to increment since that was done in error.
* Create bust_page service and spec
* Specify spec with .once
* Create bust_tag service and spec
* Create bust_event service and spec
* Create bust_podcast service and spec
* Add bust_article service
* Make timestamps lambdas
* Remove nil safe operators
* Updated bust_article specs
* Add spec for TIMEFRAMES and relative time
* Refactor edge caching logic to live in EdgeCache::Service
* Rename EdgeCache::Service to EdgeCache::Buster
* Reorganize some code, move from Buster to Bust naming
* Turn CacheBuster into a module
This class used no internal state, so repeatedly creating short-lived objects seems wasteful.
* Consistently use string interpolation and parenthesis
* Destructure arrays into meaningful names, formatting
* Fix request spec for internal classified listings controller
Interestingly this works when asserting directly on the module, but not on a double.
Asserting directly in the module seems sufficient for this test so the indirection
was removed.
* Turn CacheBuster into a module
This class used no internal state, so repeatedly creating short-lived objects seems wasteful.
* Fix specs after rebasing