高性能なシーケンス化の実装
27802 ワード
必要な環境:jdk 8、redis使用シーン:redisキャッシュ最適化、秒殺シーン、java自体が提供するシーケンス化性能よりも優れている
依存jarパッケージ:
依存jarパッケージ:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.dyuproject.protostuffgroupId>
<artifactId>protostuff-coreartifactId>
<version>1.0.8version>
dependency>
<dependency>
<groupId>com.dyuproject.protostuffgroupId>
<artifactId>protostuff-runtimeartifactId>
<version>1.0.8version>
dependency>
上のコード:
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
/**
* Created by xpc on 2018/12/19.
* redis
*/
public class DictDaoImpl{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final JedisPool jedisPool;
//
private RuntimeSchema<Dict> schema = RuntimeSchema.createFrom(Dict.class);
public DictDaoImpl(String ip, int port){
jedisPool = new JedisPool(ip,port);
}
//
public Dict load(String dictId){
//redis
try{
Jedis jedis = jedisPool.getResource();
try{
String key = "seckill:"+dictId;
//
// get -> byte[] -> -> Object(Dict)
// , , protostuff
//protostuff:pojo.
byte[] bytes = jedis.get(key.getBytes());
//
if(bytes != null){
//
Dict seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes,seckill,schema);
//seckill
return seckill;
}
}finally {
jedis.close();
}
}catch (Exception e){
logger.error(e.getMessage(),e);
}
return null;
}
// Dict
public String store(Dict dict){
//set Object(Dict) -> -> byte[]
try{
Jedis jedis = jedisPool.getResource();
try{
String key = "seckill:"+dict.getKey();
byte[] bytes = ProtostuffIOUtil.toByteArray(dict,schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//
int timeout = 60 * 60 ;//1
String result = jedis.setex(key.getBytes(),timeout,bytes);
return result;
}finally {
jedis.close();
}
}catch (Exception e){
logger.error(e.getMessage(),e);
}
return null;
}
static class Dict {
private String key;
private HashMap<String,String> map;
public Dict(String key, HashMap<String, String> map) {
this.key = key;
this.map = map;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public HashMap<String, String> getMap() {
return map;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "Dict{" +
"key=" + key +
", map=" + map +
'}';
}
}
public static void main(String [] args){
DictDaoImpl dictDaoImpl = new DictDaoImpl("localhost",6379);
for (int i = 0; i < 5; i++) {
String key = "k" + i;
for (int j = 0; j < 10; j++) {
String val = "v" + j;
String tmpKey = "k" + j;
HashMap<String,String> map = new HashMap<>();
map.put(tmpKey,val);
Dict dict = dictDaoImpl.load(val);
if(dict == null){
dict = new Dict(key,map);
if(dict != null){
String result = dictDaoImpl.store(dict);
// System.out.println(result);
dict = dictDaoImpl.load(key);
System.out.println(dict.toString());
}
}
}
}
}
}