<commit_before>/*
 *    Copyright 2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.caches.memcached;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;

import net.spy.memcached.MemcachedClient;

import org.apache.ibatis.cache.CacheException;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

/**
 * @version $Id$
 */
final class MemcachedClientWrapper {

    /**
     * This class log.
     */
    private final Log log = LogFactory.getLog(MemcachedCache.class);

    private final MemcachedConfiguration configuration;

    private final MemcachedClient client;

    public MemcachedClientWrapper() {
        configuration = MemcachedConfigurationBuilder.getInstance().parseConfiguration();
        try {
            client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
        } catch (IOException e) {
            String message = "Impossible to instantiate a new memecached client instance, see nested exceptions";
            log.error(message, e);
            throw new RuntimeException(message, e);
        }

        if (log.isDebugEnabled()) {
            log.debug("Running new Memcached client using " + configuration);
        }
    }

    /**
     * Converts the iBatis object key in the proper string representation.
     *
     * @param key the iBatis object key.
     * @return the proper string representation.
     */
    private String toKeyString(final Object key) {
        String keyString = configuration.getKeyPrefix() + Integer.toHexString(key.hashCode());
        if (log.isDebugEnabled()) {
            log.debug("Object key '"
                    + key
                    + "' converted in '"
                    + keyString
                    + "'");
        }
        return keyString;
    }

    /**
     *
     * @param key
     * @return
     */
    public Object getObject(Object key) {<commit_msg>Issue 660:	Memcached CacheKey collision<commit_after>/*
 *    Copyright 2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.caches.memcached;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;

import net.spy.memcached.MemcachedClient;

import org.apache.ibatis.cache.CacheException;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

/**
 * @version $Id$
 */
final class MemcachedClientWrapper {

    /**
     * This class log.
     */
    private final Log log = LogFactory.getLog(MemcachedCache.class);

    private final MemcachedConfiguration configuration;

    private final MemcachedClient client;

    public MemcachedClientWrapper() {
        configuration = MemcachedConfigurationBuilder.getInstance().parseConfiguration();
        try {
            client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
        } catch (IOException e) {
            String message = "Impossible to instantiate a new memecached client instance, see nested exceptions";
            log.error(message, e);
            throw new RuntimeException(message, e);
        }

        if (log.isDebugEnabled()) {
            log.debug("Running new Memcached client using " + configuration);
        }
    }

    /**
     * Converts the iBatis object key in the proper string representation.
     *
     * @param key the iBatis object key.
     * @return the proper string representation.
     */
    private String toKeyString(final Object key) {
        String keyString = configuration.getKeyPrefix() + key; // issue #660, key collision
        if (log.isDebugEnabled()) {
            log.debug("Object key '"
                    + key
                    + "' converted in '"
                    + keyString
                    + "'");
        }
        return keyString;
    }

    /**
     *
     * @param key
     * @return
     */
    public Object getObject(Object key) {
{'fraction_non_alphanumeric': 0.08989412897016362, 'fraction_numerical': 0.004234841193455245, 'mean_word_length': 2.529891304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2893987', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1324, 'n_words': 430}