anchor.diff:      ok.
   
   fetch_history(ProviderID, Callback, From) ->
  -    To = worker_host:state_get(?MODULE, last_seq),
  -    ?info("History fetch ~p", [[ProviderID, Callback, From, To]]),
  +    NewestCached = newest_cached_seq(),
  +    OldestCached = oldest_cached_seq(),
  +    ?info("Fetching from cache; {provider:~p, from:~p, cache_start:~p, cache_end:~p}",
  +        [ProviderID, From, OldestCached, NewestCached]),
  +
  +    case {OldestCached, From >= OldestCached} of
  +        {cache_empty, _} ->
  +            fetch_from_db(From, last_seq(), ProviderID, Callback);
  +        {_, true} ->
  +            fetch_from_cache(From, NewestCached, ProviderID, Callback);
  +        {_, false} ->
  +            fetch_from_cache(From, NewestCached, ProviderID, Callback),
  +            fetch_from_db(From, OldestCached - 1, ProviderID, Callback)
  +    end.
  +
  +
  +fetch_from_cache(From, To, ProviderID, Callback) ->
  +    Cache = worker_host:state_get(?MODULE, cache),
  +    CachedList = gb_trees:values(Cache),
  +    Shifted = lists:dropwhile(fun({Seq, _}) -> Seq < From end, CachedList),
  +    Trimmed = lists:takewhile(fun({Seq, _}) -> Seq =< To end, Shifted),
  +    lists:foreach(fun({Seq, {Doc, Type}}) ->
  +        handle_change(Seq, Doc, Type, #{ProviderID => {Callback, infinity}})
  +    end, Trimmed).
  +
  +fetch_from_db(From, To, ProviderID, Callback) ->
       couchdb_datastore_driver:changes_start_link(fun
           (_Seq, stream_ended, _Type) -> ok;
           (Seq, Doc, Type) -> handle_change(Seq, Doc, Type, #{ProviderID => {Callback, infinity}})
  
positive.code: start_changes_stream(StartSeq) ->
      couchdb_datastore_driver:changes_start_link(fun common_change_callback/3, StartSeq, infinity).
  
  -spec get_last_seq() -> non_neg_integer().
  get_last_seq() ->
      try
          {ok, LastSeq, _} = couchdb_datastore_driver:db_run(couchbeam_changes, follow_once, [], 30),
          binary_to_integer(LastSeq)
      catch
          E:R -> ?error("Last sequence number unknown (assuming 1) due to ~p:~p", [E, R]), 1
      end.
  
  get_providers(_Seq, Doc, space) ->
      #document{value = Value} = Doc,
  
negative.code: 
  -spec common_change_callback(Seq :: non_neg_integer(), datastore:document() | stream_ended, model_behaviour:model_type() | undefined) -> ok.
  common_change_callback(_Seq, stream_ended, _Type) ->
      CurrentSeq = newest_cached_seq(),
      ?error("Changes stream broken - restarting at ~p", [CurrentSeq]),
      start_changes_stream(CurrentSeq),
      ok;
  
  common_change_callback(Seq, Doc, Type) ->
      cleanup_expired_callbacks(),
      update_cache(Seq, Doc, Type),
      Callbacks = worker_host:state_get(?MODULE, provider_callbacks),
      handle_change(Seq, Doc, Type, Callbacks).
  
  handle_change(Seq, Doc, Type, Callbacks) ->
      NoOp = fun(_Seq, _Doc, _Type) -> ok end,
      Providers = get_providers(Seq, Doc, Type),
      ?info("Dispatching {seq=~p, doc=~p, type=~p} to ~p having ~p", [Seq, Doc, Type, Providers, Callbacks]),
  
      lists:foreach(fun(Provider) ->
          {Callback, _} = maps:get(Provider, Callbacks, NoOp),
          spawn(fun() -> Callback(Seq, Doc, Type) end)
      end, Providers).
  
  cleanup_expired_callbacks() ->
      worker_host:state_update(?MODULE, provider_callbacks, fun(Callbacks) ->
          Now = now_seconds(),
          maps:filter(fun(_ID, {_Callback, ExpiresAt}) ->
  