应用场景 最近在公司做项目,需要对聊天内容进行存储,考虑到数据库查询的IO连接数高、连接频繁的因素,决定利用缓存做。 从网上了解到redis可以对所有的内容进行二进制的存储,而java是可以对所有对象进行序列化的,序列化的方法会在下面的代码中提供实现。 序列化 这里我编写了一个java序列化的工具,主要是对对象转换成byte[],和根据byte[]数组反序列化成java对象; 主要是用到了ByteArrayOutputStream和ByteArrayInputStream; 需要注意的是每个自定义的需要序列化的对象都要实现Serializable接口; 其代码如下: packagecom.bean.util; importjava.io.ByteArrayInputStream; importjava.io.ByteArrayOutputStream; importjava.io.IOException; importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; public classObjectUtil { /**对象转byte[] * @param obj * @return * @throws IOException */ public static byte[]objectToBytes(Object obj) throws Exception{ ByteArrayOutputStream bo =new ByteArrayOutputStream(); ObjectOutputStream oo = newObjectOutputStream(bo); oo.writeObject(obj); byte[] bytes =bo.toByteArray(); bo.close(); oo.close(); return bytes; } /**byte[]转对象 * @param bytes * @return * @throws Exception */ public static ObjectbytesToObject(byte[] bytes) throws Exception{ ByteArrayInputStream in =new ByteArrayInputStream(bytes); ObjectInputStream sIn = newObjectInputStream(in); return sIn.readObject(); } } 定义一个消息类,主要用于接收消息内容和消息下表的设置。 packagecom.bean; importjava.io.Serializable; /**定义消息类接收消息内容和设置消息的下标 * @author lenovo * */ public classMessage implements Serializable{ private static final longserialVersionUID = 7792729L; private int id; private String content; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getContent() { return content; } public void setContent(String content){ this.content = content; } } 利用redis做队列,我们采用的是redis中list的push和pop操作; 结合队列的特点: 只允许在一端插入 新元素只能在队列的尾部 FIFO:先进先出原则 redis中lpush(rpop)或rpush(lpop)可以满足要求,而redis中list 里要push或pop的对象仅需要转换成byte[]即可 java采用Jedis进行redis的存储和redis的连接池设置 packagecom.redis.util; importjava.util.List; import java.util.Map; importjava.util.Set; importredis.clients.jedis.Jedis; importredis.clients.jedis.JedisPool; importredis.clients.jedis.JedisPoolConfig; public classJedisUtil { private static String JEDIS_IP; private static int JEDIS_PORT; private static String JEDIS_PASSWORD; //private static String JEDIS_SLAVE; private static JedisPool jedisPool; static { Configuration conf =Configuration.getInstance(); JEDIS_IP =conf.getString("jedis.ip", "127.0.0.1"); JEDIS_PORT =conf.getInt("jedis.port", 6379); JEDIS_PASSWORD =conf.getString("jedis.password", null); JedisPoolConfig config = newJedisPoolConfig(); config.setMaxActive(5000); config.setMaxIdle(256);//20 config.setMaxWait(5000L); config.setTestOnBorrow(true); config.setTestOnReturn(true); config.setTestWhileIdle(true); config.setMinEvictableIdleTimeMillis(60000l); config.setTimeBetweenEvictionRunsMillis(3000l); config.setNumTestsPerEvictionRun(-1); jedisPool = newJedisPool(config, JEDIS_IP, JEDIS_PORT, 60000); } /** * 获取数据 * @param key * @return */ public static String get(String key) { String value = null; Jedis jedis = null; try { jedis =jedisPool.getResource(); value =jedis.get(key); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } return value; } public static void close(Jedis jedis){ try { jedisPool.returnResource(jedis); } catch (Exception e) { if(jedis.isConnected()) { jedis.quit(); jedis.disconnect(); } } } /** * 获取数据 * * @param key * @return */ public static byte[] get(byte[] key) { byte[] value = null; Jedis jedis = null; try { jedis =jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } return value; } public static void set(byte[] key,byte[] value) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.set(key,value); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } public static void set(byte[] key,byte[] value, int time) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.set(key,value); jedis.expire(key,time); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } public static void hset(byte[] key,byte[] field, byte[] value) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.hset(key,field, value); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } public static void hset(String key,String field, String value) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.hset(key,field, value); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } /** * 获取数据 * * @param key * @return */ public static String hget(String key,String field) { String value = null; Jedis jedis = null; try { jedis =jedisPool.getResource(); value =jedis.hget(key, field); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } return value; } /** * 获取数据 * * @param key * @return */ public static byte[] hget(byte[] key,byte[] field) { byte[] value = null; Jedis jedis = null; try { jedis =jedisPool.getResource(); value =jedis.hget(key, field); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } return value; } public static void hdel(byte[] key,byte[] field) { Jedis jedis =null; try { jedis =jedisPool.getResource(); jedis.hdel(key,field); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } /** * 存储REDIS队列顺序存储 * @param byte[] key reids键名 * @param byte[] value 键值 */ public static void lpush(byte[] key,byte[] value) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.lpush(key,value); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } /** * 存储REDIS队列反向存储 * @param byte[] key reids键名 * @param byte[] value 键值 */ public static void rpush(byte[] key,byte[] value) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.rpush(key,value); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } /** * 将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端 * @param byte[] key reids键名 * @param byte[] value 键值 */ public static void rpoplpush(byte[]key, byte[] destination) { Jedis jedis = null; try { jedis =jedisPool.getResource(); jedis.rpoplpush(key,destination); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } /** * 获取队列数据 * @param byte[] key 键名 * @return */ public static List |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|