You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by ArjunReddy <in...@yahoo.com> on 2009/04/17 23:22:12 UTC
0penJPA QueryCaching
Hi All,
I am trying to cache the query results generated and later on use the results from the cache instead of touching the database.
This is what I am doing to put/get the values from the cache.
**********
final String queryString = "select model from StaticContent model";
Query query = getEntityManager().createQuery(queryString);
OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
QueryResultCacheImpl queryResultCache = (QueryResultCacheImpl)oemf.getQueryResultCache();
QueryCache qCache = queryResultCache.getDelegate();
qCache.put(key, (QueryResult)query.getResultList()); // Put into the cache
QueryResult cachedItems = qCache.get(key); // Get from the cache
**********
The thing I don't understand is how do I get the QueryKey (Referred to as key here in my code) ?
I tried doing this
QueryKey key = QueryKey.newInstance(q); but the q here in this line of code is supposed to be of type org.apache.openjpa.kernel.Query?
Can anybody help me with this please?
Thanks.
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2653013.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by ArjunReddy <in...@yahoo.com>.
Anyone?
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2661480.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by ArjunReddy <in...@yahoo.com>.
Um... Anybody?
Hi All,
I am trying to cache the query results generated and later on use the results from the cache instead of touching the database.
This is what I am doing to put/get the values from the cache.
**********
final String queryString = "select P from Parents P";
Query query = getEntityManager().createQuery(queryString);
OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
QueryResultCacheImpl queryResultCache = (QueryResultCacheImpl)oemf.getQueryResultCache();
QueryCache qCache = queryResultCache.getDelegate();
qCache.put(key, (QueryResult)query.getResultList()); // Put into the cache
QueryResult cachedItems = qCache.get(key); // Get from the cache
**********
The thing I don't understand is how do I get the QueryKey (Referred to as key here in my code) ?
I tried doing this
QueryKey key = QueryKey.newInstance(q); but the q here in this line of code is supposed to be of type org.apache.openjpa.kernel.Query?
Can anybody help me with this please?
Thanks.
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2658393.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by ArjunReddy <in...@yahoo.com>.
Hi Ravi and all,
This is what my code looks like as of now, the retrieving data from the
database part:
final String queryString = "SELECT S FROM StaticContent S JOIN S.aadses
Aads";
Query query = getEntityManager().createQuery(queryString);
List abc = query.getResultList();
When I debug it, this is what I am getting. Please look at the image
attached.
http://n2.nabble.com/file/n2685502/QueryCache.jpg
I am able to retrieve everything from the database but the QueryCache is
null. My Persistence.xml file has the three lines
<property name="openjpa.DataCache" value="true"/>
<property name="openjpa.RemoteCommitProvider" value="sjvm"/>
<property name="openjpa.QueryCache" value="true"/>
>From this, maybe can anyone tell me what I am doing wrong?
Thanks.
ArjunReddy wrote:
>
> Hi Ravi,
>
> Thanks for replying. I will debug and see... right now I see null next to
> QueryCache but I will check again because I might be doing something wrong
> somewhere. I have two more questions actually.
>
> 1). When you said the below thing, I guess you were talking about the
> Query Key right? and if you were, then you were right because I was able
> to retrieve the Query key when I did the below thing.
>
> *************
> CacheMap cmap =
> ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
>
> The above will give you a map and MAY be cmap.keySet() or
> cmap.getPinnedKeys() should give you set of keys.
>
> *************
>
> 2). In the link you gave me, it says that "The object id list is not
> cached until the list returned at query execution time is fully traversed.
> " Does that mean, I have to like Iterate through the list in order for the
> object ids to get cached?
>
> Thanks.
>
>
> rpalache wrote:
>>
>> Hi,
>>
>> Did you get a chance to go through the link I pasted in previous reply:
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
>> ?
>>
>> I this openJPA is already doing what ever you are trying to do.
>> So, you do not need to do anything in your application.
>>
>> You can turn on debugging and see if the call is going to database or the
>> results are returned from cache.
>>
>> Regards,
>> Ravi.
>>
>>
>> ArjunReddy wrote:
>>>
>>> Hi Ravi,
>>>
>>> Thanks a lot for replying. I tried doing what you have suggested.
>>>
>>> This is what I have done.
>>>
>>> final String queryString = "select P from Parents P";
>>> javax.persistence.Query query =
>>> getEntityManager().createQuery(queryString);
>>>
>>> QueryResultCacheImpl qcache = (QueryResultCacheImpl)
>>> (OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory())).getQueryResultCache();
>>> // I have casted to QueryResultCacheImpl because
>>> QueryResultCache.getDelegate() is deprecated and the API suggets using
>>> QueryResultCacheImpl.
>>>
>>> qcache.pin(query); //Pinning the query results to the cache
>>>
>>> ConcurrentQueryCache cqc = (ConcurrentQueryCache)qcache.getDelegate();
>>> CacheMap cmap = cqc.getCacheMap();
>>>
>>> Iterator it = cmap.keySet().iterator(); //cmap.getPinnedKeys() is also
>>> returning the same result.
>>>
>>> while(it.hasNext())
>>> {
>>> System.out.println(it.next());
>>> }
>>>
>>>
>>> And this is the result I got:
>>>
>>> org.apache.openjpa.datacache.QueryKey@28b16efe[query:[select P from
>>> Parents P],access
>>> path:[com.xyz.Parents],subs:true,ignoreChanges:false,startRange:0,endRange:9223372036854775807,timeout:-1]
>>>
>>>
>>> It's returning back the query instead of the query results.:-( I just
>>> want to be able to put my query results (Instances of the Parents Class
>>> which are rows of the Parents table in the database) into the cache and
>>> retrieve it back and see if I am getting them right. Any suggestions as
>>> what I should be doing? Do I have to do something with the DataCache? I
>>> have these defined in my persistence.xml file.
>>>
>>> <property name="openjpa.DataCache" value="true"/>
>>> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>>> <property name="openjpa.QueryCache" value="true"/>
>>>
>>> Also in my Parents table, I have DataCache defined as follows.
>>>
>>> @Entity
>>> @DataCache
>>> @Table(name = "PARENTS", schema = "SYSTEM")
>>> public class Parents implements java.io.Serializable {
>>>
>>> ...
>>> ...
>>>
>>> }
>>>
>>>
>>> Thanks.
>>>
>>>
>>>
>>> rpalache wrote:
>>>>
>>>> Hi Arjun,
>>>>
>>>>>> I am trying to cache the query results generated and later on use
>>>>>> the results from the cache instead of touching the database.
>>>>
>>>> I think if you enable the querycache properly then openJPA has to do it
>>>> for you.
>>>>
>>>> Check this link which explains the same:
>>>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
>>>>
>>>> To enable query cache add the following in the persistence.xml:
>>>>
>>>> <property name="openjpa.DataCache" value="true"/>
>>>> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>>>> <property name="openjpa.QueryCache" value="true"/>
>>>>
>>>> I hope that answers your question.
>>>>
>>>> If not, and you are still looking to get a key then may be the
>>>> following will help:
>>>>
>>>> CacheMap cmap =
>>>> ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
>>>>
>>>> The above will give you a map and MAY be cmap.keySet() or
>>>> cmap.getPinnedKeys() should give you set of keys.
>>>>
>>>> Regards,
>>>> Ravi.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> ArjunReddy wrote:
>>>>>
>>>>> Hi All,
>>>>>
>>>>> I am trying to cache the query results generated and later on use the
>>>>> results from the cache instead of touching the database.
>>>>>
>>>>> This is what I am doing to put/get the values from the cache.
>>>>>
>>>>>
>>>>> **********
>>>>> final String queryString = "select P from Parents P";
>>>>> Query query = getEntityManager().createQuery(queryString);
>>>>>
>>>>> OpenJPAEntityManagerFactory oemf =
>>>>> OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
>>>>> QueryResultCacheImpl queryResultCache =
>>>>> (QueryResultCacheImpl)oemf.getQueryResultCache();
>>>>> QueryCache qCache = queryResultCache.getDelegate();
>>>>>
>>>>> qCache.put(key, (QueryResult)query.getResultList()); // Put into the
>>>>> cache
>>>>> QueryResult cachedItems = qCache.get(key); // Get from the cache
>>>>>
>>>>> **********
>>>>>
>>>>>
>>>>> The thing I don't understand is how do I get the QueryKey (Referred to
>>>>> as key here in my code) ?
>>>>>
>>>>> I tried doing this
>>>>> QueryKey key = QueryKey.newInstance(q); but the q here in this line of
>>>>> code is supposed to be of type org.apache.openjpa.kernel.Query?
>>>>>
>>>>>
>>>>>
>>>>> Can anybody help me with this please?
>>>>>
>>>>> Thanks.
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2685502.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by ArjunReddy <in...@yahoo.com>.
Hi Ravi,
Thanks for replying. I will debug and see... but then what were you talking
about when you said the below thing?
*************
And you are still looking to get a key then may be the following will help:
CacheMap cmap =
((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
The above will give you a map and MAY be cmap.keySet() or
cmap.getPinnedKeys() should give you set of keys.
*************
Thanks.
rpalache wrote:
>
> Hi,
>
> Did you get a chance to go through the link I pasted in previous reply:
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
> ?
>
> I this openJPA is already doing what ever you are trying to do.
> So, you do not need to do anything in your application.
>
> You can turn on debugging and see if the call is going to database or the
> results are returned from cache.
>
> Regards,
> Ravi.
>
>
> ArjunReddy wrote:
>>
>> Hi Ravi,
>>
>> Thanks a lot for replying. I tried doing what you have suggested.
>>
>> This is what I have done.
>>
>> final String queryString = "select P from Parents P";
>> javax.persistence.Query query =
>> getEntityManager().createQuery(queryString);
>>
>> QueryResultCacheImpl qcache = (QueryResultCacheImpl)
>> (OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory())).getQueryResultCache();
>> // I have casted to QueryResultCacheImpl because
>> QueryResultCache.getDelegate() is deprecated and the API suggets using
>> QueryResultCacheImpl.
>>
>> qcache.pin(query); //Pinning the query results to the cache
>>
>> ConcurrentQueryCache cqc = (ConcurrentQueryCache)qcache.getDelegate();
>> CacheMap cmap = cqc.getCacheMap();
>>
>> Iterator it = cmap.keySet().iterator(); //cmap.getPinnedKeys() is also
>> returning the same result.
>>
>> while(it.hasNext())
>> {
>> System.out.println(it.next());
>> }
>>
>>
>> And this is the result I got:
>>
>> org.apache.openjpa.datacache.QueryKey@28b16efe[query:[select P from
>> Parents P],access
>> path:[com.xyz.Parents],subs:true,ignoreChanges:false,startRange:0,endRange:9223372036854775807,timeout:-1]
>>
>>
>> It's returning back the query instead of the query results.:-( I just
>> want to be able to put my query results (Instances of the Parents Class
>> which are rows of the Parents table in the database) into the cache and
>> retrieve it back and see if I am getting them right. Any suggestions as
>> what I should be doing? Do I have to do something with the DataCache? I
>> have these defined in my persistence.xml file.
>>
>> <property name="openjpa.DataCache" value="true"/>
>> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>> <property name="openjpa.QueryCache" value="true"/>
>>
>> Also in my Parents table, I have DataCache defined as follows.
>>
>> @Entity
>> @DataCache
>> @Table(name = "PARENTS", schema = "SYSTEM")
>> public class Parents implements java.io.Serializable {
>>
>> ...
>> ...
>>
>> }
>>
>>
>> Thanks.
>>
>>
>>
>> rpalache wrote:
>>>
>>> Hi Arjun,
>>>
>>>>> I am trying to cache the query results generated and later on use the
>>>>> results from the cache instead of touching the database.
>>>
>>> I think if you enable the querycache properly then openJPA has to do it
>>> for you.
>>>
>>> Check this link which explains the same:
>>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
>>>
>>> To enable query cache add the following in the persistence.xml:
>>>
>>> <property name="openjpa.DataCache" value="true"/>
>>> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>>> <property name="openjpa.QueryCache" value="true"/>
>>>
>>> I hope that answers your question.
>>>
>>> If not, and you are still looking to get a key then may be the following
>>> will help:
>>>
>>> CacheMap cmap =
>>> ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
>>>
>>> The above will give you a map and MAY be cmap.keySet() or
>>> cmap.getPinnedKeys() should give you set of keys.
>>>
>>> Regards,
>>> Ravi.
>>>
>>>
>>>
>>>
>>>
>>> ArjunReddy wrote:
>>>>
>>>> Hi All,
>>>>
>>>> I am trying to cache the query results generated and later on use the
>>>> results from the cache instead of touching the database.
>>>>
>>>> This is what I am doing to put/get the values from the cache.
>>>>
>>>>
>>>> **********
>>>> final String queryString = "select P from Parents P";
>>>> Query query = getEntityManager().createQuery(queryString);
>>>>
>>>> OpenJPAEntityManagerFactory oemf =
>>>> OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
>>>> QueryResultCacheImpl queryResultCache =
>>>> (QueryResultCacheImpl)oemf.getQueryResultCache();
>>>> QueryCache qCache = queryResultCache.getDelegate();
>>>>
>>>> qCache.put(key, (QueryResult)query.getResultList()); // Put into the
>>>> cache
>>>> QueryResult cachedItems = qCache.get(key); // Get from the cache
>>>>
>>>> **********
>>>>
>>>>
>>>> The thing I don't understand is how do I get the QueryKey (Referred to
>>>> as key here in my code) ?
>>>>
>>>> I tried doing this
>>>> QueryKey key = QueryKey.newInstance(q); but the q here in this line of
>>>> code is supposed to be of type org.apache.openjpa.kernel.Query?
>>>>
>>>>
>>>>
>>>> Can anybody help me with this please?
>>>>
>>>> Thanks.
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2671124.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by rpalache <ra...@oracle.com>.
Hi,
Did you get a chance to go through the link I pasted in previous reply:
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
?
I this openJPA is already doing what ever you are trying to do.
So, you do not need to do anything in your application.
You can turn on debugging and see if the call is going to database or the
results are returned from cache.
Regards,
Ravi.
ArjunReddy wrote:
>
> Hi Ravi,
>
> Thanks a lot for replying. I tried doing what you have suggested.
>
> This is what I have done.
>
> final String queryString = "select P from Parents P";
> javax.persistence.Query query =
> getEntityManager().createQuery(queryString);
>
> QueryResultCacheImpl qcache = (QueryResultCacheImpl)
> (OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory())).getQueryResultCache();
> // I have casted to QueryResultCacheImpl because
> QueryResultCache.getDelegate() is deprecated and the API suggets using
> QueryResultCacheImpl.
>
> qcache.pin(query); //Pinning the query results to the cache
>
> ConcurrentQueryCache cqc = (ConcurrentQueryCache)qcache.getDelegate();
> CacheMap cmap = cqc.getCacheMap();
>
> Iterator it = cmap.keySet().iterator(); //cmap.getPinnedKeys() is also
> returning the same result.
>
> while(it.hasNext())
> {
> System.out.println(it.next());
> }
>
>
> And this is the result I got:
>
> org.apache.openjpa.datacache.QueryKey@28b16efe[query:[select P from
> Parents P],access
> path:[com.xyz.Parents],subs:true,ignoreChanges:false,startRange:0,endRange:9223372036854775807,timeout:-1]
>
>
> It's returning back the query instead of the query results.:-( I just want
> to be able to put my query results (Instances of the Parents Class which
> are rows of the Parents table in the database) into the cache and retrieve
> it back and see if I am getting them right. Any suggestions as what I
> should be doing? Do I have to do something with the DataCache? I have
> these defined in my persistence.xml file.
>
> <property name="openjpa.DataCache" value="true"/>
> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
> <property name="openjpa.QueryCache" value="true"/>
>
> Also in my Parents table, I have DataCache defined as follows.
>
> @Entity
> @DataCache
> @Table(name = "PARENTS", schema = "SYSTEM")
> public class Parents implements java.io.Serializable {
>
> ...
> ...
>
> }
>
>
> Thanks.
>
>
>
> rpalache wrote:
>>
>> Hi Arjun,
>>
>>>> I am trying to cache the query results generated and later on use the
>>>> results from the cache instead of touching the database.
>>
>> I think if you enable the querycache properly then openJPA has to do it
>> for you.
>>
>> Check this link which explains the same:
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
>>
>> To enable query cache add the following in the persistence.xml:
>>
>> <property name="openjpa.DataCache" value="true"/>
>> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>> <property name="openjpa.QueryCache" value="true"/>
>>
>> I hope that answers your question.
>>
>> If not, and you are still looking to get a key then may be the following
>> will help:
>>
>> CacheMap cmap =
>> ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
>>
>> The above will give you a map and MAY be cmap.keySet() or
>> cmap.getPinnedKeys() should give you set of keys.
>>
>> Regards,
>> Ravi.
>>
>>
>>
>>
>>
>> ArjunReddy wrote:
>>>
>>> Hi All,
>>>
>>> I am trying to cache the query results generated and later on use the
>>> results from the cache instead of touching the database.
>>>
>>> This is what I am doing to put/get the values from the cache.
>>>
>>>
>>> **********
>>> final String queryString = "select P from Parents P";
>>> Query query = getEntityManager().createQuery(queryString);
>>>
>>> OpenJPAEntityManagerFactory oemf =
>>> OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
>>> QueryResultCacheImpl queryResultCache =
>>> (QueryResultCacheImpl)oemf.getQueryResultCache();
>>> QueryCache qCache = queryResultCache.getDelegate();
>>>
>>> qCache.put(key, (QueryResult)query.getResultList()); // Put into the
>>> cache
>>> QueryResult cachedItems = qCache.get(key); // Get from the cache
>>>
>>> **********
>>>
>>>
>>> The thing I don't understand is how do I get the QueryKey (Referred to
>>> as key here in my code) ?
>>>
>>> I tried doing this
>>> QueryKey key = QueryKey.newInstance(q); but the q here in this line of
>>> code is supposed to be of type org.apache.openjpa.kernel.Query?
>>>
>>>
>>>
>>> Can anybody help me with this please?
>>>
>>> Thanks.
>>>
>>>
>>
>>
>
>
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2668021.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by ArjunReddy <in...@yahoo.com>.
Hi Ravi,
Thanks a lot for replying. I tried doing what you have suggested.
This is what I have done.
final String queryString = "select P from Parents P";
javax.persistence.Query query = getEntityManager().createQuery(queryString);
QueryResultCacheImpl qcache = (QueryResultCacheImpl)
(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory())).getQueryResultCache();
// I have casted to QueryResultCacheImpl because
QueryResultCache.getDelegate() is deprecated and the API suggets using
QueryResultCacheImpl.
qcache.pin(query); //Pinning the query results to the cache
ConcurrentQueryCache cqc = (ConcurrentQueryCache)qcache.getDelegate();
CacheMap cmap = cqc.getCacheMap();
Iterator it = cmap.keySet().iterator(); //cmap.getPinnedKeys() is also
returning the same result.
while(it.hasNext())
{
System.out.println(it.next());
}
And this is the result I got:
org.apache.openjpa.datacache.QueryKey@28b16efe[query:[select P from Parents
P],access
path:[com.xyz.Parents],subs:true,ignoreChanges:false,startRange:0,endRange:9223372036854775807,timeout:-1]
It's returning back the query instead of the query results.:-( I just want
to be able to put my query results (Instances of the Parents Class which are
rows of the Parents table in the database) into the cache and retrieve it
back and see if I am getting them right. Any suggestions as what I should be
doing? Do I have to do something with the DataCache? I have these defined in
my persistence.xml file.
<property name="openjpa.DataCache" value="true"/>
<property name="openjpa.RemoteCommitProvider" value="sjvm"/>
<property name="openjpa.QueryCache" value="true"/>
Also in my Parents table, I have DataCache defined as follows.
@Entity
@DataCache
@Table(name = "PARENTS", schema = "SYSTEM")
public class Parents implements java.io.Serializable {
...
...
}
Thanks.
rpalache wrote:
>
> Hi Arjun,
>
>>> I am trying to cache the query results generated and later on use the
>>> results from the cache instead of touching the database.
>
> I think if you enable the querycache properly then openJPA has to do it
> for you.
>
> Check this link which explains the same:
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
>
> To enable query cache add the following in the persistence.xml:
>
> <property name="openjpa.DataCache" value="true"/>
> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
> <property name="openjpa.QueryCache" value="true"/>
>
> I hope that answers your question.
>
> If not, and you are still looking to get a key then may be the following
> will help:
>
> CacheMap cmap =
> ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
>
> The above will give you a map and MAY be cmap.keySet() or
> cmap.getPinnedKeys() should give you set of keys.
>
> Regards,
> Ravi.
>
>
>
>
>
> ArjunReddy wrote:
>>
>> Hi All,
>>
>> I am trying to cache the query results generated and later on use the
>> results from the cache instead of touching the database.
>>
>> This is what I am doing to put/get the values from the cache.
>>
>>
>> **********
>> final String queryString = "select P from Parents P";
>> Query query = getEntityManager().createQuery(queryString);
>>
>> OpenJPAEntityManagerFactory oemf =
>> OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
>> QueryResultCacheImpl queryResultCache =
>> (QueryResultCacheImpl)oemf.getQueryResultCache();
>> QueryCache qCache = queryResultCache.getDelegate();
>>
>> qCache.put(key, (QueryResult)query.getResultList()); // Put into the
>> cache
>> QueryResult cachedItems = qCache.get(key); // Get from the cache
>>
>> **********
>>
>>
>> The thing I don't understand is how do I get the QueryKey (Referred to as
>> key here in my code) ?
>>
>> I tried doing this
>> QueryKey key = QueryKey.newInstance(q); but the q here in this line of
>> code is supposed to be of type org.apache.openjpa.kernel.Query?
>>
>>
>>
>> Can anybody help me with this please?
>>
>> Thanks.
>>
>>
>
>
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2664742.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.
Re: 0penJPA QueryCaching
Posted by rpalache <ra...@oracle.com>.
Hi Arjun,
>> I am trying to cache the query results generated and later on use the results from the cache instead of touching the database.
I think if you enable the querycache properly then openJPA has to do it for you.
Check this link which explains the same:
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_cache_query
To enable query cache add the following in the persistence.xml:
<property name="openjpa.DataCache" value="true"/>
<property name="openjpa.RemoteCommitProvider" value="sjvm"/>
<property name="openjpa.QueryCache" value="true"/>
I hope that answers your question.
If not, and you are still looking to get a key then may be the following will help:
CacheMap cmap = ((ConcurrentQueryCache)(OpenJPAPersistence.cast(EntityManager.getEntityManagerFactory()).getQueryResultCache().getDelegate())).getCacheMap();
The above will give you a map and MAY be cmap.keySet() or cmap.getPinnedKeys() should give you set of keys.
Regards,
Ravi.
Hi All,
I am trying to cache the query results generated and later on use the results from the cache instead of touching the database.
This is what I am doing to put/get the values from the cache.
**********
final String queryString = "select P from Parents P";
Query query = getEntityManager().createQuery(queryString);
OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(EntityManagerHelper.getEmf());
QueryResultCacheImpl queryResultCache = (QueryResultCacheImpl)oemf.getQueryResultCache();
QueryCache qCache = queryResultCache.getDelegate();
qCache.put(key, (QueryResult)query.getResultList()); // Put into the cache
QueryResult cachedItems = qCache.get(key); // Get from the cache
**********
The thing I don't understand is how do I get the QueryKey (Referred to as key here in my code) ?
I tried doing this
QueryKey key = QueryKey.newInstance(q); but the q here in this line of code is supposed to be of type org.apache.openjpa.kernel.Query?
Can anybody help me with this please?
Thanks.
--
View this message in context: http://n2.nabble.com/0penJPA-QueryCaching-tp2653013p2661607.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.