首页>代码>Spring MVC+bootstrap+redis+mybatis整合开发开源高并发秒杀商品系统>/seckill/src/main/java/org/seckill/dao/cache/RedisDao.java
package org.seckill.dao.cache;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.seckill.entity.Seckill;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Created by wchb7 on 16-5-27.
*/
public class RedisDao {
private final Log LOG = LogFactory.getLog(this.getClass());
private final JedisPool jedisPool;
private RuntimeSchema<Seckill> schema = RuntimeSchema.createFrom(Seckill.class);
public RedisDao(String ip, int port) {
jedisPool = new JedisPool(ip, port);
}
public Seckill getSeckill(long seckillId) {
//redis逻辑操作
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckill:" + seckillId;
//并没有实现内部序列化操作
//get:byte[]->反序列化->Object(Seckill)
//采用自定义序列化
byte[] bytes = jedis.get(key.getBytes());
if (bytes != null) {
//空对象
Seckill seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, seckill, schema);
//seckill 被反序列化
return seckill;
}
} finally {
jedis.close();
}
} catch (Exception e) {
LOG.error(e.getMessage());
}
return null;
}
/**
* Seckill 对象传递到redis中
*
* @param seckill
* @return
*/
public String putSeckill(Seckill seckill) {
//set:Object(Seckill)->序列化->byte[] ->发送给redis
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckill:" + seckill.getSeckillId();
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//超时缓存
int timeOut = 60 * 60;
String result = jedis.setex(key.getBytes(), timeOut, bytes);
return result;
} finally {
jedis.close();
}
} catch (Exception e) {
LOG.error(e.getMessage());
}
return null;
}
}
最近下载更多
黄小熙 LV7
2024年11月12日
dapeng0011 LV15
2024年2月24日
笛卡尔积 LV6
2023年2月17日
弹指又十年 LV8
2022年9月18日
asd6260647 LV4
2022年7月2日
moneyla LV5
2022年3月11日
李可以 LV5
2022年2月19日
xuweisong2010 LV28
2021年10月28日
qiheideguang LV18
2021年10月17日
ltq50736 LV4
2021年9月3日

最近浏览