@@ -30,16 +30,18 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
30
30
unsafeRun(f(ticker)) shouldBe Outcome .succeeded(Some (succeed))
31
31
}
32
32
33
- private def newCCache = Caffeine .newBuilder.build[String , Entry [String ]]
33
+ case class MyInt (int : Int )
34
+
35
+ private def newCCache = Caffeine .newBuilder.build[MyInt , Entry [String ]]
34
36
35
37
private def newFCache [F [_]: Sync , V ](
36
- underlying : com.github.benmanes.caffeine.cache.Cache [String , Entry [V ]]
38
+ underlying : com.github.benmanes.caffeine.cache.Cache [MyInt , Entry [V ]]
37
39
) = {
38
- CaffeineCache [F , V ](underlying)
40
+ CaffeineCache [F , MyInt , V ](underlying)
39
41
}
40
42
41
43
private def newIOCache [V ](
42
- underlying : com.github.benmanes.caffeine.cache.Cache [String , Entry [V ]]
44
+ underlying : com.github.benmanes.caffeine.cache.Cache [MyInt , Entry [V ]]
43
45
) = {
44
46
newFCache[IO , V ](underlying)
45
47
}
@@ -49,14 +51,14 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
49
51
it should " return the value stored in the underlying cache if expiration is not specified" in ticked { _ =>
50
52
val underlying = newCCache
51
53
val entry = Entry (" hello" , expiresAt = None )
52
- underlying.put(" key1 " , entry)
54
+ underlying.put(MyInt ( 1 ) , entry)
53
55
54
- newIOCache(underlying).get(" key1 " ).map(_ shouldBe Some (" hello" ))
56
+ newIOCache(underlying).get(MyInt ( 1 ) ).map(_ shouldBe Some (" hello" ))
55
57
}
56
58
57
59
it should " return None if the given key does not exist in the underlying cache" in ticked { _ =>
58
60
val underlying = newCCache
59
- newIOCache(underlying).get(" non-existent key " ).map(_ shouldBe None )
61
+ newIOCache(underlying).get(MyInt ( 2 ) ).map(_ shouldBe None )
60
62
}
61
63
62
64
it should " return None if the given key exists but the value has expired" in ticked { ticker =>
@@ -65,8 +67,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
65
67
val underlying = newCCache
66
68
val expiredEntry =
67
69
Entry (" hello" , expiresAt = Some (Instant .ofEpochMilli(now.toMillis).minusSeconds(60 )))
68
- underlying.put(" key1 " , expiredEntry)
69
- newIOCache(underlying).get(" key1 " ).map(_ shouldBe None )
70
+ underlying.put(MyInt ( 1 ) , expiredEntry)
71
+ newIOCache(underlying).get(MyInt ( 1 ) ).map(_ shouldBe None )
70
72
}
71
73
}
72
74
@@ -76,17 +78,17 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
76
78
val underlying = newCCache
77
79
val expiredEntry =
78
80
Entry (" hello" , expiresAt = Some (Instant .ofEpochMilli(now.toMillis).plusSeconds(60 )))
79
- underlying.put(" key1 " , expiredEntry)
80
- newIOCache(underlying).get(" key1 " ).map(_ shouldBe Some (" hello" ))
81
+ underlying.put(MyInt ( 1 ) , expiredEntry)
82
+ newIOCache(underlying).get(MyInt ( 1 ) ).map(_ shouldBe Some (" hello" ))
81
83
}
82
84
}
83
85
84
86
behavior of " put"
85
87
86
88
it should " store the given key-value pair in the underlying cache with no TTL" in ticked { _ =>
87
89
val underlying = newCCache
88
- newIOCache(underlying).put(" key1 " )(" hello" , None ) *>
89
- IO { underlying.getIfPresent(" key1 " ) }
90
+ newIOCache(underlying).put(MyInt ( 1 ) )(" hello" , None ) *>
91
+ IO { underlying.getIfPresent(MyInt ( 1 ) ) }
90
92
.map(_ shouldBe Entry (" hello" , None ))
91
93
}
92
94
@@ -98,8 +100,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
98
100
99
101
val underlying = newCCache
100
102
101
- newFCache[IO , String ](underlying).put(" key1 " )(" hello" , Some (10 .seconds)).map { _ =>
102
- underlying.getIfPresent(" key1 " ) should be(Entry (" hello" , expiresAt = Some (now.plusSeconds(10 ))))
103
+ newFCache[IO , String ](underlying).put(MyInt ( 1 ) )(" hello" , Some (10 .seconds)).map { _ =>
104
+ underlying.getIfPresent(MyInt ( 1 ) ) should be(Entry (" hello" , expiresAt = Some (now.plusSeconds(10 ))))
103
105
}
104
106
}
105
107
@@ -108,8 +110,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
108
110
val now = Instant .ofEpochMilli(ctx.now().toMillis)
109
111
110
112
val underlying = newCCache
111
- newFCache[IO , String ](underlying).put(" key1 " )(" hello" , Some (30 .days)).map { _ =>
112
- underlying.getIfPresent(" key1 " ) should be(
113
+ newFCache[IO , String ](underlying).put(MyInt ( 1 ) )(" hello" , Some (30 .days)).map { _ =>
114
+ underlying.getIfPresent(MyInt ( 1 ) ) should be(
113
115
Entry (" hello" , expiresAt = Some (now.plusMillis(30 .days.toMillis)))
114
116
)
115
117
}
@@ -120,20 +122,20 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
120
122
it should " delete the given key and its value from the underlying cache" in ticked { _ =>
121
123
val underlying = newCCache
122
124
val entry = Entry (" hello" , expiresAt = None )
123
- underlying.put(" key1 " , entry)
124
- underlying.getIfPresent(" key1 " ) should be(entry)
125
+ underlying.put(MyInt ( 1 ) , entry)
126
+ underlying.getIfPresent(MyInt ( 1 ) ) should be(entry)
125
127
126
- newIOCache(underlying).remove(" key1 " ) *>
127
- IO (underlying.getIfPresent(" key1 " )).map(_ shouldBe null )
128
+ newIOCache(underlying).remove(MyInt ( 1 ) ) *>
129
+ IO (underlying.getIfPresent(MyInt ( 1 ) )).map(_ shouldBe null )
128
130
}
129
131
130
132
behavior of " get after put"
131
133
132
134
it should " store the given key-value pair in the underlying cache with no TTL, then get it back" in ticked { _ =>
133
135
val underlying = newCCache
134
136
val cache = newIOCache(underlying)
135
- cache.put(" key1 " )(" hello" , None ) *>
136
- cache.get(" key1 " ).map { _ shouldBe defined }
137
+ cache.put(MyInt ( 1 ) )(" hello" , None ) *>
138
+ cache.get(MyInt ( 1 ) ).map { _ shouldBe defined }
137
139
}
138
140
139
141
behavior of " get after put with TTL"
@@ -143,28 +145,28 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
143
145
val underlying = newCCache
144
146
val cache = newFCache[IO , String ](underlying)
145
147
146
- cache.put(" key1 " )(" hello" , Some (5 .seconds)) *>
147
- cache.get(" key1 " ).map { _ shouldBe defined }
148
+ cache.put(MyInt ( 1 ) )(" hello" , Some (5 .seconds)) *>
149
+ cache.get(MyInt ( 1 ) ).map { _ shouldBe defined }
148
150
}
149
151
150
152
it should " store the given key-value pair with the given TTL, then get it back (after a sleep) when not expired" in ticked {
151
153
implicit ticker =>
152
154
val underlying = newCCache
153
155
val cache = newFCache[IO , String ](underlying)
154
156
155
- cache.put(" key1 " )(" hello" , Some (50 .seconds)) *>
157
+ cache.put(MyInt ( 1 ) )(" hello" , Some (50 .seconds)) *>
156
158
IO .sleep(40 .seconds) *> // sleep, but not long enough for the entry to expire
157
- cache.get(" key1 " ).map { _ shouldBe defined }
159
+ cache.get(MyInt ( 1 ) ).map { _ shouldBe defined }
158
160
}
159
161
160
162
it should " store the given key-value pair with the given TTL, then return None if the entry has expired" in ticked {
161
163
implicit ticker =>
162
164
val underlying = newCCache
163
165
val cache = newFCache[IO , String ](underlying)
164
166
165
- cache.put(" key1 " )(" hello" , Some (50 .seconds)) *>
167
+ cache.put(MyInt ( 1 ) )(" hello" , Some (50 .seconds)) *>
166
168
IO .sleep(60 .seconds) *> // sleep long enough for the entry to expire
167
- cache.get(" key1 " ).map { _ shouldBe empty }
169
+ cache.get(MyInt ( 1 ) ).map { _ shouldBe empty }
168
170
}
169
171
170
172
}
0 commit comments