1. 分别序列化 elements ,然后 set 存储
2. 序列化list对象,set存储
这两种方法都类似mc的 object方法存储,运用这种方式意味着放弃redis对list提供的操作方法。
import net.spy.memcached.compat.closeutil;import net.spy.memcached.compat.log.logger;import net.spy.memcached.compat.log.loggerfactory;import redis.clients.jedis.client;import redis.clients.jedis.jedis;import redis.clients.jedis.jedispool;import redis.clients.jedis.jedispoolconfig;import java.io.*;import java.util.arraylist;import java.util.list;import java.util.random;/** * created by intellij idea. * user: lifeng.xu * date: 12-6-11 * time: 上午11:10 * to change this template use file | settings | file templates. */public class jedistest { private static logger logger = loggerfactory.getlogger(jedistest.class); /** * jedis pool for jedis resource * @return */ public static jedispool buildjedispool(){ jedispoolconfig config = new jedispoolconfig(); config.setmaxactive(1); config.setminidle(50); config.setmaxidle(3000); config.setmaxwait(5000); jedispool jedispool = new jedispool(config, "*****", ****); return jedispool; } /** * test data * @return */ public static list<user> buildtestdata(){ user a = new user(); a.setname("a"); user b = new user(); b.setname("b"); list<user> list = new arraylist<user>(); list.add(a); list.add(b); return list; } /** * test for */ public static void testsetelements(){ list<user> testdata = buildtestdata(); jedis jedis = buildjedispool().getresource(); string key = "testsetelements" + new random(1000).nextint(); jedis.set(key.getbytes(), objectstranscoder.serialize(testdata)); //验证 byte[] in = jedis.get(key.getbytes()); list<user> list = objectstranscoder.deserialize(in); for(user user : list){ system.out.println("testsetelements user name is:" + user.getname()); } } public static void testsetensemble(){ list<user> testdata = buildtestdata(); jedis jedis = buildjedispool().getresource(); string key = "testsetensemble" + new random(1000).nextint(); jedis.set(key.getbytes(), listtranscoder.serialize(testdata)); //验证 byte[] in = jedis.get(key.getbytes()); list<user> list = (list<user>)listtranscoder.deserialize(in); for(user user : list){ system.out.println("testsetensemble user name is:" + user.getname()); } } public static void main(string[] args) { testsetelements(); testsetensemble(); } public static void close(closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (exception e) { logger.info("unable to close %s", closeable, e); } } } static class user implements serializable{ string name; public string getname() { return name; } public void setname(string name) { this.name = name; } } static class objectstranscoder{ public static byte[] serialize(list<user> value) { if (value == null) { throw new nullpointerexception("can't serialize null"); } byte[] rv=null; bytearrayoutputstream bos = null; objectoutputstream os = null; try { bos = new bytearrayoutputstream(); os = new objectoutputstream(bos); for(user user : value){ os.writeobject(user); } os.writeobject(null); os.close(); bos.close(); rv = bos.tobytearray(); } catch (ioexception e) { throw new illegalargumentexception("non-serializable object", e); } finally { close(os); close(bos); } return rv; } public static list<user> deserialize(byte[] in) { list<user> list = new arraylist<user>(); bytearrayinputstream bis = null; objectinputstream is = null; try { if(in != null) { bis=new bytearrayinputstream(in); is=new objectinputstream(bis); while (true) { user user = (user) is.readobject(); if(user == null){ break; }else{ list.add(user); } } is.close(); bis.close(); } } catch (ioexception e) { logger.warn("caught ioexception decoding %d bytes of data", in == null ? 0 : in.length, e); } catch (classnotfoundexception e) { logger.warn("caught cnfe decoding %d bytes of data", in == null ? 0 : in.length, e); } finally { closeutil.close(is); closeutil.close(bis); } return list; } } static class listtranscoder{ public static byte[] serialize(object value) { if (value == null) { throw new nullpointerexception("can't serialize null"); } byte[] rv=null; bytearrayoutputstream bos = null; objectoutputstream os = null; try { bos = new bytearrayoutputstream(); os = new objectoutputstream(bos); os.writeobject(value); os.close(); bos.close(); rv = bos.tobytearray(); } catch (ioexception e) { throw new illegalargumentexception("non-serializable object", e); } finally { close(os); close(bos); } return rv; } public static object deserialize(byte[] in) { object rv=null; bytearrayinputstream bis = null; objectinputstream is = null; try { if(in != null) { bis=new bytearrayinputstream(in); is=new objectinputstream(bis); rv=is.readobject(); is.close(); bis.close(); } } catch (ioexception e) { logger.warn("caught ioexception decoding %d bytes of data", in == null ? 0 : in.length, e); } catch (classnotfoundexception e) { logger.warn("caught cnfe decoding %d bytes of data", in == null ? 0 : in.length, e); } finally { closeutil.close(is); closeutil.close(bis); } return rv; } }}
ps:redsi中存储list没有封装对object的api,是不是也是倾向于只存储用到的字段,而不是存储object本身呢?redis是一个in-mem的产品,会觉得我们应用的方式。
更多redis相关技术文章,请访问redis教程栏目进行学习!
以上就是redis中list怎么存储对象的详细内容。
