问题
I have following large object (20KB) cached in memcache -
Product :
{
BasicInfo, //~5KB
SellerId, //int
CityId, //int
AdditionalInfo //~15KB
}
This is being accessed at multiple places-
- At some place, only sellerId or cityId is required.
- At some places, only basicInfo is required.
- At some places, whole object is required.
So we are fetching whole object unnecessarily in 1st and 2nd cases while we only require some bytes. Should I store these separately in memcache (only problem is I need to invalidate multiple keys on updation)?
Is there any better way to handle these cases?
回答1:
There are 2 aspects to consider here and the trade-off between them.
- Time to access cached data
- Consistency of data
How storing small granular data will reduce time to access i.e why complex object are taking more time?
For storing any value in memcache, it has to be serialized and deserialized. The more complex the object is, it will add more processing for serialization and de-serialization @ application end.
It is common in production to have memcache in a separate machine/s (same n/w or different n/w). In such cases, the bigger data size will add latency due to network i/o and roundtrip time.
It is a trade-off to use complete object or multiple granular objects.
In our case, where we use cache mostly for our cached data are high frequent read and rarely updated. We store granular level objects.
来源:https://stackoverflow.com/questions/45455922/store-single-big-object-in-memcached-vs-multiple-keys