ZupIT/beagle

[EPIC] - Remove Beagle Cache and implement extension point

Closed this issue · 0 comments

Context

Beagle has implemented a generic per-endpoint caching system by default. BFF creates a hash based on the content of each endpoint and whenever a new deployment is made, it warns clients that the cache is invalid and needs to be updated. This control is done by sending statusCode 304 in the response to requests.

In Android and iOS applications it's possible to configure the cache behavior, defining the maximum size of the cache on disk and in memory and the maximum lifetime of the cache. On the Web, caching works differently through a set of pre-defined strategies. This cache can only be enabled or disabled for all platforms.

Problem

Providing a default cache implementation has the following limitations:

  • The cache is only updated in a new BFF deploy;
  • Caching is made by endpoint, with no customization option;
  • Implementation is relatively complex for functionality purposes, making maintenance difficult.

This implementation also goes against the Beagle principle of having an open contract that is easy to implement on any technology. And also for an application to use a cache that serves it well, it must be strongly linked to its business rule.

Proposition

The suggestion is to remove all the cache code from the Backend, Android, iOS and Web applications and provide an extension point for the user to implement the cache as needed and to handle the prefetch for navigations with the shouldPrefetch property enabled.

For that, a contract must be created with two methods which will be invoked by the Beagle core whenever a component request occurs to control cache and prefetch. The ViewClient will be this contract and it will have a fetch and a prefetch method, receiving the request's RequestData as parameter and will return a ResponseData class.

interface ViewClient {
    fun fetch(requestData: RequestData): ResponseData
    fun prefetch(requestData: RequestData): ResponseData
}

The parameter and response of the method fetch may change between the platforms. The important thing is that the behavior stays the same.

This way, the user is free to process the request before and/or after it is executed, for example, by caching that request based on the logged in user. Beagle will have a default ViewClient implementation that only makes requests.

Important

  • By removing the cache we have no need for local storages, i.e. we can remove the BeagleStorage (Web), StoreHandler (Android) and CacheManagerProtocol (iOS);
  • Only requests to load views such as navigations, LazyComponent requests and start server driven flows pass through this contract;
  • Requests in progress will not be used for prefetch, this behavior can be added in the future.

Tasks

Remove cache and implement extension point on:

  • Android
  • iOS
  • Web
  • Flutter
  • Backend

It is also necessary to update the documentation:

  • Update cache documentation
  • Create migration guide