karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.27k stars 118 forks source link

Generic key type #82

Open dnaka91 opened 1 year ago

dnaka91 commented 1 year ago

I was wondering why the value was made generic with the introduction of Go generics, but the key not. A quick check in the code revealed that there are some parts of the API that rely on a string as key, but overall it seems possible to support other types.

The motivation behind this is, that we try to avoid unneeded GC overhead at work, and often we have an integer as key. By making the key generic, we could avoid the extra conversion to a string just to be able to lookup values in the cache.

karlseguin commented 1 year ago

Never came up. The value change is pretty straightforward.

I think the biggest issue with the key is the bucketing and getting a hash key out of any value. Off the top of my head, don't know how to do that without requiring a hash function (or the key type to implement a HashCode function).

Some advanced functionality wouldn't work, like DeletePrefix - but I think it would be fine to fail in those cases.

dnaka91 commented 1 year ago

Great to hear (I mean, you seem very open to introducing this).

But true, there's probably no easy way of supporting the hashing logic, other than defining a new interface that all key types would need to implement.