6
\$\begingroup\$

Is this a proper way to cache side effects (occurring on job.tryLoad)?

case class DFStorage(private var cache: Map[String, DataFrame] = Map()) {
  def tryLoad(job: Job): Kleisli[IO, MakeContext, \/[List[String], Unit]] = {
    if(!cache.contains(job.id)) {
      job.tryLoad.map(_.map(df => add(job, df)))
    } else {
      IO(().right[List[String]]).liftKleisli
    }
  }
  def add(job: Job, df: DataFrame): IO[Unit] = {
    IO(cache += (job.id -> df))
  }
  def toMap = IO(cache)
}
\$\endgroup\$
  • \$\begingroup\$ Is the IO something internal? I can't find any documentation for it. \$\endgroup\$ – gardenhead Jan 4 '16 at 22:27
  • \$\begingroup\$ @gardenhead scalaz.effect.IO \$\endgroup\$ – Reactormonk Jan 5 '16 at 3:24
1
\$\begingroup\$

Looks good to me.

Although, having cache as a simple Map might lay claim to more memory that you like. Associating an LRU with it would allow each addition to evict an ancient job's cached dataframe.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.