redis在java项目中的使用

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 {

  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">abstract</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">[] serialize(Object value);
  
  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">abstract</span> Object deserialize(<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span>[] in) <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throws</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> IOException;
  
  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">void</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> close(Closeable closeable) {
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (closeable != <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">try</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {
        closeable.close();
      } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (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(</span>"unchecked"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">)
  @Override
  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">[] serialize(Object value) {
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (value == <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {  
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throw</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span> NullPointerException("Can't serialize null"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">);  
    }  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span>[] result = <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    ByteArrayOutputStream bos </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    ObjectOutputStream os </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">try</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {  
      bos </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ByteArrayOutputStream();  
      os </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ObjectOutputStream(bos);
      M m </span>=<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (M) value;
      os.writeObject(m);  
      os.close();  
      bos.close();  
      result </span>=<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> bos.toByteArray();  
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (IOException e) {  
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throw</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("Non-serializable object"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">, e);  
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">finally</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {  
      close(os);  
      close(bos);  
    }  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">return</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> result;  
  }

  @SuppressWarnings(</span>"unchecked"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">)
  @Override
  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> M deserialize(<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">[] in) {
    M result </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    ByteArrayInputStream bis </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    ObjectInputStream is </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">try</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {  
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (in != <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {  
        bis </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ByteArrayInputStream(in);  
        is </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ObjectInputStream(bis);  
        result </span>=<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (M) is.readObject();  
        is.close();  
        bis.close();  
      }  
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (IOException e) {
        e.printStackTrace();
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (ClassNotFoundException e) {  
        e.printStackTrace();
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">finally</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {  
      close(is);  
      close(bis);  
    }  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">return</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> 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(</span>"unchecked"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">)
</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> List&lt;M&gt; deserialize(<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span>[] in) <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throws</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> IOException {
    List</span>&lt;M&gt; list = <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">();
    ByteArrayInputStream bis </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
    ObjectInputStream is </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">try</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (in != <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {
        bis </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ByteArrayInputStream(in);
        is </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ObjectInputStream(bis);
        </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">while</span> (<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">true</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {
          M m </span>=<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (M)is.readObject();
          </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (m == <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) {
            </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">break</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
          }
          list.add(m);
        }
        is.close();
        bis.close();
      }
  } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (Exception e) {  
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 128, 0, 1)">//</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 128, 0, 1)">  e.printStackTrace(); </span>
  }  <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">finally</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {
      is.close();
      bis.close();
    }
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">return</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">  list;
  }

@SuppressWarnings(</span>"unchecked"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">)
@Override
  </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">public</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">[] serialize(Object value) {
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">if</span> (value == <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">)
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throw</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span> NullPointerException("Can't serialize null"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">);

    List</span>&lt;M&gt; values = (List&lt;M&gt;<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">) value;
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">byte</span>[] results = <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
    ByteArrayOutputStream bos </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
    ObjectOutputStream os </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">null</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">;
    
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">try</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {
      bos </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ByteArrayOutputStream();
      os </span>= <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> ObjectOutputStream(bos);
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">for</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (M m : values) {
        os.writeObject(m);
      }
      results </span>=<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> bos.toByteArray();
      os.close();
      bos.close();
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">catch</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> (IOException e) {
      </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">throw</span> <span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("Non-serializable object"<span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)">, e);
    } </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">finally</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> {
      close(os);
      close(bos);
    }
    </span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 255, 1)">return</span><span style="margin: 0; padding: 0; font-family: &quot;Courier New&quot; !important; font-size: 0.75rem; color: rgba(0, 0, 0, 1)"> 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();
}
}

来源: http://www.cnblogs.com/huskyking/p/6007003.html
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none">
</wiz_tmp_tag>
来自为知笔记 (Wiz)