Skip to content

Instantly share code, notes, and snippets.

@kareblak
Last active August 29, 2015 14:06

Revisions

  1. kareblak revised this gist Sep 4, 2014. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -22,5 +22,15 @@
    purge()
    }

    ...

    def get(key: K, orElse: FV): Option[V] = {
    get(key).orElse {
    Option(orElse()).map { value =>
    val element = new Element(key, value)
    cache.put(element)
    value
    }
    }
    }
    }
    }
  2. kareblak created this gist Sep 4, 2014.
    26 changes: 26 additions & 0 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    case class GetOrElseCachedValue[K, V](key: K, orElse: () => V)
    case class GetCachedValue[K, V](key: K)
    case class PutCachedValue[K, V](key: K, value: V)
    case class ReturnCachedValue[V](value: Option[V])
    case class RemoveCachedValue[K](key: K)
    case object PurgeCachedValues

    class CacheActor[K, V] private[cache](cache: Cache) extends Actor with ActorLogging with StatsD {

    type FV = (() => V)

    def receive = {
    case GetOrElseCachedValue(key: K, orElse: FV) =>
    sender ! ReturnCachedValue(get(key, orElse))
    case GetCachedValue(key: K) =>
    sender ! ReturnCachedValue(get(key))
    case PutCachedValue(key: K, value: FV) =>
    put(key, value)
    case RemoveCachedValue(key: K) =>
    remove(key)
    case PurgeCachedValues =>
    purge()
    }

    }
    }