Skip to content

Instantly share code, notes, and snippets.

@MansurAshraf
Last active March 22, 2017 07:27
Show Gist options
  • Select an option

  • Save MansurAshraf/aafd363ba1a3a968ab81f9f9fe8c7d3d to your computer and use it in GitHub Desktop.

Select an option

Save MansurAshraf/aafd363ba1a3a968ab81f9f9fe8c7d3d to your computer and use it in GitHub Desktop.
class InMemoryStore[M[_]: Monad, K, V] extends Store[M, K, V] {
override val monad: Monad[M] = implicitly[Monad[M]]
private val map = new ConcurrentHashMap[K, V]()
override def get(k: K): M[Option[V]] = monad.pure(Option(map.get(k)))
override def put(kv: (K, V)): M[Unit] = monad.pure {
val (k, v) = kv
map.put(k, v)
}
override def close(): Unit = map.clear()
}
object InMemoryStore {
def blocking[K, V]: BlockingStore[K, V] = new InMemoryStore[Try, K, V] with BlockingStore[K, V]
def async[K, V]: AsyncStore[K, V] = new InMemoryStore[Future, K, V] with AsyncStore[K, V]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment