Skip to main content

Proxy Middleware

A proxy middleware is a kind of middleware that may skip calling the next middleware in a chain.

CachesReturnValue is a good example.

module ConvenientService
module Common
module Plugins
module CachesReturnValue
class Middleware < Core::MethodChainMiddleware
# ...
def next(*args, **kwargs, &block)
key = cache.keygen(:return_values, method, *args, **kwargs, &block)

cache.fetch(key) { chain.next(*args, **kwargs, &block) }
end
# ...
end
end
end
end
end

When the cache does NOT contain any value by key, it calls the next middleware in the chain and stores its return value.

When the cache already contains a value by key, it does NOT trigger the next middleware at all.

That is why this middleware is considered proxy middleware.

In contrast, decorator middlewares always call chain.next.

note

cache.fetch in CachesReturnValue has the same semantics as Ruby's Hash#fetch.