Skip to content

Commit 6917c5e

Browse files
authored
implement Cache#invalidate (#24)
1 parent 40dc268 commit 6917c5e

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

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

+28-17
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ trait Cache[-Key, +Error, +Value] {
5050
*/
5151
def get(key: Key): IO[Error, Value]
5252

53+
/**
54+
* Invalidates the value associated with the specified key.
55+
*/
56+
def invalidate(key: Key): UIO[Unit]
57+
5358
/**
5459
* Returns the approximate number of values in the cache.
5560
*/
@@ -114,6 +119,24 @@ object Cache {
114119

115120
new Cache[Key, Error, Value] {
116121

122+
def cacheStats: UIO[CacheStats] =
123+
ZIO.succeed(CacheStats(hits.longValue, misses.longValue))
124+
125+
def contains(k: Key): UIO[Boolean] =
126+
ZIO.succeed(map.containsKey(k))
127+
128+
def entryStats(k: Key): UIO[Option[EntryStats]] =
129+
ZIO.succeed {
130+
val value = map.get(k)
131+
if (value eq null) None
132+
else {
133+
value match {
134+
case MapValue.Pending(_, _) => None
135+
case MapValue.Complete(_, _, entryState) => Some(EntryStats(entryState.loaded))
136+
}
137+
}
138+
}
139+
117140
def get(k: Key): IO[Error, Value] =
118141
ZIO.effectSuspendTotal {
119142
var key: MapKey[Key] = null
@@ -160,26 +183,14 @@ object Cache {
160183

161184
}
162185

163-
def contains(k: Key): UIO[Boolean] =
164-
ZIO.succeed(map.containsKey(k))
186+
def invalidate(k: Key): UIO[Unit] =
187+
ZIO.succeed {
188+
map.remove(k)
189+
()
190+
}
165191

166192
def size: UIO[Int] =
167193
ZIO.succeed(map.size)
168-
169-
def cacheStats: UIO[CacheStats] =
170-
ZIO.succeed(CacheStats(hits.longValue, misses.longValue))
171-
172-
def entryStats(k: Key): UIO[Option[EntryStats]] =
173-
ZIO.succeed {
174-
val value = map.get(k)
175-
if (value eq null) None
176-
else {
177-
value match {
178-
case MapValue.Pending(_, _) => None
179-
case MapValue.Complete(_, _, entryState) => Some(EntryStats(entryState.loaded))
180-
}
181-
}
182-
}
183194
}
184195
}
185196
}

zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala

+22-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ object CacheSpec extends DefaultRunnableSpec {
2121
] with Has[Clock.Service] with Has[zio.console.Console.Service] with Has[zio.system.System.Service] with Has[
2222
Random.Service
2323
] with Has[Blocking.Service], TestFailure[Any], TestSuccess] = suite("CacheSpec")(
24+
testM("cacheStats") {
25+
checkM(Gen.anyInt) { salt =>
26+
for {
27+
cache <- Cache.make(100, Duration.Infinity, Lookup(hash(salt)))
28+
_ <- ZIO.foreachPar((1 to 100).map(_ / 2))(cache.get)
29+
cacheStats <- cache.cacheStats
30+
hits = cacheStats.hits
31+
misses = cacheStats.misses
32+
} yield assert(hits)(equalTo(49L)) &&
33+
assert(misses)(equalTo(51L))
34+
}
35+
},
36+
testM("invalidate") {
37+
checkM(Gen.anyInt) { salt =>
38+
for {
39+
cache <- Cache.make(100, Duration.Infinity, Lookup(hash(salt)))
40+
_ <- ZIO.foreach(1 to 100)(cache.get)
41+
_ <- cache.invalidate(42)
42+
contains <- cache.contains(42)
43+
} yield assert(contains)(isFalse)
44+
}
45+
},
2446
suite("lookup")(
2547
testM("sequential") {
2648
checkM(Gen.anyInt) { salt =>
@@ -58,18 +80,6 @@ object CacheSpec extends DefaultRunnableSpec {
5880
size <- cache.size
5981
} yield assert(size)(equalTo(10))
6082
}
61-
},
62-
testM("cacheStats") {
63-
checkM(Gen.anyInt) { salt =>
64-
for {
65-
cache <- Cache.make(100, Duration.Infinity, Lookup(hash(salt)))
66-
_ <- ZIO.foreachPar((1 to 100).map(_ / 2))(cache.get)
67-
cacheStats <- cache.cacheStats
68-
hits = cacheStats.hits
69-
misses = cacheStats.misses
70-
} yield assert(hits)(equalTo(49L)) &&
71-
assert(misses)(equalTo(51L))
72-
}
7383
}
7484
)
7585
}

0 commit comments

Comments
 (0)