All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.smart.cloud.starter.redis.autoconfigure.RedisAutoConfiguration Maven / Gradle / Ivy

/*
 * Copyright © 2019 collin ([email protected])
 *
 * 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 io.github.smart.cloud.starter.redis.autoconfigure;

import io.github.smart.cloud.starter.redis.adapter.impl.RedisAdapterImpl;
import org.redisson.Redisson;
import io.github.smart.cloud.starter.redis.adapter.IRedisAdapter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * redis配置
 *
 * @author collin
 * @date 2018-10-17
 */
@Configuration
@ConditionalOnClass({Redisson.class, RedisOperations.class})
@EnableCaching
public class RedisAutoConfiguration {

    @Primary
    @Bean
    public RedisTemplate initRedisTemplate(final RedisConnectionFactory connectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate initStringRedisTemplate(final RedisConnectionFactory connectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return stringRedisTemplate;
    }

    @Bean
    public CacheManager cacheManager() {
        return new SimpleCacheManager();
    }

    @Bean
    public ValueOperations redisValueOperations(
            final RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public ListOperations redisListOperations(
            final RedisTemplate redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public SetOperations redisSetOperations(
            final RedisTemplate redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations redisZsetOperations(
            final RedisTemplate redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    @Bean
    public HashOperations redisHashOperations(
            final RedisTemplate redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public IRedisAdapter redisAdapterImpl(final StringRedisTemplate stringRedisTemplate) {
        return new RedisAdapterImpl(stringRedisTemplate);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy