Giter VIP home page Giter VIP logo

Comments (3)

ksassnowski avatar ksassnowski commented on May 30, 2024

That would be pretty tricky to implement because it would essentially break the entire downloader pipeline.

Right now, each request is run through each RequestMiddleware first, so that any middleware can alter or drop the request before it gets sent. Then, the response is passed through each ResponseMiddleware. Adding a kind of middleware that takes a request and immediately returns a response would completely break this pattern, unfortunately.

A possible way to deal with this could be, extending the Request class to allow setting a Response on the request. The downloader could then check if the request already has a response set and simply return that response instead of sending the request. I think this is similar to how Symfony's kernel events work, too.

You could then implement a caching middleware by simply setting the cached response on the request inside a regular RequestMiddleware. The hypothetical API could look something like this:

class CachingMiddleware implements RequestMiddlewareInterface
{
    public function handleRequest(Request $request): Request
    {
        // assuming the cache returns `null` if no cached response exists, it's safe
        // to always set the result of `forRequest` on the request.
        $cachedResponse = $this->cache->forRequest($request);
        
        return $request->setResponse($cachedResponse);
    }
}

This would still allow the other middlewares to run as usual which is something I definitely want to preserve.

from core.

aaronflorey avatar aaronflorey commented on May 30, 2024

That's a great way of doing it, i'll work on adding that, Thanks!

from core.

Related Issues (20)

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.