in reply to Where to save module data

I thought it would be a good idea to cache it to a text file and use that if it under a few hours old.

It certainly would be a good idea. However, if you'll permit me to chip in with the results of my experience, I caution against writing such a caching system yourself. I have done this more than once and have also inherited code where others have done this more than once. In every single case the caching system has drawbacks - one or two are serious, all are irritating.

In Perl we are fortunate indeed to have a very high-quality but generic and flexible cache module available to use in the shape of CHI and it would be hard to praise it too highly. Do try it out in your (apparently fairly simple) use case. You should find it easy to use and very efficient and hopefully that will encourage you to use it for other, more involved, cache scenarios.

Is there a standard way to handle this?

To quote AST: The nice thing about standards is that you have so many of them to choose from.


🦛

Replies are listed 'Best First'.
Re^2: Where to save module data
by Bod (Parson) on Oct 09, 2024 at 11:28 UTC
    if you'll permit me to chip in with the results of my experience...

    I am always eager to hear hippo experience nuggets!

    In Perl we are fortunate indeed to have a very high-quality but generic and flexible cache module available to use in the shape of CHI and it would be hard to praise it too highly.

    Thank you hippo - until now, this module has been unknown to me.

    Briefly looking at it, I can immediately see its value and will try it out with a simple use case...

      CHI does look like the way to go. But its File driver defaults to /tmp/chi-driver-file (or OS equivalent), and it sounds to me like your data would more appropriately be in /var/cache/your-module-name.

      Filesystem Hierarchy Standard:
      /var/cache is intended for cached data from applications. Such data is locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. Unlike /var/spool, the cached files can be deleted without data loss. The data must remain valid between invocations of the application and rebooting the system. Files located under /var/cache may be expired in an application specific manner, by the system administrator, or both. The application must always be able to recover from manual deletion of these files (generally because of a disk space shortage). No other requirements are made on the data format of the cache directories.
      You'd want to specify a location for Windows, too. A little googling didn't find any close equivalent to /var/cache for non-FHS systems, so if /var/cache doesn't exist, you'd likely just want to use something under File::Spec->tmpdir or let CHI default.
      --
      A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |

      I just thought I'd add a comment on CHI. I hadn't ever heard of it
      either and it seems like a valuable module to know about if you need to
      store/cache data. Happily, I report that it installs without drama on
      cygwinPerl; lots of dependencies though. IMHO however, this is generally
      a good thing. It shows that the author has thought carefully about
      her/his code and researched thoroughly.

      Oct 19, 2024 at 22:30 UTC
      Examine what is said, not who speaks.
      Love the truth but pardon error.
      Silence betokens consent.
      In the absence of evidence, opinion is indistinguishable from prejudice.