Objectify: How to disable session cache?

I like Objectify "just use" (") to get an instance of Objectify, but I use a use case where I could use some tips.

My use case for a data warehouse is such that in one part of my process I will write objects in a lengthy process. Hundreds of thousands of objects. They will be well scattered across time / entity groups (so the datastore statement is not a problem for me). During this lengthy process, I will not need to read the storage object more than once.

I know that I can disable the second level cache using Objectify.cache (false) to create an instance that memecache will not use. It's great.

My problem is with the session cache. I just looked a bit into the Objectify code, and it seems that in WriteEngine.java, when we do "save ()" for the entity we are facing:

                // Also stuff this in the session
                session.addValue(key, obj);

So does objectification hold my objects in memory? I would like to disable saving objects in any kind of cache.

+4
source share
2 answers

Unfortunately, now the only way to do this is to periodically call ofy().clear(). I see that you added the problem to the github tracker, which is good.

+1
source

, ... @stickfigure answer - Objectify. , . , Objectify ... , , ! , , , , , - .

, Objectify , . OfyService,

import static com.industryopenings.seeker.shared.OfyService.ofyw;

. ofyw(), ofy(). , ... ofyr() ofyw() . . .

OfyService ...

  • , Objectify, ofyw(), cache(false) cache(true)
  • clear() OFYW_USE_COUNT_THRESHOLD ofyw()

, ... . , ofyw() ( Objectify) .

import com.googlecode.objectify.*;
import com.googlecode.objectify.impl.ObjectifyImpl;

public final class OfyService {
    public final static int OFYW_USE_COUNT_THRESHOLD = 10;
    public static int currentReferenceCount = 0;
    private static ObjectifyFactory defaultFactory = new ObjectifyFactory();
    private static ObjectifyFactory noCacheFactory = new NoCacheFactory();

    // This factory always returns a no-cache instance.
    // BUT: end users can be foolish and turn it back on if they want... having potentially ripple effects downstream
    // Best practice if you're going to use this OfyService is to never call the cache(boolean) method
    private static class NoCacheFactory extends ObjectifyFactory {
        @Override
        public Objectify begin() {
            return new ObjectifyImpl<>(this).cache(false);
        }
    }

    // Note!  We probably need to register our classes in both factories
    static {
        defaultFactory.register(Doc.class);
        defaultFactory.register(SiteLogEntry.class);
        defaultFactory.register(SiteLogRunSummary.class);

        noCacheFactory.register(Doc.class);
        noCacheFactory.register(SiteLogEntry.class);
        noCacheFactory.register(SiteLogRunSummary.class);
    }

    // ofyr to get an Objectify... with the "r" to denote read / caching enabled.
    public static Objectify ofyr() {
        ObjectifyService.setFactory(defaultFactory);
        return ObjectifyService.ofy();
    }

    // ofyr to get an Objectify... with the "w" to denote writing / no caching enabled.
    public static Objectify ofyw(){
        // This will ensure we're not using second level cache (memecache)
        ObjectifyService.setFactory(noCacheFactory); 

        // In lieu of a true solution we're simply going to clean house every X times ofyw() is called
        currentReferenceCount++;
        Objectify o = ObjectifyService.ofy();
        if(currentReferenceCount > OFYW_USE_COUNT_THRESHOLD){
            o.clear();
            currentReferenceCount = 0;
        }
        return o;
    }
}

, - .

+2

Source: https://habr.com/ru/post/1627465/


All Articles