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

Configurable FastRoute Caching #1749

Closed

Conversation

lupo-lupov
Copy link
Contributor

Enabling FastRoute cache can be made.

There are 2 new parameters in main settings that are with same meaning like FastRoute cache aprameters, but prefixed with 'router':

  • routerCacheDisabled - turn on/off route cache (true|false)
  • routerCacheFile - file path to be cached (string - file path with writeable directory)

Cache is disabled by default

@tuupola
Copy link
Contributor

tuupola commented Jan 31, 2016

Double negatives give headache. To turn on cache you must use routerCacheDisabled = false. It would be more readable to have routerCacheEnabled = true.

@lupo-lupov
Copy link
Contributor Author

I totally agree with you, but don't you think we'll lose consistency with actual FastRoute parameters? Isn't keeping their names the same, makes it more clear, that they are passed straight to the FastRoute function?

@tuupola
Copy link
Contributor

tuupola commented Feb 1, 2016

Oh sorry. Yes you are correct, consistency is better. Did not think this through.

@@ -156,7 +158,11 @@ private function registerDefaultServices($userSettings)
* @return RouterInterface
*/
$this['router'] = function () {
return new Router;
if ($this['settings']['routerCacheDisabled']) {
Copy link
Member

Choose a reason for hiding this comment

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

This setting may not exist if someone is using their own Container.

@akrabat
Copy link
Member

akrabat commented Feb 6, 2016

Can we have unit tests for this please?

@akrabat akrabat added this to the 3.3.0 milestone Feb 6, 2016
@ghost
Copy link

ghost commented Feb 6, 2016

I'll do my best for the tests :)

What exactly do you mean

This setting may not exist if someone is using their own Container.

If I understand, if someone is using their own Container, he/she should add instance of Router (\Slim\Interfaces\RouterInterface) and if he\she wants to use cache, the way is to use public method Slim/Router::setCacheFilePath to do it.

@lupo-lupov
Copy link
Contributor Author

Sorry for my last post (it was not from my personal, but my company account).
Here are the unit tests.
Any additional proposals are appreciated.

@akrabat
Copy link
Member

akrabat commented Feb 6, 2016

What exactly do you mean

This setting may not exist if someone is using their own Container.

I mean that you can't assume that those array keys will exist in the settings array and will need to test for their existence.

@vlakoff
Copy link
Contributor

vlakoff commented Feb 8, 2016

I vote for setting name routerCacheEnabled rather than routerCacheDisabled, considering Slim doesn't cache by default, and FastRoute cachedDispatcher caches by default but has to be explicitly chosen.

@silentworks
Copy link
Member

I am not for this PR, I intentionally left our route caching from the core of the Framework due to the issues it could cause for someone who doesn't understand what it does. This would lead to us having more issues to attend to due to people enabling cache this easy. If you don't remove the cached route file on each deploy, you end up with a broken system. And for this reason I left this out and was planning to add to the documentation how to get it working along with these warnings.

@vlakoff
Copy link
Contributor

vlakoff commented Feb 8, 2016

What are the performance gains? If they are not significant I would favor simplicity too (one less thing to manage).

@lupo-lupov
Copy link
Contributor Author

Yes KISS principle.
I had a doubts, is it against slim philosophy or not...
Always dealing with cache and log directories on deployment is a little bit tricky.
Maybe it's not worth when managing light projects, but from the other side, the developer should know what he/she's doing and such option should make him think :)

@silentworks
Copy link
Member

Thought about this a bit more and I agree with you on KISS. Can you update the PR please and we will look to get this merged in for 3.3.0.

@SvenRtbg
Copy link

SvenRtbg commented Mar 2, 2016

I have two thoughts:

  1. More configuration in an array? Is there no other way?
  2. Caching the routing is great, but what I really need is a way to deploy a precompiled cache file, because I don't like writeable directories on a production server. Currently there is such a check in Slim, so in production the directory has to be writeable even though the file is already there.

@danopz danopz mentioned this pull request Mar 7, 2016
@lupo-lupov
Copy link
Contributor Author

Hi
About 1 - it's Slim style default configuration - my personal opinion is also the same (better array configurations instead of specific configuration class/object)
About 2 - you are right, I understand your case, but what if someone enable cache, but directory is not writeable... he'll never understand that actually is not using cache - any suggestions ?

@dopesong
Copy link
Contributor

dopesong commented Mar 9, 2016

Throw exception when not able to create cache file - do not check is dir writable or not. Because like @SvenRtbg said I would prefer to precompile cache file during deploy process :)

@SvenRtbg
Copy link

SvenRtbg commented Mar 9, 2016

The \FastRoute\cachedDispatcher() function does not explicitly check whether the cache file is writeable. It also does not throw an exception itself, it would probably trigger a PHP error if file_put_contents() cannot write to the cache file. However, if writing to a cache fails, it should not really affect the operation.

If Slim wants to add nicer behavior and notify the user about the problem, it should check if the cache file exists - it not, the directory should be writeable. Also, an existing cache file should be PHP code returning an array... in fact there are so many details that the FastRoute code takes care of, I think dealing with details of caching should be the task of FastRoute. If the current implementation does not correctly report the problem, send a pull request to FastRoute to deal with it more nicely, and then just use it.

@akrabat akrabat added this to the 3.5.0 milestone May 8, 2016
@akrabat akrabat removed this from the 3.4.0 milestone May 8, 2016
@akrabat akrabat closed this in 0141e08 May 8, 2016
@akrabat
Copy link
Member

akrabat commented May 8, 2016

Thank you. I rebased, which is why this doesn't look like it has been merged, even though it has been.

Note that I removed the routerCacheDisabled and allow setting routerCacheFile to false to disable FastRoute caching.

@akrabat akrabat modified the milestones: 3.4.0, 3.5.0 May 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants