cached_value icon indicating copy to clipboard operation
cached_value copied to clipboard

[Request] Add large cache option

Open cadaniel opened this issue 3 years ago • 2 comments

So I really like where this package is going.

Something I'd like to see is to have dependant cache hold multiple values.

I'd like to build a cache of multiple values, and only compute if that value is new.

For example

string getName(int id) {
    switch(id) {
        case 0: 
            return "zero";
       case 1:
            return "one";
      ....
    }
} 

... 
int id = 0;
final factorialCacheFirst = CachedValue.dependent(
    on: () => id,
    compute: () => getName(id),
  ); // computes

final factorialCacheSecond = CachedValue.dependent(
    on: () => id,
    compute: () => getName(id),
  ); // returns cached value

id = 1;
final factorialCacheThird = CachedValue.dependent(
    on: () => id,
    compute: () => getName(id),
  ); // computes

id = 0;
final factorialCacheSecond = CachedValue.dependent(
    on: () => id,
    compute: () => getName(id),
  ); // returns cached value

id = 1;
final factorialCacheThird = CachedValue.dependent(
    on: () => id,
    compute: () => getName(id),
  ); // computes

The summary is I'd love to cache a function's return value for a variety of inputs.

cadaniel avatar May 03 '21 21:05 cadaniel

Your example is a little bit off but I think I get it.

This generally evolves keeping a cache map of dependency and results.

renancaraujo avatar May 04 '21 12:05 renancaraujo

The riverpod family provider is really handy to use for that use-case, until we implement your suggestion here (might not happen).

spydon avatar Jul 15 '24 17:07 spydon