<commit_before>package com.github.ryanbrainard.richsobjects.api.client;

import com.github.ryanbrainard.richsobjects.api.model.BasicSObjectInformation;
import com.github.ryanbrainard.richsobjects.api.model.GlobalDescription;
import com.github.ryanbrainard.richsobjects.api.model.QueryResult;
import com.github.ryanbrainard.richsobjects.api.model.SObjectDescription;
import net.spy.memcached.ConnectionFactory;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.PlainCallbackHandler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Map;

/**
 * @author <NAME>
 */
public class MemcachedUserCache implements SfdcApiUserCache {

    static final MemcachedClient memcachedClient;

    static {
        AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(
                System.getenv("MEMCACHE_USERNAME"),
                System.getenv("MEMCACHE_PASSWORD")));
        ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder();
        ConnectionFactory cf = factoryBuilder
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setAuthDescriptor(ad).setTranscoder(new ClassLoaderRegisteringSerializingTranscoder())
                .build();

        try {
            memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(System.getenv("MEMCACHE_SERVERS"), 11211)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private final String userKey;
    private final SfdcApiClient apiClient;
    
    public MemcachedUserCache(String userKey, SfdcApiClient apiClient) {
        this.userKey = userKey;
        this.apiClient = apiClient;
    }

    private String fullyQualifiedKey(String entityKey) {
        return userKey + entityKey;
    }

    private <T> T loadEntity(final String entityKey, final ValueProvider<T> provider) {
        final Object fromCache = memcachedClient.get(fullyQualifiedKey(entityKey));

        if (fromCache != null) {
            //noinspection unchecked<commit_msg>throw if memcached env vars are missing<commit_after>package com.github.ryanbrainard.richsobjects.api.client;

import com.github.ryanbrainard.richsobjects.api.model.BasicSObjectInformation;
import com.github.ryanbrainard.richsobjects.api.model.GlobalDescription;
import com.github.ryanbrainard.richsobjects.api.model.QueryResult;
import com.github.ryanbrainard.richsobjects.api.model.SObjectDescription;
import net.spy.memcached.ConnectionFactory;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.PlainCallbackHandler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Map;

/**
 * @author <NAME>
 */
public class MemcachedUserCache implements SfdcApiUserCache {

    static final MemcachedClient memcachedClient;

    static {
        AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(
                getEnvOrThrow("MEMCACHE_USERNAME"),
                getEnvOrThrow("MEMCACHE_PASSWORD")));
        ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder();
        ConnectionFactory cf = factoryBuilder
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setAuthDescriptor(ad).setTranscoder(new ClassLoaderRegisteringSerializingTranscoder())
                .build();

        try {
            memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(getEnvOrThrow("MEMCACHE_SERVERS"), 11211)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String getEnvOrThrow(String name) {
        final String value = System.getenv(name);
        if (value == null) {
            throw new NullPointerException("[" + name + "] is must not be null");
        }
        return value;
    }

    private final String userKey;
    private final SfdcApiClient apiClient;
    
    public MemcachedUserCache(String userKey, SfdcApiClient apiClient) {
        this.userKey = userKey;
        this.apiClient = apiClient;
    }

    private String fullyQualifiedKey(String entityKey) {
        return userKey + entityKey;
    }

    private <T> T loadEntity(final String entityKey, final ValueProvider<T> provider) {
        final Object fromCache = memcachedClient.get(fullyQualifiedKey(entityKey));

        if (fromCache != null) {
            //noinspection unchecked
{'fraction_non_alphanumeric': 0.08730325288562435, 'fraction_numerical': 0.002098635886673662, 'mean_word_length': 3.618217054263566, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7595687', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1286, 'n_words': 258}