cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Caching Query Results
Date Thu, 17 Feb 2011 07:07:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/9/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAYDOC/Caching+Query+Results?focusedCommentId=25202267#comment-25202267">Caching
Query Results</a></h2>
        <h4>Page
        <b>comment added</b> by              <a href="https://cwiki.apache.org/confluence/display/~grobmeier">Christian
Grobmeier</a>
    </h4>
    <br/>
    <div class="notificationGreySide">
       <p>Starting from:<br/>
List objects1 = context.performQuery("MyQuery", false);<br/>
...<br/>
it should be:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
NamedQuery query = <span class="code-keyword">new</span> NamedQuery(<span class="code-quote">"MyQuery"</span>);
List objects = context.performQuery(query, <span class="code-keyword">false</span>);
</pre>
</div></div>

<p>By default cached results are used. Now if we want to force refresh, we need to use
add the following (for just this invocation - this does not affect the underlying saved query)</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
NamedQuery query = <span class="code-keyword">new</span> NamedQuery(<span class="code-quote">"MyQuery"</span>);
query.setForceNoCache(<span class="code-keyword">true</span>);
List objects = context.performQuery(query, <span class="code-keyword">false</span>);
</pre>
</div></div>

<p>Note that parameterized named queries will still work correctly with the cache. We've
already mentioned that the users must ensure that two queries must have different names if
they fetch logically different data. This is NOT the case with queries stored in the DataMap.
If you run the same named query with different sets of parameters, Cayenne will internally
generate unique cache keys for each distinct parameter set.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Map&lt;<span class="code-object">String</span>,?&gt; parameters = Collections.singletonMap(<span
class="code-quote">"key"</span>, <span class="code-quote">"value1"</span>);
NamedQuery query = <span class="code-keyword">new</span> NamedQuery(<span class="code-quote">"MyQuery"</span>,
parameters);
List objects = context.performQuery(query);
</pre>
</div></div>

<p>Now if we run the same query with a different set of parameters, Cayenne will do
the right thing and create a separate entry in the cache:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Map&lt;<span class="code-object">String</span>,?&gt; parameters = Collections.singletonMap(<span
class="code-quote">"key"</span>, <span class="code-quote">"value2"</span>);
NamedQuery query = <span class="code-keyword">new</span> NamedQuery(<span class="code-quote">"MyQuery"</span>,
parameters);
List objects = context.performQuery(query);
</pre>
</div></div>
    </div>

        
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="https://cwiki.apache.org/confluence/display/CAYDOC/Caching+Query+Results?focusedCommentId=25202267#comment-25202267">View
Online</a>
              |
       <a id="reply-25202267" href="https://cwiki.apache.org/confluence/display/CAYDOC/Caching+Query+Results?replyToComment=25202267#comment-25202267">Reply
To This</a>
           </div>

</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message