redis的使用 Java_redis在java项目中的使用

在上一篇文章中已经讲了redis的spring配置,这篇将会描述redis在java项目中的使用。

redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字,字符串等,可以用string-value的形式存储;另一种是存对象、集合等,最好用序列化的方式来存储。

1、存储简单数据

try{

Jedis jedis= newJedis();

jedis.set("name", "JackGSmith");}catch(Exception e) {//如果缓存连不上,则不处理

System.out.println("登录无法更新该用户缓存");

}

从redis缓存中获取key为“name”的值,使用jedis.get("name"),用一个String变量接收即可。

2、存储对象、集合

存对象集合用序列化的方式存储,用反序列化的方式取值。存储的key和value都是转化成字节码的形式。

先定义一个抽象类:SerializeTranscoder.java,代码如下:

packagecn.com.taiji.sample.utils;importjava.io.Closeable;importjava.io.IOException;public abstract classSerializeTranscoder {public abstract byte[] serialize(Object value);public abstract Object deserialize(byte[] in) throwsIOException;public voidclose(Closeable closeable) {if (closeable != null) {try{

closeable.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}

}

再建一个序列化的类,ObjectTranscoder.java,继承上面这个抽象类,该类是用来序列化存储对象用的,代码如下:

packagecn.com.taiji.sample.utils;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;public class ObjectTranscoder extendsSerializeTranscoder{

@SuppressWarnings("unchecked")

@Overridepublic 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= newByteArrayOutputStream();

os= newObjectOutputStream(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);

}returnresult;

}

@SuppressWarnings("unchecked")

@Overridepublic M deserialize(byte[] in) {

M result= null;

ByteArrayInputStream bis= null;

ObjectInputStream is= null;try{if (in != null) {

bis= newByteArrayInputStream(in);

is= newObjectInputStream(bis);

result=(M) is.readObject();

is.close();

bis.close();

}

}catch(IOException e) {

e.printStackTrace();

}catch(ClassNotFoundException e) {

e.printStackTrace();

}finally{

close(is);

close(bis);

}returnresult;

}

}

接着在新建一个ListTranscoder.java文件,用来序列化存储List(集合)对象,基本同上,代码如下:

packagecn.com.taiji.sample.utils;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.List;public class ListTranscoder extendsSerializeTranscoder {

@SuppressWarnings("unchecked")public List deserialize(byte[] in) throwsIOException {

List list = new ArrayList<>();

ByteArrayInputStream bis= null;

ObjectInputStream is= null;try{if (in != null) {

bis= newByteArrayInputStream(in);

is= newObjectInputStream(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();

}returnlist;

}

@SuppressWarnings("unchecked")

@Overridepublic byte[] serialize(Object value) {if (value == null)throw new NullPointerException("Can't serialize null");

List values = (List) value;byte[] results = null;

ByteArrayOutputStream bos= null;

ObjectOutputStream os= null;try{

bos= newByteArrayOutputStream();

os= newObjectOutputStream(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);

}returnresults;

}

}

现在,就可以用序列化的方式存储对象或集合了:

try{

Jedis jedis= newJedis();

List noticeList =systemNoticeManager.listQuery(noticeQModel);if(noticeList.size()>0 && noticeList != null){

ListTranscoder listTranscoder = new ListTranscoder();

jedis.set(loginUser.getId().getBytes(), listTranscoder.serialize(noticeList));

}

}catch(Exception e) {//如果缓存连不上,则不处理

System.out.println("登录无法更新该用户缓存");

}

存的key使用用户id,所以取出list就很简单了:

try{

Jedis jedis= newJedis();byte[] list =jedis.get(loginUser.getId().getBytes());ListTranscoder listTranscoder = new ListTranscoder();

List newList =listTranscoder.deserialize(list);try{

responseJson(JsonTools.toJsonStr(newList), response);

}catch(IOException e) {

e.printStackTrace();

}}

至此,redis在java项目中的使用就到此结束了。关于redis的其他命令,可以自己去百度下。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值