Springboot集成jedis 类似jFinal方式序列化key和value
在使用jFinal的时候redis是集成过的,直接简单暴力的使用,关于key和value的序列化已经处理过了
Cache bbs = Redis.use(); 然后bbs.set 或者bbs.get超级简单,在使用springboot的时候根据jFinal的处理方式。这里感谢jFinal,感谢开源,这里算是盗用代码
首先是配置jedis,百度方法很多,这里就简单的过一下
redis配置
@Configuration
@EnableCaching
@Slf4j
@PropertySource("classpath:application.yml")
public class RedisCacheConfiguration extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private Integer minIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.jedis.pool.max-active}")
private Integer maxActive;
@Bean
public JedisPool redisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMinIdle(minIdle);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,null);
log.info("JedisPool注入成功!");
log.info("redis地址:" + host + ":" + port);
return jedisPool;
}
}
这时候基本就可以使用了
开始工具类,展示我的目录
直接看代码
import org.springframework.stereotype.Component;
@Component
public interface IKeyNamingPolicy {
String getKeyName(Object key);
IKeyNamingPolicy defaultKeyNamingPolicy = new IKeyNamingPolicy() {
@Override
public String getKeyName(Object key) {
return key.toString();
}
};
}
import org.springframework.stereotype.Component;
/**
* 描述:
*
* @author 任文旭
* @date 2019/3/21 18:37
*/
@Component
public class IKeyNamingPolicyImpl implements IKeyNamingPolicy {
@Override
public String getKeyName(Object key) {
return key.toString();
}
}
@Component
public interface ISerializer {
byte[] keyToBytes(String key);
String keyFromBytes(byte[] bytes);
byte[] fieldToBytes(Object field);
Object fieldFromBytes(byte[] bytes);
byte[] valueToBytes(Object value);
Object valueFromBytes(byte[] bytes);
}
import de.ruedigermoeller.serialization.FSTObjectInput;
import de.ruedigermoeller.serialization.FSTObjectOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.util.SafeEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* 描述:
*
* @author 任文旭
* @date 2019/3/21 18:47
*/
@Component
@Slf4j
public class FstSerializer implements ISerializer{
public static final ISerializer me = new FstSerializer();
@Override
public byte[] keyToBytes(String key) {
return SafeEncoder.encode(key);
}
@Override
public String keyFromBytes(byte[] bytes) {
return SafeEncoder.encode(bytes);
}
@Override
public byte[] fieldToBytes(Object field) {
return valueToBytes(field);
}
@Override
public Object fieldFromBytes(byte[] bytes) {
return valueFromBytes(bytes);
}
@Override
public byte[] valueToBytes(Object value) {
FSTObjectOutput fstOut = null;
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
fstOut = new FSTObjectOutput(bytesOut);
fstOut.writeObject(value);
fstOut.flush();
return bytesOut.toByteArray();
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if(fstOut != null)
try {fstOut.close();} catch (IOException e) {
log.error(e.getMessage(), e);}
}
}
@Override
public Object valueFromBytes(byte[] bytes) {
if(bytes == null || bytes.length == 0)
return null;
FSTObjectInput fstInput = null;
try {
fstInput = new FSTObjectInput(new ByteArrayInputStream(bytes));
return fstInput.readObject();
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if(fstInput != null)
try {fstInput.close();} catch (IOException e) {log.error(e.getMessage(), e);}
}
}
}
JedisCache使用的就是jFinal中的Cache