@@ -175,47 +175,6 @@ func TestCacheNoPurge(t *testing.T) {
175175 assert .Equal (t , []string {"key3" , "key4" , "key1" }, lc .Keys ())
176176}
177177
178- func TestCacheWithDeleteExpired (t * testing.T ) {
179- var evicted []string
180- lc := NewCache [string , string ]().WithTTL (150 * time .Millisecond ).WithOnEvicted (
181- func (key string , value string ) {
182- evicted = append (evicted , key , value )
183- })
184-
185- lc .Set ("key1" , "val1" , 0 )
186-
187- time .Sleep (100 * time .Millisecond ) // not enough to expire
188- lc .DeleteExpired ()
189- assert .Equal (t , 1 , lc .Len ())
190-
191- v , ok := lc .Get ("key1" )
192- assert .Equal (t , "val1" , v )
193- assert .True (t , ok )
194-
195- time .Sleep (200 * time .Millisecond ) // expire
196- lc .DeleteExpired ()
197- v , ok = lc .Get ("key1" )
198- assert .False (t , ok )
199- assert .Equal (t , "" , v )
200-
201- assert .Equal (t , 0 , lc .Len ())
202- assert .Equal (t , []string {"key1" , "val1" }, evicted )
203-
204- // add new entry
205- lc .Set ("key2" , "val2" , 0 )
206- assert .Equal (t , 1 , lc .Len ())
207-
208- // nothing deleted
209- lc .DeleteExpired ()
210- assert .Equal (t , 1 , lc .Len ())
211- assert .Equal (t , []string {"key1" , "val1" }, evicted )
212-
213- // Purge, cache should be clean
214- lc .Purge ()
215- assert .Equal (t , 0 , lc .Len ())
216- assert .Equal (t , []string {"key1" , "val1" , "key2" , "val2" }, evicted )
217- }
218-
219178func TestCache_Values (t * testing.T ) {
220179 lc := NewCache [string , string ]().WithMaxKeys (3 )
221180
@@ -303,58 +262,6 @@ func TestCacheInvalidateAndEvict(t *testing.T) {
303262 assert .Zero (t , lc .Len ())
304263}
305264
306- func TestCacheExpired (t * testing.T ) {
307- lc := NewCache [string , string ]().WithTTL (time .Millisecond * 5 )
308-
309- lc .Set ("key1" , "val1" , 0 )
310- assert .Equal (t , 1 , lc .Len ())
311-
312- v , ok := lc .Peek ("key1" )
313- assert .Equal (t , v , "val1" )
314- assert .True (t , ok )
315-
316- v , ok = lc .Get ("key1" )
317- assert .Equal (t , v , "val1" )
318- assert .True (t , ok )
319-
320- time .Sleep (time .Millisecond * 10 ) // wait for entry to expire
321- assert .Equal (t , 1 , lc .Len ()) // but not purged
322-
323- v , ok = lc .Peek ("key1" )
324- assert .Equal (t , "val1" , v , "expired and marked as such, but value is available" )
325- assert .False (t , ok )
326-
327- v , ok = lc .Get ("key1" )
328- assert .Equal (t , "val1" , v , "expired and marked as such, but value is available" )
329- assert .False (t , ok )
330-
331- assert .Empty (t , lc .Values ())
332- }
333-
334- func TestCache_GetExpiration (t * testing.T ) {
335- lc := NewCache [string , string ]().WithTTL (time .Second * 5 )
336-
337- lc .Set ("key1" , "val1" , time .Second * 5 )
338- assert .Equal (t , 1 , lc .Len ())
339-
340- exp , ok := lc .GetExpiration ("key1" )
341- assert .True (t , ok )
342- assert .True (t , exp .After (time .Now ().Add (time .Second * 4 )))
343- assert .True (t , exp .Before (time .Now ().Add (time .Second * 6 )))
344-
345- lc .Set ("key2" , "val2" , time .Second * 10 )
346- assert .Equal (t , 2 , lc .Len ())
347-
348- exp , ok = lc .GetExpiration ("key2" )
349- assert .True (t , ok )
350- assert .True (t , exp .After (time .Now ().Add (time .Second * 9 )))
351- assert .True (t , exp .Before (time .Now ().Add (time .Second * 11 )))
352-
353- exp , ok = lc .GetExpiration ("non-existing-key" )
354- assert .False (t , ok )
355- assert .Zero (t , exp )
356- }
357-
358265func TestCacheRemoveOldest (t * testing.T ) {
359266 lc := NewCache [string , string ]().WithLRU ().WithMaxKeys (2 )
360267
0 commit comments