<commit_before>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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.springframework.cloud.aws.cache.redis;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.cache.Cache;
import org.springframework.cloud.aws.cache.AbstractCacheFactory;
import org.springframework.data.redis.cache.PublicRedisCache;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.srp.SrpConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ClassUtils;
import redis.clients.jedis.JedisShardInfo;

/**
 * @author <NAME>
 */
public class RedisCacheFactory extends AbstractCacheFactory<RedisConnectionFactory> {

	private static final boolean JEDIS_AVAILABLE = ClassUtils.isPresent("redis.clients.jedis.Jedis", ClassUtils.getDefaultClassLoader());
	private static final boolean JREDIS_AVAILABLE = ClassUtils.isPresent("org.jredis.JRedis", ClassUtils.getDefaultClassLoader());
	private static final boolean SRP_AVAILABLE = ClassUtils.isPresent("redis.client.RedisClient", ClassUtils.getDefaultClassLoader());
	private static final boolean LETTUCE_AVAILABLE = ClassUtils.isPresent("com.lambdaworks.redis.RedisClient", ClassUtils.getDefaultClassLoader());

	@Override
	public boolean isSupportingCacheArchitecture(String architecture) {
		return "redis".equalsIgnoreCase(architecture);
	}

	@Override
	public Cache createCache(String cacheName, String host, int port) throws Exception {
		return new PublicRedisCache(cacheName, null, getRedisTemplate(getConnectionFactory(host, port)), getExpiryTime(cacheName));
	}

	@Override
	protected void destroyConnectionClient(RedisConnectionFactory connectionClient) throws Exception {
		if (connectionClient instanceof DisposableBean) {
			((DisposableBean) connectionClient).destroy();
		}
	}

	//TODO: Add and test other connection factories
	@Override
	protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
		if (JEDIS_AVAILABLE) {
			return new JedisConnectionFactory(new JedisShardInfo(hostName, port));
		} else if (JREDIS_AVAILABLE) {
			return new JredisConnectionFactory();
		} else if (SRP_AVAILABLE) {
			return new SrpConnectionFactory();
		} else if (LETTUCE_AVAILABLE) {
			return new LettuceConnectionFactory();
		} else {
			throw new IllegalArgumentException("No Jedis, Jredis, SRP or lettuce redis client on classpath. " +
					"Please add one of the implementation to your classpath");
		}
	}

	protected RedisTemplate<?, ?> getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}<commit_msg>Add support for different redis cache drivers<commit_after>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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.springframework.cloud.aws.cache.redis;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.cache.Cache;
import org.springframework.cloud.aws.cache.AbstractCacheFactory;
import org.springframework.data.redis.cache.PublicRedisCache;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.srp.SrpConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ClassUtils;

/**
 * @author <NAME>
 */
public class RedisCacheFactory extends AbstractCacheFactory<RedisConnectionFactory> {

	private static final boolean JEDIS_AVAILABLE = ClassUtils.isPresent("redis.clients.jedis.Jedis", ClassUtils.getDefaultClassLoader());
	private static final boolean JREDIS_AVAILABLE = ClassUtils.isPresent("org.jredis.JRedis", ClassUtils.getDefaultClassLoader());
	private static final boolean SRP_AVAILABLE = ClassUtils.isPresent("redis.client.RedisClient", ClassUtils.getDefaultClassLoader());
	private static final boolean LETTUCE_AVAILABLE = ClassUtils.isPresent("com.lambdaworks.redis.RedisClient", ClassUtils.getDefaultClassLoader());

	@Override
	public boolean isSupportingCacheArchitecture(String architecture) {
		return "redis".equalsIgnoreCase(architecture);
	}

	@Override
	public Cache createCache(String cacheName, String host, int port) throws Exception {
		return new PublicRedisCache(cacheName, null, getRedisTemplate(getConnectionFactory(host, port)), getExpiryTime(cacheName));
	}

	@Override
	protected void destroyConnectionClient(RedisConnectionFactory connectionClient) throws Exception {
		if (connectionClient instanceof DisposableBean) {
			((DisposableBean) connectionClient).destroy();
		}
	}

	@Override
	protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
		if (JEDIS_AVAILABLE) {
			JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
			connectionFactory.setHostName(hostName);
			connectionFactory.setPort(port);
			return connectionFactory;
		} else if (JREDIS_AVAILABLE) {
			JredisConnectionFactory connectionFactory = new JredisConnectionFactory();
			connectionFactory.setHostName(hostName);
			connectionFactory.setPort(port);
			return connectionFactory;
		} else if (SRP_AVAILABLE) {
			SrpConnectionFactory connectionFactory = new SrpConnectionFactory();
			connectionFactory.setHostName(hostName);
			connectionFactory.setPort(port);
			return connectionFactory;
		} else if (LETTUCE_AVAILABLE) {
			LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
			lettuceConnectionFactory.setHostName(hostName);
			lettuceConnectionFactory.setPort(port);
			return lettuceConnectionFactory;
		} else {
			throw new IllegalArgumentException("No Jedis, Jredis, SRP or lettuce redis client on classpath. " +
					"Please add one of the implementation to your classpath");
		}
	}

	protected RedisTemplate<?, ?> getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}
{'fraction_non_alphanumeric': 0.08561511762437331, 'fraction_numerical': 0.0030852294639413806, 'mean_word_length': 6.190388170055453, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2410737', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2095, 'n_words': 531}