muesli / cache2go

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

Allow for setting an upper limit on memory usage #34

Open gurre opened 5 years ago

gurre commented 5 years ago

When a system is under high load the cache might grow to the point when it's consuming all available memory on the system it's running.

Consider adding some kind of upper limit to how much resources are occupied e.g. max items or max memory used. Memory usage could be calculated for structs using unsafe.Sizeof if the security requirements are accepting.

muesli commented 5 years ago

I've looked into that before, but sadly there's no way to measure how much memory the stored data interface{} occupies:

For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size of the memory referenced by the slice
gurre commented 5 years ago

The most sensible solution would be to serialize everything to []byte using encoding/gob.