Exemple : mise en cache d'un objet commande
Les commandes mémorisables en cache sont stockées dans la mémoire cache en vue d'être réutilisées à l'aide d'un mécanisme similaire pour les servlets et les fichiers JSP (Java Server Pages).
Cet exemple de mise en cache de commande correspond à une simple commande de cotation boursière.
L'exemple ci-dessous est un bean de commande de cotation boursière. Il accepte un compteur comme paramètre d'entrée et génère un prix comme paramètre de sortie.
public class QuoteCommand extends CacheableCommandImpl
{
private String ticker;
private double price;
// called to validate that command input parameters have been set
public boolean isReadyToCallExecute() {
return (ticker!=null);
}
// called by a cache-hit to copy output properties to this object
public void setOutputProperties(TargetableCommand fromCommand) {
QuoteCommand f = (QuoteCommand)fromCommand;
this.price = f.price;
}
// business logic method called when the stock price must be retrieved
public void performExecute()throws Exception {...}
//input parameters for the command
public void setTicker(String ticker) { this.ticker=ticker;}
public String getTicker() { return ticker;}
//output parameters for the command
public double getPrice() { return price;};
}
Pour mettre en cache la commande précédente en utilisant le compteur boursier comme clé de mémoire cache ainsi qu'un délai TTL de 60 secondes, utilisez la règle de mise en cache suivante :
<cache>
<cache-entry>
<class>command</class>
<sharing-policy>not-shared</sharing-policy>
<name>QuoteCommand</name>
<cache-id>
<component type="method" id="getTicker">
<required>true</required>
</component>
<priority>3</priority>
<timeout>60</timeout>
</cache-id>
</cache-entry>
</cache>