robospice icon indicating copy to clipboard operation
robospice copied to clipboard

Support caching lists and arrays of objects in ORMLite backend

Open lukesleeman opened this issue 11 years ago • 7 comments

It appears that the ORMLite cache cannot handle SpiceRequests which return an array or a List: Issue: https://github.com/octo-online/robospice/issues/35 http://stackoverflow.com/questions/15801315/robospice-storing-object-that-extends-arraylist-in-database-via-ormlite https://groups.google.com/forum/?fromgroups#!searchin/robospice/ormlite/robospice/2GaiisbzuUQ/pG_3-d48_k8J

It seems the accepted work around is to wrap the list or array in an object inside the SpiceRequest and return that. eg, rayther than returning List<XXX> you return class XXXList which contains a private variable List<XXX>. OrmLite persists the XXXList into the DB.

Many webservices return a json array of results and following the workaround means you get an extra XXXList or YYYList table in your database, which can clutter things up and be more complex to manage. It would be very nice if the ORMLite cache backend 'natively' supported SpiceRequests which return an array or List of objects.

lukesleeman avatar Feb 03 '14 06:02 lukesleeman

Digging through the code the following would be a rough plan for how support would be added:

1 - InDatabaseObjectPersisterFactory.createObjectPersister(Class< DATA > clazz) would need to be modified with logic to detect if DATA is a list or array type. Rayther than return InDatabaseObjectPersister it would need to return a different class: InDatabaseListObjectPersister. Perhaps InDatabaseListObjectPersister extends InDatabaseObjectPersister, though I am not too sure about this. Another alternative is that InDatabaseListObjectPersister wraps a InDatabaseObjectPersister and delegates some work to it?

2 - InDatabaseListObjectPersister.loadDataFromCache would need to handle the case where multiple CacheEntry objects are returned. It would basically need to loop through them and resolve each CacheEntry instead of assuming there is a single cache entry. Performance may be an issue as we would be executing lots of queries - one for each CacheEntry to resolve the associated object.

3 - InDatabaseListObjectPersister.saveDataToCacheAndReturnData(final T data, final Object cacheKey) would need to assume that T is a list or array. It would need to iterate through each element in the array and then basically perform the same logic that InDatabaseObjectPersister.saveDataToCacheAndReturnData does

4 - removeDataFromCache would need to be modified to assume that a list of CacheEntry objects will need to be removed not just one.

5 - Special attention will need to be paid to the case that the SpiceRequest returns an empty array or list. I'm unsure how the existing cache backends support SpiceRequest's that return null, but we might want to piggy back off that behaviour. Looking through InDatabaseObjectPersister I'm not sure a SpiceRequest that returns null would work with the ORM Lite backend. There seems to be the assumption that a SpiceRequest will definitely result in one row in the cacheentry table. Perhaps add boolean noResults field to CacheEntry that is true if the request returns no results?

lukesleeman avatar Feb 03 '14 06:02 lukesleeman

Can you submit a PR and tests ?

stephanenicolas avatar Jun 10 '14 22:06 stephanenicolas

Has this issue been resolved?

MoizAli avatar Oct 29 '14 17:10 MoizAli

Would love to see this fixed.

deepakazad avatar Nov 20 '14 19:11 deepakazad

We have moved away from robospice, so its unlikely that I will find time to implement this. If somebody else wants to pick it up though, they are more than welcome.

lukesleeman avatar Nov 20 '14 22:11 lukesleeman

@lukesleeman What do you use as an alternative to Robospice to accomplish this ?

deepakazad avatar Nov 21 '14 02:11 deepakazad

Sorry to hear this, but that maybe a good move. ORMLite is an orphan module, too complex for me to take it in charge on my spare time. All volunteers welcome.. I am curious of the alternative you found too @lukesleeman.

stephanenicolas avatar Nov 21 '14 04:11 stephanenicolas