Giter Site home page Giter Site logo

slim-httpcache's People

Contributors

adoy avatar adriansuter avatar akrabat avatar bcremer avatar codeguy avatar craveica avatar dependabot-preview[bot] avatar dependabot[bot] avatar designermonkey avatar dhoffend avatar dopesong avatar geggleto avatar jerfeson avatar l0gicgate avatar silentworks avatar tflight avatar zither avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slim-httpcache's Issues

Twig cache integration

Hello

Does "Slim-HttpCache" integrate with the twig cache or is an alternative to it ?

Thank you

Andrea

Body is still sent on 304

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.

RFC-9110
MDN 304

Problem with slim 4

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?

Slim requirement

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 ?

Unbind the Service Provider from the main code

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.

rename service

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

{} is not resolvable

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}"
]
}
]
}

http cache method on controller methods

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;
}`

Welcome middleware

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

Add this repo to coveralls

In order to check the coverage automatically, I would suggest to add this repo to coveralls (like all the other Slim repos).

Need to specify a target version for phpunit?

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

Abort execution early if client side cache is still valid

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;
});

does not work

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.