-
Notifications
You must be signed in to change notification settings - Fork 5
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 new CacheStrategy & CacheMiddleware classes #3
Conversation
addcd13
to
22d148b
Compare
} | ||
|
||
protected function should_bypass_cache( RequestInterface $request ) { | ||
$bypass = $request->getHeader( self::HEADER_BYPASS_CACHE ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can inspect the request here to e.g. put tighter scrutiny on POST
requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g. We discussed inspecting the GraphQL operation for mutations and setting those to not cache.
Additionally, we discussed using a method on e.g. the QueryContext
that we leverage to bypass the cache.
use Psr\Http\Message\RequestInterface; | ||
use RemoteDataBlocks\Logging\LoggerManager; | ||
|
||
class CacheStrategy extends GreedyCacheStrategy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth s/CacheStrategy
/CustomCacheStrategy
?
Just thinking, in other context, like when instantiated, some sort of qualifier like that might convey more meaning. And RemoteDataBlocksCacheStrategy
feels a tad long even for me (though I'm open to it!).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the PHP namespace will suffice for now to indicate these are bespoke & not from the composer lib. If / when we have more than one implementation of the strategy / middleware classes, that's probably a good time to be more specific about the naming, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up prefixing these with Rdb
-- feel free to push a change if anyone has a preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, didn't test.
It would be nice to have unit tests on the cache strategy class. Maybe take Cursor for a spin on that if you haven't tried it yet? I suspect it'd do nicely with a class that's so direct as that one.
$uri_string = $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(); | ||
$method = $request->getMethod(); | ||
$body = $request->getBody()->getContents(); | ||
return $method . ' ' . $uri_string . ' ' . $body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safe to log the content of the $body
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safe to log the content of the $body?
In production, we'd not want to do this for sure. Since we're just logging to the debug log, it's probably not all that critical. At least for now as we debug the middleware and strategy.
$logger = LoggerManager::instance(); | ||
|
||
if ( $this->should_bypass_cache( $request ) ) { | ||
$logger->debug( 'Bypassing cache: ' . self::getRequestString( $request ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have any prefix in debugging messages which will make the search easier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pass a string to LoggerManager::instance()
, it accepts a namespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriszarate Seems like the Logger
constructor / create
fn does, but not the static LoggerManager::instance
fn:
remote-data-blocks/inc/logging/logger-manager/logger-manager.php
Lines 27 to 33 in 01eaa59
public static function instance(): Logger { | |
if ( null === self::$instance ) { | |
self::$instance = Logger::create( self::$log_namespace ); | |
} | |
return self::$instance; | |
} |
...unless I'm missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not just use the Logger::create
fn here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. I guess we already have a namespace and don't really need one? Or we want multi-level namespaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @mehmoodak's suggestion was for multi-level namespaces to be able to more-easily identify that the debug log came from e.g. the HTTP Client module (similar to how the debug npm package allows multi-level prefixing / filtering.
We could drill in via Query Monitor like this:
I think I'll file it to an issue for an enhancement outside this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on @mhsdef suggestion of testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to tests for these. I wanted to make sure folks agreed w/ the direction before writing them. I'll see how feasible it is.
use Psr\Http\Message\RequestInterface; | ||
use RemoteDataBlocks\Logging\LoggerManager; | ||
|
||
class CacheStrategy extends GreedyCacheStrategy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the PHP namespace will suffice for now to indicate these are bespoke & not from the composer lib. If / when we have more than one implementation of the strategy / middleware classes, that's probably a good time to be more specific about the naming, IMO.
$uri_string = $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(); | ||
$method = $request->getMethod(); | ||
$body = $request->getBody()->getContents(); | ||
return $method . ' ' . $uri_string . ' ' . $body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safe to log the content of the $body?
In production, we'd not want to do this for sure. Since we're just logging to the debug log, it's probably not all that critical. At least for now as we debug the middleware and strategy.
} | ||
|
||
protected function should_bypass_cache( RequestInterface $request ) { | ||
$bypass = $request->getHeader( self::HEADER_BYPASS_CACHE ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g. We discussed inspecting the GraphQL operation for mutations and setting those to not cache.
Additionally, we discussed using a method on e.g. the QueryContext
that we leverage to bypass the cache.
$logger = LoggerManager::instance(); | ||
|
||
if ( $this->should_bypass_cache( $request ) ) { | ||
$logger->debug( 'Bypassing cache: ' . self::getRequestString( $request ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
with debug logging
Refactors & fixes to make tests pass
5495e0a
to
8ae5f3b
Compare
… add/guzzle-caching-strategy
@@ -11,6 +11,8 @@ | |||
use GuzzleHttp\Exception\ConnectException; | |||
use GuzzleHttp\Psr7\Request; | |||
use GuzzleHttp\Exception\RequestException; | |||
use Kevinrob\GuzzleCache\Storage\VolatileRuntimeStorage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this Kevinrob
person has some good adjectives, A++
… add/guzzle-caching-strategy
… add/guzzle-caching-strategy
… add/guzzle-caching-strategy
… add/guzzle-caching-strategy
/** | ||
* @var array<string, bool> | ||
*/ | ||
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase, SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are needed to comply with the parent class' declaration
@chriszarate want to give this another once over? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, just had one suggestion (to improve my own code) :)
… add/guzzle-caching-strategy
…emote-data-blocks into add/guzzle-caching-strategy
POST
to the request methods that we can cache, but default to not caching themget_cache_ttl
methodfetch
&cache
remote_data_blocks_bypass_cache
filterThis isn't perfect yet, but it gives us a jumping-off point for fine-tuning our caching strategy.