Skip to content

Commit

Permalink
Initial cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mjftw committed Jun 22, 2021
1 parent 96f3046 commit 9bd6b10
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# bloop and metals
.bloop
.bsp

# metals
project/metals.sbt
.metals

# vs code
.vscode

# scala 3
.tasty

# sbt
project/project/
project/target/
target/

# other
.history
.scala_dependencies
.cache-main

#general
*.class
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "2.7.5"
lineLength = 100
14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
lazy val CatsEffectVersion = "2.5.1"
lazy val ScalaTestVersion = "3.2.5"

lazy val root = (project in file("."))
.settings(
organization := "io.github.mjftw",
name := "scache",
version := "0.0.1-SNAPSHOT",
scalaVersion := "2.13.4",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % CatsEffectVersion,
"org.scalatest" %% "scalatest" % ScalaTestVersion % Test
)
)
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.5.3
20 changes: 20 additions & 0 deletions src/main/scala/org/github/mjftw/scache/Cache.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.github.mjftw.scache

import cats.effect.concurrent.Ref
import cats.effect.Sync
import cats.implicits._

trait Cache[F[_], A, B] {
def put(key: A, value: B): F[Unit]
def get(key: A): F[Option[B]]
}

object Cache {
def make[F[_]: Sync, A, B]: F[Cache[F, A, B]] =
Ref.of[F, Map[A, B]](Map.empty[A, B]).map { ref =>
new Cache[F, A, B] {
def put(key: A, value: B) = ref.update(_ + (key -> value))
def get(key: A): F[Option[B]] = ref.get.map(_.get(key))
}
}
}

0 comments on commit 9bd6b10

Please sign in to comment.