slimphp / slim-httpcache Goto Github PK
View Code? Open in Web Editor NEWSlim Framework HTTP cache middleware and service provider
License: MIT License
Slim Framework HTTP cache middleware and service provider
License: MIT License
Hello
Does "Slim-HttpCache" integrate with the twig cache or is an alternative to it ?
Thank you
Andrea
There is odd behaviour around cache hits, the whole idea of the 304 status is to reduce data transmission if the browser/client has data which is still accurate.
I've noticed that the response body isn't truncated defeating the purpose, technically this isn't a bug nor an implementation issue I still think it's incorrect behaviour.
While the developers can work around this by handling it in their projects, I feel like this package should handle this.
I tried it to add it to slim 4 but I get the error
Fatal error: Uncaught TypeError: Argument 2 passed to Slim\HttpCache\Cache::__invoke() must be an instance of Psr\Http\Message\ResponseInterface, instance of Slim\Routing\RouteRunner given, called in /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php on line 283 and defined in /var/www/html/vendor/slim/http-cache/src/Cache.php:53 Stack trace: #0 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(283): Slim\HttpCache\Cache->__invoke(Object(Slim\Http\ServerRequest), Object(Slim\Routing\RouteRunner)) #1 /var/www/html/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(98): class@anonymous->handle(Object(Slim\Http\ServerRequest)) #2 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(140): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous)) #3 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Http\ServerRequest)) #4 /var/www/html/vendor/slim/slim/Slim/App.php(211): Slim\MiddlewareDispatcher->handle( in /var/www/html/vendor/slim/http-cache/src/Cache.php on line 53
Took a peak into the middleware and the __invoke method didn't have the correct arguments.
So is it just not ready form slim 4 or did I missed something?
I made an attempt to make this Slim 4 compatible. Please check https://github.com/adriansuter/Slim-HttpCache/tree/patch-slim4
@l0gicgate I could make a PR against a new branch if you like.
README is not updated yet.
Readme says that this project require slim 3 or newer... It seems that this can be use with any psr7 library that can handle middleware in the same way then slim3.
Am I wrong ?
From slimphp/Slim#1275 (comment):
Can we stop coupling the ServiceProvider with the actual code for the package. I see this is becoming a pattern that others will follow. Now that we are using Container Interop, we shouldn't bind the actual package to the Pimple ServiceProviderInterface, we should create a separate class in each package to do this.
i would suggest renaming service to httpCache
as under cache
key there is often some kind of cache backends manager providing caching abilities to the application
I am trying to add caching to Slim 3, I followed the instruction in https://www.slimframework.com/docs/v3/features/caching.html :
$app = new Slim\App(['settings' => $config]);
$container = $app->getContainer();
$container['cache'] = function () {
return new \Slim\HttpCache\CacheProvider();
};
$app->add(new \Slim\HttpCache\Cache('public', 86400));
but I get the following error :
{
"message": "Slim Application Error",
"exception": [
{
"type": "RuntimeException",
"code": 0,
"message": "{} is not resolvable",
"file": "C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\CallableResolver.php",
"line": 113,
"trace": [
"#0 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\CallableResolver.php(61): Slim\CallableResolver->assertCallable(Object(Slim\HttpCache\Cache))",
"#1 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\CallableResolverAwareTrait.php(41): Slim\CallableResolver->resolve(Object(Slim\HttpCache\Cache))",
"#2 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\DeferredCallable.php(50): Slim\DeferredCallable->resolveCallable(Object(Slim\HttpCache\Cache))",
"#3 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))",
"#4 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))",
"#5 C:\Users\Mima\Desktop\file\app\server\src\classes\middlewares\TrailingSlash.php(29): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#6 [internal function]: App\Middleware\TrailingSlash->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#7 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(App\Middleware\TrailingSlash), Array)",
"#8 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#9 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#10 C:\Users\Mima\Desktop\file\app\server\src\public\index.php(1047): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#11 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#12 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)",
"#13 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#14 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))",
"#15 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#16 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#17 C:\Users\Mima\Desktop\file\app\server\src\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#18 C:\Users\Mima\Desktop\file\app\server\src\public\index.php(1059): Slim\App->run()",
"#19 {main}"
]
}
]
}
Hi, Am trying to get the slim cache provider to work on controller methods but am not able to reach a solution. I have added the HTTP cache provider from composer following the steps from this Slim Page and even added it through dependency injection. Then I have a method that needs to return a JSON response with cache expires to my client app. I have done the tests in Postman and am getting the "Slim application error" Here is my settup>>
$app=new Slim\App(); $app->add(new \Slim\HttpCache\Cache('public', 86400)); //require dependencies require './app/dependencies.php'; //require routes require './app/routes.php'; $app->run();
here is the dependencies file>>
<?php $container=$app->getContainer(); $container['cache'] = function () { return new \Slim\HttpCache\CacheProvider(); }; $container['UserController']= function (){ return new UserController(); }; $container['SongsController']= function (){ return new SongsController(); };
Here is part of the routes file>>
$app->get('/login', 'UserController:login'); $app->get('/songs/all[/{offset}]', 'SongsController:all');
And finally the method in SongsController that should return the result with cache is this>>
` public function all($request, $response, $args){
$res=array();
$uid= $this->authToken();
if ($uid){
//proceed
$body=$request->getQueryParams();
$offset=isset($body['offset']) ? $body['offset'] : 0;
$res['success']=1;
$res['messages']="Songs retrieved";
$res['uid']=$uid;
$res['audios']= $this->getAllAudios($offset);
}else{
$res['success']=3;
$res['message']="Access denied! Please login to proceed";
}
$response->withJson($res);
$newResponse = $this->cache->withExpires($response, time() + 3600);
return $newResponse;
}`
Hey,
Nice to see your cache middleware. I was looking at the code, and find it also suits Conduit and may be others which are PSR-7 framework.
Some questions / concerns :
1 . I think the CacheProvider is mainly for Pimple . May be we can move that also to require-dev ?
As I am not so closely watching Slim, ( even I am watching) I may have missed whether you said of splitting the PSR-Http of Slim. Any plans of splitting to standalone component ?
Thank you
I cant seem to find any cache documentation at all for Slim 4 https://www.slimframework.com/docs/v4/
Is this Slim-HttpCache built into Slim 4 now?
If not, how do i use this HttpCache? Really struggaling without docs.
In order to check the coverage automatically, I would suggest to add this repo to coveralls (like all the other Slim repos).
When Travis tests on php version 7 it installs a newer version of phpunit.
This results in the error PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/travis/build/slimphp/Slim-HttpCache/tests/CacheProviderTest.php on line 7
(example)
This happens because phpunit version 6 released on 2017-02-03 is namespaced. You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
I wonder if the current implementation of the HTTP Cache is really performant. To me, it looks like the cache middleware creates the whole response first and then compares the Last-Modified
and ETag
header of the response with the request to determine if a 304
status code should be returned. Therefore the complete response is created but will not be used, since Slim will not output the body if the status is 304
.
In Slim 2 the abort
method was used to immediately stop the execution as soon as the Last-Modified
/ETag
were set (assuming the client side cache was still valid). I think it would also be a good idea for Slim 3 to return the 304
response as soon as possible. This will make the usage a bit more verbose and not automagically, but will probably improve CPU/memory usage and response times.
For example, the current checks in the Cache middleware could be moved to a new method in the cache provider (e.g. isStillValid()
):
$app->get('/foo', function ($req, $res, $args) {
$resWithEtag = $this['cache']->withEtag($res, 'abc');
if ($this['cache']->isStillValid($req, $res)) {
return $resWithEtag->withStatus(304); // or even another convenience method
}
// ... some intensive calls to create the response
$resWithEtag->write($body);
return $resWithEtag;
});
Can we get a new release? Mainly for this change: https://github.com/slimphp/Slim-HttpCache/blob/master/composer.json#L20
Response header:
Cache-Control:no-store, no-cache, must-revalidate
Cache-Control:public, max-age=86400
ETag:"default_css"
then the client asks it again, whitout the etag.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.