Skip to content

Commit 4ec7a42

Browse files
authored
Cache managed resource (#71)
* Add .bsp to .gitignore * 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 Scala 3 * Fix doc after Adam's review * fixup! Fix doc after Adam's review * fixup! Fix doc after Adam's review
1 parent 6fb05d1 commit 4ec7a42

File tree

9 files changed

+1474
-70
lines changed

9 files changed

+1474
-70
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,12 @@ project/plugins/project/
408408
.metals/
409409
metals.sbt
410410
.bloop/
411+
.bsp/
411412
project/secret
412413

413414
# mdoc
414415
website/node_modules
415416
website/build
416417
website/i18n/en.json
417418
website/static/api
419+

build.sbt

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ lazy val zioCache = crossProject(JSPlatform, JVMPlatform, NativePlatform)
6060
.settings(buildInfoSettings("zio.cache"))
6161
.settings(
6262
libraryDependencies ++= Seq(
63-
"dev.zio" %% "zio" % zioVersion,
64-
"dev.zio" %% "zio-test" % zioVersion % Test,
65-
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
63+
"dev.zio" %% "zio" % zioVersion,
64+
"org.scala-lang.modules" %% "scala-collection-compat" % "2.7.0",
65+
"dev.zio" %% "zio-test" % zioVersion % Test,
66+
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
6667
)
6768
)
6869
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))

zio-cache/shared/src/main/scala/zio/cache/Cache.scala

-67
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,6 @@ object Cache {
281281
}
282282
}
283283

284-
/**
285-
* A `MapKey` represents a key in the cache. It contains mutable references
286-
* to the previous key and next key in the `KeySet` to support an efficient
287-
* implementation of a sorted set of keys.
288-
*/
289-
private final class MapKey[Key](
290-
val value: Key,
291-
var previous: MapKey[Key] = null,
292-
var next: MapKey[Key] = null
293-
)
294-
295284
/**
296285
* A `MapValue` represents a value in the cache. A value may either be
297286
* `Pending` with a `Promise` that will contain the result of computing the
@@ -346,60 +335,4 @@ object Cache {
346335
new AtomicBoolean(false)
347336
)
348337
}
349-
350-
/**
351-
* A `KeySet` is a sorted set of keys in the cache ordered by last access.
352-
* For efficiency, the set is implemented in terms of a doubly linked list
353-
* and is not safe for concurrent access.
354-
*/
355-
private final class KeySet[Key] {
356-
private[this] var head: MapKey[Key] = null
357-
private[this] var tail: MapKey[Key] = null
358-
359-
/**
360-
* Adds the specified key to the set.
361-
*/
362-
def add(key: MapKey[Key]): Unit =
363-
if (key ne tail) {
364-
if (tail ne null) {
365-
val previous = key.previous
366-
val next = key.next
367-
if (next ne null) {
368-
key.next = null
369-
if (previous ne null) {
370-
previous.next = next
371-
next.previous = previous
372-
} else {
373-
head = next
374-
head.previous = null
375-
}
376-
}
377-
tail.next = key
378-
key.previous = tail
379-
tail = key
380-
} else {
381-
head = key
382-
tail = key
383-
}
384-
}
385-
386-
/**
387-
* Removes the lowest priority key from the set.
388-
*/
389-
def remove(): MapKey[Key] = {
390-
val key = head
391-
if (key ne null) {
392-
val next = key.next
393-
if (next ne null) {
394-
key.next = null
395-
head = next
396-
head.previous = null
397-
} else {
398-
head = null
399-
tail = null
400-
}
401-
}
402-
key
403-
}
404-
}
405338
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package zio.cache
2+
3+
/**
4+
* A `KeySet` is a sorted set of keys in the cache ordered by last access.
5+
* For efficiency, the set is implemented in terms of a doubly linked list
6+
* and is not safe for concurrent access.
7+
*/
8+
private[cache] final class KeySet[Key] {
9+
private[this] var head: MapKey[Key] = null
10+
private[this] var tail: MapKey[Key] = null
11+
12+
/**
13+
* Adds the specified key to the set.
14+
*/
15+
def add(key: MapKey[Key]): Unit =
16+
if (key ne tail) {
17+
if (tail ne null) {
18+
val previous = key.previous
19+
val next = key.next
20+
if (next ne null) {
21+
key.next = null
22+
if (previous ne null) {
23+
previous.next = next
24+
next.previous = previous
25+
} else {
26+
head = next
27+
head.previous = null
28+
}
29+
}
30+
tail.next = key
31+
key.previous = tail
32+
tail = key
33+
} else {
34+
head = key
35+
tail = key
36+
}
37+
}
38+
39+
/**
40+
* Removes the lowest priority key from the set.
41+
*/
42+
def remove(): MapKey[Key] = {
43+
val key = head
44+
if (key ne null) {
45+
val next = key.next
46+
if (next ne null) {
47+
key.next = null
48+
head = next
49+
head.previous = null
50+
} else {
51+
head = null
52+
tail = null
53+
}
54+
}
55+
key
56+
}
57+
}

0 commit comments

Comments
 (0)