springboot 整合redis

4 篇文章 0 订阅

1.pom文件

<!--Spring Boot -->
		<!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- springboot整合redis --> 
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-data-redis</artifactId>  
        </dependency>

2.配置文件

#redis  
spring.redis.hostName=127.0.0.1
spring.redis.port=6379    
spring.redis.pool.maxActive=8    
spring.redis.pool.maxWait=-1    
spring.redis.pool.maxIdle=8    
spring.redis.pool.minIdle=0    
spring.redis.timeout=0

3.存取字符串

@Autowired
private StringRedisTemplate stringRedisTemplate;
	
@Test
public void save(){
   stringRedisTemplate.opsForValue().set("spingboot-redis","this is a test message");
}

执行后可在redis中查看

127.0.0.1:6379 > get springboot-redis
"this is a test message"

4.存取对象

class  A 需要实现Serializable接口

package com.knife.test;

import java.io.Serializable;

public class A implements Serializable{
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String toString() {
		return "{\"name\":\""+name+"\",\"age\":"+age+"}";
	}

	public A(String name, int age) {
		this.name = name;
		this.age = age;
	}
}

序列化class

package com.knife.test;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class RedisObjectSerializer implements RedisSerializer<Object>{

	private Converter<Object,byte[]> serializer = new SerializingConverter();
    private Converter<byte[],Object> deserializer = new DeserializingConverter();

    static final byte[] EMPTY_ARRAY = new byte[0];


    public byte[] serialize(Object o) throws SerializationException {

        if(o == null){
            return EMPTY_ARRAY;
        }

        try {
            return serializer.convert(o);
        }catch (Exception e){
            return EMPTY_ARRAY;
        }
    }

    public Object deserialize(byte[] bytes) throws SerializationException {
        if(isEmpty(bytes)){
            return null;
        }
        try {
            return  deserializer.convert(bytes);
        }catch (Exception e){
            throw new SerializationException("Can not deserializer",e);
        }

    }

    private boolean isEmpty(byte[] data){

        return (data == null || data.length == 0);
    }



}

redis配置class

package com.knife.test;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

	
	@Bean
    JedisConnectionFactory jedisConnectionFactory(){
        return  new JedisConnectionFactory();
    }

    /**
     * 存入对象序列化信息
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisSerizlizerObj(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new RedisObjectSerializer());
        return redisTemplate;
    }
    
}

测试

    @Autowired
	private RedisTemplate<String,A> rt;
	
    @Test
    public void saveobj(){
    	rt.opsForValue().set("classb", new A("wzj",29));
    }
    
    @Test
    public void getobj(){
    	System.out.println(rt.opsForValue().get("classb"));
    }

结果

127.0.0.1:6379>get classa
"\xac\xed\x00\x05sr\x00\x10com.knife.test.A\x19{\x8e\xc1`\xcc\xe4\xba\x02\x00\x02I\x00\x03ageL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x1ct\x00\x03chq"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值