0

I am working on an application that involves a lot of image caching, to reduce database costs. I am using Cached_network_image with Flutter_Cache_Manager.

My usage :

SizedBox(
  width: 75,
  height: 75,
  child: AspectRatio(
    aspectRatio: 16/13,
    child: Container(
      decoration: BoxDecoration(
        color: colorProvider.fourthColor,
        borderRadius: BorderRadius.circular(5),
      ),
      clipBehavior: Clip.hardEdge,
      child: query["thumbnail"].trim().isNotEmpty ? CachedNetworkImage(
        imageUrl: query["thumbnail"].trim(),
        fit: BoxFit.cover,
        cacheManager: CacheManager(
          Config(
            '${query["thumbnail"].trim()}',
            stalePeriod: const Duration(hours: 36),
          ),
        ),
        fadeInDuration: const Duration(milliseconds: 10),
        errorWidget: (context, url, error) => Center(
          child: Icon(Icons.error, size: 24, color: colorProvider.secondText),
        ),
      ) : Center(child: Icon(Icons.error, size: 24, color: colorProvider.secondText,),),
    ),
  ),
),

Here I am caching the image for 36 hours, and after 36 hours, the image is removed from the cache. But, I want to extend the stalePeriod to 36 hours whenever a user views the image.

For example, if an image is cached and an hour has remained before it's removed from the cache. If the user views it again, I want to change the cached image's live time from an hour back to 36 hours.

Every bit of help would be much appreciated, for this is very crucial to my app.

1 Answer 1

0

Directly modifying the existing cached image's expiration time is not possible in most caching libraries. One idea would be to recach the image with all network costs. Another idea could be to implement a custom invalidation mechanism within your caching library. This could involve: Storing an additional metadata field with the cached image indicating its original expiration time. When the image is viewed, update the metadata field with the new expiration time (36 hours from the current time). During cache cleanup, check the metadata field and only remove images that have exceeded their original expiration time, not the updated one. This approach requires modifying the caching library or implementing a custom caching solution, which can be more complex. Good Luck.

Sign up to request clarification or add additional context in comments.

3 Comments

Can't we just re-cache the image that's in the cache, whenever user views it. Without fetching it from the database?
"The cache knows when files have been used latest. When cleaning the cache (which happens continuously), the cache deletes files when there are too many, ordered by last use, and when files just haven't been used for longer than the stale period." (pub.dev/packages/…)
In other words: By retrieving a cached image you put this image to the end of the to-delete-line. You could think of writing a manager which fetches the oldest cached images to make them stay in cache. Another point is: You can configure the number of files which will be cached: "maxNrOfCacheObjects: 20," and you can play with the stalePeriod (set it to 365 days). But be aware: The os can delete cached images to anytime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.