下面就是代碼,麻煩大師解讀一下啊,太難了private Logger logger = LoggerFactory.getLogger(this.getClass());private final JedisPool jedisPool; ?//類似數(shù)據(jù)庫連接池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; ? ? ? ? ? ?byte[] bytes = jedis.get(key.getBytes()); ? ? ? ? ? ?if (bytes != null) { ? ? ? ? ? ? ? ?Seckill seckill = schema.newMessage(); ?//創(chuàng)建空對象 ? ? ? ? ? ? ? ?ProtobufIOUtil.mergeFrom(bytes, seckill, schema); ? ? ? ? ? ? ? ?//seckill被反序列化 ? ? ? ? ? ? ? ?return seckill; ? ? ? ? ? ?} ? ? ? ?} finally { ? ? ? ? ? ?jedis.close(); ? ? ? ?} ? ?} catch (Exception e) { ? ? ? ?logger.error(e.getMessage(), e); ? ?} ? ?return null;}public String putSeckill(Seckill seckill) { ? ?//set Object(Seckill) --> 序列化 --> byte[] ? ?try { ? ? ? ?Jedis jedis = jedisPool.getResource(); ? ? ? ?try { ? ? ? ? ? ?String key = "seckill: " + seckill.getSeckillId(); ? ? ? ? ? ?byte[] bytes = ProtobufIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); ? ? ? ? ? ?//超時(shí)緩存 ? ? ? ? ? ?int timeout = 60 * 60; ?//緩存一小時(shí),單位為秒 ? ? ? ? ? ?String result = jedis.setex(key.getBytes(), timeout, bytes); ? ? ? ? ? ?return result; ? ? ? ?} finally { ? ? ? ? ? ?jedis.close(); ? ? ? ?} ? ?} catch (Exception e) { ? ? ? ?logger.error(e.getMessage(), e); ? ?} ? ?return null;}
添加回答
舉報(bào)
0/150
提交
取消