hyperoslo / Cache

:package: Nothing but Cache.
Other
2.97k stars 334 forks source link

Cache Expiry .seconds bug #186

Closed gabors closed 4 years ago

gabors commented 6 years ago

Recently I found that when saving a Codable into a cache that is configured to use Expiry .seconds() will never make the cache item expire. Every time I fetch an entry from the cache the expiry.date shows the current date + seconds, and therefore isExpired is always false.

When I save using the Expiry .date() entries expire as expected.

gabors commented 6 years ago

EDIT: even with saving to cache with .date() Expiry some of the API behave inconsistently.

Here is my testing:

myCache.entry(ofType: String.self, forKey: key) would find the entry in the cache (unless I just called removeExpiredObjects()) and the entry would correctly show isExpired as true.

OTH, when I call myCache.existsObject(ofType: String.self, forKey: key) this would return true, which is unexpected.

ALSO! calling myCache.object(ofType: String.self, forKey: key) also finds the value in the cache even though it is expired. Very unexpected.

onmyway133 commented 6 years ago

@gabors Hi, I wonder if this fixes your issue https://github.com/hyperoslo/Cache/pull/176 ?

hoaidv commented 6 years ago

Hi @onmyway133, the code reads

case .seconds(let seconds):
      return Date().addingTimeInterval(seconds)

Then obviously if we call isExpired lately, the item will never get expired because the resulting date will be always greater than .now().

miaoruiyuan commented 4 years ago

@onmyway133 the same problem for me ,you fix it ?