Here's the code. The total class is rather big (about a 1000 lines) so
I've only copied the relevant lines.
/***** START ******/
package nl.colours.sn.product.productdataexchange.service;
import net.spy.memcached.*;
/**
* Implementation of the ProductDataExchange service component.
*/
public class ProductDataExchangeServiceImpl extends
SimpleServiceComponent implements ProductDataExchangeService,
ConfigurationManagementListener {
public class TrainingList implements Serializable {
private static final long serialVersionUID = 1L;
private String trainingList;
public TrainingList()
{
// empty constructor
}
public TrainingList(String myList) {
trainingList = myList;
}
public String getTrainingList() {
return trainingList;
}
}
public List<Training> getAllTrainings() {
try {
LOG.log(Level.INFO, "Get all tranining (before cache.)");
synchronized (myUpdatingCachedTrainings) {
if(myCachedTrainings==null ||
myUpdatingCachedTrainings || (myCachedTrainingsLastUpdate != null &&
(new Date().getTime() - myCachedTrainingsLastUpdate.getTime()) >=
getUpdateInterval() )){
myCachedTrainings = getTrainingExportManager
().getAll();
TrainingList myList = new TrainingList("test");
writeTrainingToCache(myList);
myUpdatingCachedTrainings=false;
myCachedTrainingsLastUpdate = new Date();
LOG.log(Level.INFO, "Trainings cache was
updated.");
}
}
try {
Object trainingList = getTrainingFromCache();
if (trainingList != null)
myCachedTrainings = (List<Training>)trainingList;
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error casting cache object to
List<Training>");
}
return myCachedTrainings;
} catch (ProductDataExchangeException e) {
LOG.log(Level.SEVERE, "Error during retrieving all
products [" + e.getMessage() + "]");
return Collections.emptyList();
}
}
private String getMemCacheHostName() {
try {
String cacheHostName =
myConfigurationManagement.getParameter
(WCBConstants.CONFIG_SET_NAME + "."
+ WCBConstants.MEMCACHE_SERVER_IP);
return cacheHostName;
} catch (ConfigurationManagementException e) {
LOG.log(Level.WARNING, "Could not find parameter '" +
WCBConstants.MEMCACHE_SERVER_IP
+ "' in configuration set '" +
WCBConstants.CONFIG_SET_NAME + "'", e);
}
return WCBConstants.DEFAULT_MEMCACHE_SERVER_IP;
}
private void writeTrainingToCache(TrainingList myList) {
try {
// Get a memcached client connected to one server
MemcachedClient myCacheClient = new MemcachedClient(new
InetSocketAddress(getMemCacheHostName(), 11211));
// add the new object
myCacheClient.add(cacheKey, 21600, myList);
LOG.log(Level.INFO, "Trainings written to MemCache.");
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error writing trainingObject to Cache. " +
e.getMessage() );
}
}
private Object getTrainingFromCache() {
Object myObject = null;
try {
// Get a memcached client connected to one server
MemcachedClient myCacheClient = new MemcachedClient(new
InetSocketAddress(getMemCacheHostName(), 11211));
// Try to get a value, for up to 5 seconds, and cancel if it
doesn't return
Future<Object> f = myCacheClient.asyncGet(cacheKey);
try {
myObject = f.get(5, TimeUnit.SECONDS);
LOG.log(Level.INFO, "Trainings read from MemCache.");
} catch (TimeoutException e) {
// Since we don't need this, go ahead and cancel the operation.
This
// is not strictly necessary, but it'll save some work on the
// server.
f.cancel(false);
// Do other timeout related stuff
LOG.log(Level.SEVERE, "Error timeout expired during retrieving
trainingObject Cache.");
}
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error during retrieving trainingObject
Cache.");
}
return myObject;
}
}
/***** END ******/