Objectify, the @Ignore annotation and caching

409 Views Asked by At

I'm using Objectify and have a base entity that all others descend from. This base entity has an non-persisted updated flag that is set by descendant classes when there is a change to an object property value that needs to be saved. I do this to prevent needless writes to the datastore when syncing data with clients.

The base entity looks like this

@Cache
public abstract class WordBuzzEntity {

    @Ignore
    private boolean updated = false;

    public boolean isUpdated() {
        return updated;
    }

    public void setUpdated() {
        updated = true;
    }

    public void save(boolean async) {
        if (!updated)
            return;

        if (async)
            ofy().save().entity(this);
        else
            ofy().save().entity(this).now();
    }
}

I noticed when loading users with

User user = ofy().load().type(User.class).id(LoginTest.TEST_ID).now();

That the updated flag was sometimes set to true at the point of load.

Is this because of the Objectify session cache or memcache? Are ignored properties cached in this instance when an object is reloaded?

Adding a line to set updated back to false at the point of save resolves my issue, but I'd like to understand what's going on.

1

There are 1 best solutions below

2
On BEST ANSWER

The Objectify session cache stores instances of objects, therefore ignored fields are not actually ignored when an item is loaded from the cache as it just pulls up the last instance rather than creating a new one.