permutive-engineering / refreshable   2.0.0

Apache License 2.0 GitHub

Offers a `Refreshable` type that operates like a cache of size 1 with a background fiber that periodically refreshes the stored value

Scala versions: 3.x 2.13 2.12

Offers a Refreshable type that operates like a cache of size 1 with a background fiber that periodically refreshes the stored value


Installation

Add the following line to your build.sbt file:

libraryDependencies += "com.permutive" %% "refreshable" % "2.0.0"

The library is published for Scala versions: 2.12, 2.13 and 3.

Usage

Refreshable

Refreshable lives in the Typelevel Scala ecosystem and offers a Refreshable type that operates like a cache of size 1 with a background fiber that periodically refreshes the stored value. Use it when you have criticial data that needs to be cached and you would rather read stale data in the event that refreshing the data fails.

trait Refreshable[F[_], A] {

  /** Get the unwrapped value of `A`
    */
  def value: F[A] = get.map(_.value)

  /** Get the value of `A` wrapped in a status
    */
  def get: F[CachedValue[A]]

  /** Cancel refreshing
    */
  def cancel: F[Boolean]

  /** Restart refreshing
    */
  def restart: F[Boolean]
}

sealed trait CachedValue[A] {
  def value: A
}

object CachedValue {
  case class Success[A](value: A) extends CachedValue[A]
  case class Error[A](value: A, error: Throwable) extends CachedValue[A]
  case class Cancelled[A](value: A) extends CachedValue[A]
}

Contributors to this project

TimWSpence janstenpickle alejandrohdezma
TimWSpence janstenpickle alejandrohdezma