Springboot2.0統合Redis

6671 ワード

springboot 2に基づく.0統合redis step 1、依存

        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
            1.1.3
        
        
            org.projectlombok
            lombok
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.apache.commons
            commons-pool2
        
        
        
            com.alibaba
            fastjson
            1.2.36
        
    

Step 2、redisconfig構成、fastjsonを使用したシーケンス番号
package com.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @author: songgt
 * @date: 2018-07-11 8:52
 */
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        //  fastjson   
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value       fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key      StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

Step 3、fastjsonシーケンス化処理
package com.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;
/**
 * @author: songgt
 * @date: 2018-07-11 8:52
 */
public class FastJsonRedisSerializer implements RedisSerializer {
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private Class clazz;
    public FastJsonRedisSerializer(Class clazz){
        super();
        this.clazz = clazz;
    }
    @Override
    public byte[] serialize(T t) throws SerializationException {
        if (null == t){
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }
    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (null == bytes || bytes.length <=0){
            return null;
        }
        String str = new String(bytes,DEFAULT_CHARSET);
        //        :      ,    addAccept
        //  com.alibaba.fastjson.JSONException: autoType is not support
        ParserConfig.getGlobalInstance().addAccept("com.entity.CatProduct");
        return JSON.parseObject(str,clazz);
    }
}

Step 4、サービス層listの操作を実現
package com.service.impl;

import com.entity.CatProduct;
import com.service.CatService;
import org.assertj.core.util.Lists;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
 * @author: songgt
 * @date: 2018-07-11 8:52
 */
@Service("catServiceImpl")
public class CatServiceImpl implements CatService {
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public Long addProduct(CatProduct catProduct) {
        List catProductList = Lists.newArrayList();
        catProductList.add(catProduct);
        long x = redisTemplate.opsForList().leftPush(catProduct.getUserId()+catProduct.getPId()+"",catProductList);
        return x;
    }
    @Override
    public List getProductFromCat() {
        //   redisTemplate.opsForList().leftPop("10127")           key
        List catProductList = (List) redisTemplate.opsForList().leftPop("10127");
        return catProductList;
    }
}

以上がredisを統合するプロセスで、CatProduct(カート)とCatServiceインタフェースを省略しました.これを記録として、後続の作業でredisの迅速な統合を容易にします.この文章をご覧になったら、もっと良い意見があります.ご指摘を歓迎します.