You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Lon Varscsak <lo...@gmail.com> on 2016/11/16 22:41:12 UTC

Shared Cache and relationships

So previously this year, I posted about about how while fetching an object
that it’s relationships weren’t automatically refreshing, even though I was
not caching (myself) any data.  This is where I learned about “shared
caches” and realized that if I turned it off, I kind of get the behavior I
want (except now peer synchronization doesn’t work).  Data within a single
context is cached (if I choose), but once the context is gone, the
snapshots are gone.  It doesn’t seem to make sense that a global cache
would hold onto snapshots beyond the life of the objects across
contexts…but that’s what it appears to do.

The response was, “refresh your relationships with prefetches”.  This
doesn’t seem like a clean solution to me.  It forces the developer to “be
aware” of stale data and force fetches on relationships that they might not
need in that moment.  Realize that a lot of components in my application
might shared data (like a Customer, or an Order), but the components don’t
necessarily access all of the information in those relationships.  I
believe EOF, the snapshot would decrement to 0 (when that snapshot wasn’t
referred to anymore by any context) and the next fetch would be fresh.  Is
there no concept like this for Cayenne?

Any help would be appreciated.  It’s very possible I’m just missing the
obvious approach.

-Lon

Re: Shared Cache and relationships

Posted by do...@xsinet.co.za.
Hi Lon

Here is my kludge/hack of how I handle the problem of when "fetching an 
object that it\u2019s relationships weren\u2019t automatically refreshing".
It's based on Cayenne storing a fault object in the data map to indicate 
that the relationship hasn't been fetched yet.
Note, that I don't think this works if the relationship is being prefetched.

public class Patient extends _Patient
{
    private Object  historyFault;
    private long  lastHistoryAccess;
    private final long  ONE_SECOND = 1000;

    @Override
    public PatientHistory getMedicalHistory()
    {
        if ( historyFault == null )  historyFault = readPropertyDirectly( 
MEDICAL_HISTORY_PROPERTY );
        else  if ( System.currentTimeMillis() - lastHistoryAccess > 
ONE_SECOND )
        {
            getObjectContext().invalidateObjects( 
super.getMedicalHistory() );       // Forces refresh of objects.
            writePropertyDirectly( MEDICAL_HISTORY_PROPERTY, historyFault ); 
// Forces refetch.
        }
        lastHistoryAccess = System.currentTimeMillis();
        return = super.getMedicalHistory();
    }
}

If you only want to refetch in specific instances then put the "else" clause 
into a separate method and call that when appropriate.

Hope this helps, regards
Jurgen


-----Original Message----- 
From: Lon Varscsak
Sent: Thursday, November 17, 2016 12:41 AM
To: Apache Cayenne
Subject: Shared Cache and relationships

So previously this year, I posted about about how while fetching an object
that it\u2019s relationships weren\u2019t automatically refreshing, even though I was
not caching (myself) any data.  This is where I learned about \u201cshared
caches\u201d and realized that if I turned it off, I kind of get the behavior I
want (except now peer synchronization doesn\u2019t work).  Data within a single
context is cached (if I choose), but once the context is gone, the
snapshots are gone.  It doesn\u2019t seem to make sense that a global cache
would hold onto snapshots beyond the life of the objects across
contexts\u2026but that\u2019s what it appears to do.

The response was, \u201crefresh your relationships with prefetches\u201d.  This
doesn\u2019t seem like a clean solution to me.  It forces the developer to \u201cbe
aware\u201d of stale data and force fetches on relationships that they might not
need in that moment.  Realize that a lot of components in my application
might shared data (like a Customer, or an Order), but the components don\u2019t
necessarily access all of the information in those relationships.  I
believe EOF, the snapshot would decrement to 0 (when that snapshot wasn\u2019t
referred to anymore by any context) and the next fetch would be fresh.  Is
there no concept like this for Cayenne?

Any help would be appreciated.  It\u2019s very possible I\u2019m just missing the
obvious approach.

-Lon