A queue of keys stamped with the creation timestamp allows for dropping
elements from the front easily. This makes the expiration cheap, but pays
in memory resources. Jan's suggestion is subtly cheaper from a memory
perspective, but pays with a bit more CPU cycles. Since this is just a
simple queue, a ring buffer might be adequate.

Alternatively, you can consider using a LRU, 2Q or ARC cache[0], for
instance github.com/hashicorp/golang-lru

This can put an upper bound on the size of the data set and depending on
your needs this might suit your application better: a sudden burst in
insertion rates cannot accidentally overflow your systems memory, but will
simply result in a system slowdown.

[0] 2Q caches often beat LRU caches. ARC caches are even better, but there
are rumors around that the ghost-like feature of the ARC caches may have
patents.


On Wed, Jul 19, 2017 at 9:40 PM Rajanikanth Jammalamadaka <
rajanika...@gmail.com> wrote:

> Thanks Shawn/Jan
>
> On Wednesday, July 19, 2017 at 3:32:59 PM UTC-4, Jan Mercl wrote:
>
>>
>> On Wed, Jul 19, 2017 at 9:22 PM Rajanikanth Jammalamadaka <
>> rajan...@gmail.com> wrote:
>>
>> > Does somebody have a recommendation for an expiring dict? If I have to
>> write my own, is there a way to avoid iterating over all the keys to delete
>> the expired entries?
>>
>> Common approach is to tag the entries with insertion or expiration time
>> and remove invalid items only on retrieval. If there are not enough
>> retrievals that keep the ratio of useful/expired map entries sane, a
>> "cleaner" goroutine can be run concurrently.
>>
>>
>>
>>
>> --
>>
>> -j
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to