muesli / cache2go

Concurrency-safe Go caching library with expiration capabilities and access counters
Other
2.11k stars 518 forks source link

why not have a map to storage the eternal item? #30

Open icaiyu opened 5 years ago

icaiyu commented 5 years ago

Establish a map to storage the eternal items so that ExpirationCheck don't have to check them in the iteration ? why not?

muesli commented 5 years ago

Can you elaborate a bit more what you're suggesting?

icaiyu commented 5 years ago

Every expirationCheck will iterate all items including the permanent items and and non-permanent items. I think it unnecessary to check the permanent items, why not store them to another map so that the expirationCheck checks the non-permanent items only.

Plus.. I think that using cleanupInterval to check the expiration is not that efficient. Like this example: Add an item with lifespan = 10 to a blank table, the cleanupInterval will be 10s, 6s later, add another item with lifespan = 8, it will call the table.expirationcheck() because item.lifespan < cleanupInteval in fact, it's unnecessary to check because 10 - 6 < 8 (the cleanupTimer countdown to 10-6 = 4) . In a nutshell, the cleanupTimer is really close to call expirationCheck as time goes by, however, the cleanupInterval remains unchanged!

Since we couldn't get the countdown time of the cleanupTimer directly, note down the cleaning time as cleanupTime is helpful to make it.

I submit a PR. If I was wrong please correct me and I will be really grateful. Thank you in advance!