File: API%20Usage.html

package info (click to toggle)
oscache 2.4.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 7,004 kB
  • ctags: 2,079
  • sloc: java: 9,014; xml: 2,238; jsp: 574; makefile: 11; sh: 11
file content (76 lines) | stat: -rw-r--r-- 4,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<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>