76 lines
4.7 KiB
HTML
76 lines
4.7 KiB
HTML
<html>
|
|
<head>
|
|
<title>OSCache -
|
|
Usage
|
|
</title>
|
|
<link rel="stylesheet" href="styles/site.css" type="text/css" />
|
|
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
</head>
|
|
|
|
<body>
|
|
<table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
|
|
<tr>
|
|
<td valign="top" class="pagebody">
|
|
<p>Beside the <a href="JSP Tags.html" title="JSP Tags">JSP tag library</a> and the <a href="CacheFilter.html" title="CacheFilter">CacheFilter</a> you can use OSCache through its straightforward API. You can use the <a href="http://www.opensymphony.com/oscache/api/com/opensymphony/oscache/general/GeneralCacheAdministrator.html" title="Visit page outside Confluence">GeneralCacheAdministrator</a> to create, flush and administrate the cache. The GeneralCacheAdministrator has a cache instance and delegates different Cache's methods. Furthermore the GeneralCacheAdministrator is in charge of load the <a href="Configuration.html" title="Configuration">cache.properties</a> and create a cache instance with the properties definded. You have to store an instance of the GeneralCacheAdministrator in a static value or use a singleton pattern to access the same GeneralCacheAdministrator.</p>
|
|
|
|
<h4><a name="APIUsage-Typicalusewithfailover"></a>Typical use with fail over</h4>
|
|
|
|
<div class="code"><div class="codeContent">
|
|
<pre class="code-java"><span class="code-object">String</span> myKey = <span class="code-quote">"myKey"</span>;
|
|
<span class="code-object">String</span> myValue;
|
|
<span class="code-object">int</span> myRefreshPeriod = 1000;
|
|
<span class="code-keyword">try</span> {
|
|
<span class="code-comment">// Get from the cache
|
|
</span> myValue = (<span class="code-object">String</span>) admin.getFromCache(myKey, myRefreshPeriod);
|
|
} <span class="code-keyword">catch</span> (NeedsRefreshException nre) {
|
|
<span class="code-keyword">try</span> {
|
|
<span class="code-comment">// Get the value (probably from the database)
|
|
</span> myValue = <span class="code-quote">"This is the content retrieved."</span>;
|
|
<span class="code-comment">// Store in the cache
|
|
</span> admin.putInCache(myKey, myValue);
|
|
} <span class="code-keyword">catch</span> (Exception ex) {
|
|
<span class="code-comment">// We have the current content <span class="code-keyword">if</span> we want fail-over.
|
|
</span> myValue = (<span class="code-object">String</span>) nre.getCacheContent();
|
|
<span class="code-comment">// It is essential that cancelUpdate is called <span class="code-keyword">if</span> the
|
|
</span> <span class="code-comment">// cached content is not rebuilt
|
|
</span> admin.cancelUpdate(myKey);
|
|
}
|
|
}</pre>
|
|
</div></div>
|
|
|
|
<h4><a name="APIUsage-Typicalusewithoutfailover"></a>Typical use without fail over</h4>
|
|
|
|
<div class="code"><div class="codeContent">
|
|
<pre class="code-java"><span class="code-object">String</span> myKey = <span class="code-quote">"myKey"</span>;
|
|
<span class="code-object">String</span> myValue;
|
|
<span class="code-object">int</span> myRefreshPeriod = 1000;
|
|
<span class="code-keyword">try</span> {
|
|
<span class="code-comment">// Get from the cache
|
|
</span> myValue = (<span class="code-object">String</span>) admin.getFromCache(myKey, myRefreshPeriod);
|
|
} <span class="code-keyword">catch</span> (NeedsRefreshException nre) {
|
|
<span class="code-keyword">try</span> {
|
|
<span class="code-comment">// Get the value (probably from the database)
|
|
</span> myValue = <span class="code-quote">"This is the content retrieved."</span>;
|
|
<span class="code-comment">// Store in the cache
|
|
</span> admin.putInCache(myKey, myValue);
|
|
updated = <span class="code-keyword">true</span>;
|
|
} <span class="code-keyword">finally</span> {
|
|
<span class="code-keyword">if</span> (!updated) {
|
|
<span class="code-comment">// It is essential that cancelUpdate is called <span class="code-keyword">if</span> the
|
|
</span> <span class="code-comment">// cached content could not be rebuilt
|
|
</span> admin.cancelUpdate(myKey);
|
|
}
|
|
}
|
|
}</pre>
|
|
</div></div>
|
|
|
|
<h4><a name="APIUsage-Note"></a>Note</h4>
|
|
|
|
<table cellpadding='5' width='85%' cellspacing='8px' class='noteMacro' border="0" align='center'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="./icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b class="strong">Be Careful</b><br />
|
|
<p>If a NeedsRefreshException is raised you have to invoke admin.putInCache or even admin.cancelUpdate to avoid deadlock situation.</p></td></tr></table>
|
|
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|