SpringDataRedis整合Lettuce学习整理

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("com.easy.cloud")
@EnableWebMvc
@EnableAutoConfiguration
public class AppConfig {
	@Bean
	public RedisConnectionFactory redisConnectionFactory() {
		return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379));
	}

	@Bean
	public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		template.setConnectionFactory(factory);
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new RedisObjectSerializer());
		return template;
	}

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
	@Autowired
	RedisTemplate<Object, Object> redisTemplate;
	
	public String setObj() {
		User user=new User();
		user.setId(100);
		user.setUsername("dede");
		user.setPassword("4321");
		redisTemplate.opsForValue().set(user.getId(), user);
		return "success";
	}
	
	public String getObj() {
		User user=(User) redisTemplate.opsForValue().get(100);
		return user.getUsername();
	}
}
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

public class ProtoBufUtil {
	@SuppressWarnings("unchecked")
	public static <T> byte[] serializer(T o) {
		Class<T> clzz = (Class<T>) o.getClass();
		Schema<T> schema = RuntimeSchema.getSchema(clzz);
		return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(1024 * 1024));
	}

	public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
		T obj = null;
		try {
			obj = clazz.newInstance();
			Schema<T> schema = RuntimeSchema.getSchema(clazz);
			ProtostuffIOUtil.mergeFrom(bytes, obj, schema);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return obj;
	}

}
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class RedisObjectSerializer implements RedisSerializer<Object>{

	@Override
	public byte[] serialize(Object t) throws SerializationException {
		return ProtoBufUtil.serializer(t);
	}

	@Override
	public Object deserialize(byte[] bytes) throws SerializationException {
		return ProtoBufUtil.deserializer(bytes, Object.class);
	}

}

 下面这个序列化是完全基于Spring来实现的,其实也可以使用SpringSerializerUtils类来实现

import org.springframework.data.redis.serializer.RedisSerializer;
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.SerializationException;

public class RedisSerializerUtil 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 Object deserialize(byte[] bytes) {
		if (isEmpty(bytes)) {
			return null;
		}
		try {
			return deserializer.convert(bytes);
		} catch (Exception ex) {
			throw new SerializationException("Cannot deserialize", ex);
		}
	}

	public byte[] serialize(Object object) {
		if (object == null) {
			return EMPTY_ARRAY;
		}
		try {
			return serializer.convert(object);
		} catch (Exception ex) {
			return EMPTY_ARRAY;
		}
	}

	private boolean isEmpty(byte[] data) {
		return (data == null || data.length == 0);
	}

}

猜你喜欢

转载自blog.csdn.net/u013560667/article/details/81539757