Does play automatically cache data?
I’m trying to read this code that someone else wrote and he does Cache.get while inside a foreach loop. This is also where the program stalls.
This person did not use Cache.set anywhere else related to this method so I don’t understand what is happening.
An example of the code:
for(aList page: pages){
//blah blah code;
Page.PageInfo pageinfo = (Page.PageInfo) Cache.get(page.url);
//blah blah code;
}
However he does use Cache.set after this foreach loop in another foreach loop but I don’t understand how he is getting something if there is nothing set in there.
Play has its own Cache API which can be backed by the default EHCache implementation or something more advanced like Memcached.
Cache is only a cache, so it is not guaranteed that
Cache.get()calls will always provide results. The code snippet you provided is obviously lacking a null check after theCache.get()call to see whether result was found or not. If Cache doesn’t have the value, you’re supposed to get it from elsewhere (database, usually, which is more costly operation but guaranteed to return the result) or continue working without the value, if it is not critical.