fuels-ts icon indicating copy to clipboard operation
fuels-ts copied to clipboard

Evaluate Global Resources Cache Within Provider Instances

Open Torres-ssf opened this issue 1 year ago • 3 comments

The current implementation of the resources cache, managed by the Provider instance, utilizes a global cache.

This design ensures that the cache is shared among all Provider instances within the same dApp. This approach was chosen due to the possibility that multiple providers may be instantiated from different parts of the dApp, and transactions can be submitted from these various instances using resources owned by the same address.

While the global cache design has its advantages, it also introduces some unintentional issues:

  1. Each Provider instance can set its cache Time-To-Live (TTL), leading to confusion since the actual cache is shared among all instances.
  2. Developers might expect each Provider instance to manage its own cache independently, leading to potential misunderstandings.

Given the potential unintentional problems, this issue should serve as a reminder to continuously evaluate if another approach is more suitable.

Torres-ssf avatar Aug 07 '24 14:08 Torres-ssf

Should we make the setter for the TTL a static method on the provider class?

arboleya avatar Aug 08 '24 16:08 arboleya

@arboleya yes, if we're going to make the cache static then we should make the TTL setter static as well.

public class Provider {
  static cache?: ResourceCache = new ResourceCache(20000);
  
  static updateCacheTtl(ttl: number) {
    if (ttl === -1) { this.cache = undefined; return; }
    const activeData = this.cache?.getActiveData();
    // activeData as the second argument is used to invalidate the data based on the new ttl
    this.cache = new ResourceCache(ttl, activeData);
  }
}

I'm not sure about the name of updateCacheTtl but that's the idea.

nedsalk avatar Aug 08 '24 18:08 nedsalk

@Torres-ssf Regarding the cache being public, I know that you had some reasoning behind it but I can't remember it... What was it?

nedsalk avatar Aug 08 '24 18:08 nedsalk

Closing this as not planned based on investigations

See more here

maschad avatar Dec 11 '24 15:12 maschad