为什么要用到redis?
来源:互联网 发布:隔音耳罩 知乎 编辑:程序博客网 时间:2024/06/05 22:48
java项目中为什么要用到redis?
1.redis是运行在内存中的,
2.redis是内存读写,异步非阻塞的.
redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字,字符串等,可以用string-value的形式存储;另一种是存对象、集合等,最好用序列化的方式来存储。
1、存储简单数据
try { Jedis jedis = new Jedis(); jedis.set("name", "JackGSmith");} catch (Exception e) { //如果缓存连不上,则不处理 System.out.println("登录无法更新该用户缓存");}
从redis缓存中获取key为“name”的值,使用jedis.get("name"),用一个String变量接收即可。
2、存储对象、集合
存对象集合用序列化的方式存储,用反序列化的方式取值。存储的key和value都是转化成字节码的形式。
先定义一个抽象类:SerializeTranscoder.java,代码如下:
package cn.com.taiji.sample.utils;import java.io.Closeable;import java.io.IOException;public abstract class SerializeTranscoder { public abstract byte[] serialize(Object value); public abstract Object deserialize(byte[] in) throws IOException; public void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } }}
再建一个序列化的类,ObjectTranscoder.java,继承上面这个抽象类,该类是用来序列化存储对象用的,代码如下:
package cn.com.taiji.sample.utils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class ObjectTranscoder<M extends Serializable> extends SerializeTranscoder{ @SuppressWarnings("unchecked") @Override public byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] result = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); M m = (M) value; os.writeObject(m); os.close(); bos.close(); result = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return result; } @SuppressWarnings("unchecked") @Override public M deserialize(byte[] in) { M result = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); result = (M) is.readObject(); is.close(); bis.close(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { close(is); close(bis); } return result; }}
接着在新建一个ListTranscoder.java文件,用来序列化存储List(集合)对象,基本同上,代码如下:
package cn.com.taiji.sample.utils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class ListTranscoder<M extends Serializable> extends SerializeTranscoder { @SuppressWarnings("unchecked") public List<M> deserialize(byte[] in) throws IOException { List<M> list = new ArrayList<>(); ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); while (true) { M m = (M)is.readObject(); if (m == null) { break; } list.add(m); } is.close(); bis.close(); } } catch (Exception e) { // e.printStackTrace(); } finally { is.close(); bis.close(); } return list; } @SuppressWarnings("unchecked") @Override public byte[] serialize(Object value) { if (value == null) throw new NullPointerException("Can't serialize null"); List<M> values = (List<M>) value; byte[] results = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); for (M m : values) { os.writeObject(m); } results = bos.toByteArray(); os.close(); bos.close(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return results; }}
现在,就可以用序列化的方式存储对象或集合了:
try { Jedis jedis = new Jedis(); List<SystemNotice> noticeList = systemNoticeManager.listQuery(noticeQModel); if(noticeList.size()>0 && noticeList != null){ ListTranscoder<SystemNotice> listTranscoder = new ListTranscoder<SystemNotice>(); jedis.set(loginUser.getId().getBytes(), listTranscoder.serialize(noticeList)); }} catch (Exception e) { //如果缓存连不上,则不处理 System.out.println("登录无法更新该用户缓存"); }
存的key使用用户id,所以取出list就很简单了:
try { Jedis jedis = new Jedis(); byte[] list = jedis.get(loginUser.getId().getBytes()); ListTranscoder<SystemNotice> listTranscoder = new ListTranscoder<SystemNotice>(); List<SystemNotice> newList = listTranscoder.deserialize(list);try { responseJson(JsonTools.toJsonStr(newList), response); } catch (IOException e) { e.printStackTrace(); }
}
阅读全文
0 0
- 为什么要用到redis?
- 为什么要用到struts2?
- 为什么要使用redis?
- redis为什么要集群
- 为什么要使用redis?
- 为什么要使用redis
- 为什么要用redis
- Android中为什么要用到Handler
- 为什么做javaweb要用到框架
- OC为什么以及什么时候要用到block?
- 为什么要用到泛型类、泛型方法
- 为什么要使用redis数据库?
- 为什么要使用redis数据库?
- 验证码怎么产生的?为什么要用到验证码?
- ava中为什么要序列化?什么时候用到序列化?
- combobox传值之为什么要用到hiddenName属性
- c/c++中 函数为什么要用到char ** 传参 .
- 2014.5.11 DAO设计模式为什么要用到工厂类?
- Excel Sheet Column Number:有字母组成的26进制转换成10进制
- Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
- hdu 5937 Equation dfs
- 如何在win10(64位系统)上安装apache服务器
- SQLServer 语句
- 为什么要用到redis?
- android develop refer
- rsync实现站点更新
- 《重构网络:SDN架构与实现》第一章总结
- 破解练习
- 回归算法-最小二乘法及梯度下降
- Android Studio结构
- WebP格式图片应用
- ssh secure shell连接centos中文乱码