/*
 * 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.RedisCache;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ClassUtils;

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

    private static final boolean JEDIS_AVAILABLE = ClassUtils.isPresent("redis.clients.jedis.Jedis", 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 RedisCache(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 (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;
    }
}

@@ -879,24 +879,31 @@
 e.RedisCache
+Manager
 ;%0Aimport org
@@ -957,32 +957,111 @@
 nectionFactory;%0A
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;%0A
 import org.sprin
@@ -1210,66 +1210,8 @@
 ry;%0A
-import org.springframework.data.redis.core.RedisTemplate;%0A
 impo
@@ -1922,20 +1922,16 @@
  return 
-new 
 RedisCac
@@ -1936,42 +1936,23 @@
 ache
-(cacheName, null, getRedisTemplate
+Manager.builder
 (get
@@ -1985,22 +1985,24 @@
 rt))
-, getExpiryTim
+.build().getCach
 e(ca
@@ -2009,17 +2009,16 @@
 cheName)
-)
 ;%0A    %7D%0A
@@ -2384,112 +2384,85 @@
     
-if (JEDIS_AVAILABLE) %7B%0A            JedisConnectionFactory connectionFactory
+RedisStandaloneConfiguration configuration
  = new 
-J
+R
 edis
-ConnectionFactory
+StandaloneConfiguration
 ();%0A
@@ -2469,37 +2469,29 @@
         
-  
-  
 con
-nectionFactory
+figuration
 .setHost
@@ -2518,42 +2518,163 @@
     
-    connectionFactory.setPort(port
+configuration.setPort(port);%0A        if (JEDIS_AVAILABLE) %7B%0A            JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration
 );%0A 
@@ -2847,120 +2847,21 @@
 ory(
-);%0A            lettuceConnectionFactory.setHostName(hostName);%0A            lettuceConnectionFactory.setPort(port
+configuration
 );%0A 
@@ -3133,321 +3133,6 @@
  %7D%0A%0A
-    protected RedisTemplate%3C?, ?%3E getRedisTemplate(RedisConnectionFactory redisConnectionFactory) %7B%0A        RedisTemplate%3C?, ?%3E redisTemplate = new RedisTemplate%3C%3E();%0A        redisTemplate.setConnectionFactory(redisConnectionFactory);%0A        redisTemplate.afterPropertiesSet();%0A        return redisTemplate;%0A    %7D%0A
 %7D%0A

Update to new spring-data concepts.