<commit_before>package org.commcare.models;

import org.commcare.CommCareApplication;
import org.commcare.models.database.SqlStorage;
import org.javarosa.core.services.ExpressionCacher;
import org.javarosa.xpath.expr.CachedExpression;
import org.javarosa.xpath.expr.InFormCacheableExpr;

/**
 * Created by amstone326 on 1/10/18.
 */

public class AndroidInFormExpressionCacher extends ExpressionCacher {

    public AndroidInFormExpressionCacher() {
    }

    @Override
    public int cache(InFormCacheableExpr expression, Object value) {
        CachedExpression cached = new CachedExpression(expression, value);
        getCacheStorage().write(cached);
        return cached.getID();
    }

    @Override
    public Object getCachedValue(int idOfStoredCache) {
        CachedExpression cached = getCacheStorage().read(idOfStoredCache);
        if (cached == null) {
            return null;
        } else {
            return cached.getEvalResult();
        }
    }

    @Override
    public boolean environmentValidForCaching() {
        return true;
    }

    @Override
    public void wipeCacheStorage() {
        getCacheStorage().removeAll();
    }

    private SqlStorage<CachedExpression> getCacheStorage() {
        return CommCareApplication.instance()
                .getUserStorage(CachedExpression.STORAGE_KEY, CachedExpression.class);
    }

}
<commit_msg>don't load up storage multiple times<commit_after>package org.commcare.models;

import org.commcare.CommCareApplication;
import org.commcare.models.database.SqlStorage;
import org.javarosa.core.services.ExpressionCacher;
import org.javarosa.xpath.expr.CachedExpression;
import org.javarosa.xpath.expr.InFormCacheableExpr;

/**
 * Created by amstone326 on 1/10/18.
 */

public class AndroidInFormExpressionCacher extends ExpressionCacher {

    SqlStorage<CachedExpression> cacheStorage;

    public AndroidInFormExpressionCacher() {
        cacheStorage = CommCareApplication.instance()
                .getUserStorage(CachedExpression.STORAGE_KEY, CachedExpression.class);
    }

    @Override
    public int cache(InFormCacheableExpr expression, Object value) {
        CachedExpression cached = new CachedExpression(expression, value);
        cacheStorage.write(cached);
        return cached.getID();
    }

    @Override
    public Object getCachedValue(int idOfStoredCache) {
        CachedExpression cached = cacheStorage.read(idOfStoredCache);
        if (cached == null) {
            return null;
        } else {
            return cached.getEvalResult();
        }
    }

    @Override
    public boolean environmentValidForCaching() {
        return true;
    }

    @Override
    public void wipeCacheStorage() {
        cacheStorage.removeAll();
    }

}

{'fraction_non_alphanumeric': 0.08703568827385287, 'fraction_numerical': 0.005826656955571741, 'mean_word_length': 3.2003058103975537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6270959', 'n_tokens_mistral': 783, 'n_tokens_neox': 751, 'n_words': 150}