Skip to content

Commit

Permalink
Cache managed resource cdk (#3)
Browse files Browse the repository at this point in the history
* Extract KeySet and MapKey out of Cache

Because they probably going to be shared with ManagedCache

* define interface for a ManagedCache

* Copy/Paste: cache implementation and cache test for managedCache

* Adapt cache implementation and test for ManagedCache

* make ManagedCache.get return Managed[Error, Value] instead of UIO[Managed[Error, Value]]

* Make ManagedCache work on 2.11 and dotty
  • Loading branch information
strokyl authored May 19, 2022
1 parent ae86fab commit 3314b10
Show file tree
Hide file tree
Showing 8 changed files with 1,454 additions and 71 deletions.
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ThisBuild / licenses := List("Apache-2.0" -> url("http://www.apache.org/lice
ThisBuild / publishTo := Some(
s"Github $GITHUB_OWNER Apache Maven Packages of $GITHUB_PROJECT" at s"https://maven.pkg.github.com/$GITHUB_OWNER/$GITHUB_PROJECT"
)
ThisBuild / resolvers += s"GitHub $GITHUB_OWNER Apache Maven Packages" at s"https://maven.pkg.github.com/$GITHUB_OWNER/_/"
ThisBuild / resolvers += s"GitHub $GITHUB_OWNER Apache Maven_Packages" at s"https://maven.pkg.github.com/$GITHUB_OWNER/_/"
ThisBuild / developers := List(
Developer(
"jdegoes",
Expand Down Expand Up @@ -81,9 +81,10 @@ lazy val zioCache = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(buildInfoSettings("zio.cache"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
"dev.zio" %% "zio" % zioVersion,
"org.scala-lang.modules" %% "scala-collection-compat" % "2.7.0",
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand Down
67 changes: 0 additions & 67 deletions zio-cache/shared/src/main/scala/zio/cache/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,6 @@ object Cache {
}
}

/**
* A `MapKey` represents a key in the cache. It contains mutable references
* to the previous key and next key in the `KeySet` to support an efficient
* implementation of a sorted set of keys.
*/
private final class MapKey[Key](
val value: Key,
var previous: MapKey[Key] = null,
var next: MapKey[Key] = null
)

/**
* A `MapValue` represents a value in the cache. A value may either be
* `Pending` with a `Promise` that will contain the result of computing the
Expand Down Expand Up @@ -346,60 +335,4 @@ object Cache {
new AtomicBoolean(false)
)
}

/**
* A `KeySet` is a sorted set of keys in the cache ordered by last access.
* For efficiency, the set is implemented in terms of a doubly linked list
* and is not safe for concurrent access.
*/
private final class KeySet[Key] {
private[this] var head: MapKey[Key] = null
private[this] var tail: MapKey[Key] = null

/**
* Adds the specified key to the set.
*/
def add(key: MapKey[Key]): Unit =
if (key ne tail) {
if (tail ne null) {
val previous = key.previous
val next = key.next
if (next ne null) {
key.next = null
if (previous ne null) {
previous.next = next
next.previous = previous
} else {
head = next
head.previous = null
}
}
tail.next = key
key.previous = tail
tail = key
} else {
head = key
tail = key
}
}

/**
* Removes the lowest priority key from the set.
*/
def remove(): MapKey[Key] = {
val key = head
if (key ne null) {
val next = key.next
if (next ne null) {
key.next = null
head = next
head.previous = null
} else {
head = null
tail = null
}
}
key
}
}
}
57 changes: 57 additions & 0 deletions zio-cache/shared/src/main/scala/zio/cache/KeySet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package zio.cache

/**
* A `KeySet` is a sorted set of keys in the cache ordered by last access.
* For efficiency, the set is implemented in terms of a doubly linked list
* and is not safe for concurrent access.
*/
private[cache] final class KeySet[Key] {
private[this] var head: MapKey[Key] = null
private[this] var tail: MapKey[Key] = null

/**
* Adds the specified key to the set.
*/
def add(key: MapKey[Key]): Unit =
if (key ne tail) {
if (tail ne null) {
val previous = key.previous
val next = key.next
if (next ne null) {
key.next = null
if (previous ne null) {
previous.next = next
next.previous = previous
} else {
head = next
head.previous = null
}
}
tail.next = key
key.previous = tail
tail = key
} else {
head = key
tail = key
}
}

/**
* Removes the lowest priority key from the set.
*/
def remove(): MapKey[Key] = {
val key = head
if (key ne null) {
val next = key.next
if (next ne null) {
key.next = null
head = next
head.previous = null
} else {
head = null
tail = null
}
}
key
}
}
Loading

0 comments on commit 3314b10

Please sign in to comment.