-
Notifications
You must be signed in to change notification settings - Fork 356
Description
I've been playing around with Tesla and I really like it. Great Work!
I am really keen on implementing a caching middleware but I'm kind of lost. By looking at the current code I don't think it's trivial.
The only solution I came up with so far is something like this (very basic).
defmodule CachingMiddleware do
@ttl = 10
def call(env, next, _options) do
case Cache.read(env.url) do
nil ->
result = Tesla.run(env, next)
Cache.write(env.url, result.body, @ttl)
result
cached_body -> %{env | body: cached_body}
end
end
end
The problem here is that it depending on the cache backend you can't serialize the elixir data structures so you can only take the body, headers and status code.
The following problem is that in the case of a cache hit, the result will be different from the cache miss returns. The only way around this is knowing about the following middlewares (tuples, json, logger) and running them manually when having a cache hit.
I have found no way in the code to set a flag that will essentially avoid calling the adapter.
Any ideas?