使用Jedis客户端
1. java 对象,需序列化
Person</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> String <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> Person(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>,String <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> get<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>() { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> String get<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>() { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>;}</span></pre>
2. 序列化类
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">byte</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">[] seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ze(Object object) { ObjectOutputStream oos </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; ByteArrayOutputStream baos </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 序列化</span>
<span >
baos
oos </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> ObjectOutputStream(baos); oos.writeObject(object); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">byte</span>[] bytes =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> baos.toByteArray(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> bytes; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Exception e) { } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> Object unseria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ze(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">byte</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">[] bytes) { ByteArray<a href="https://m.jb51.cc/tag/input/" target="_blank" >input</a>Stream bais </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 反序列化</span>
<span >
bais
Object<a href="https://m.jb51.cc/tag/input/" target="_blank" >input</a>Stream ois </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Object<a href="https://m.jb51.cc/tag/input/" target="_blank" >input</a>Stream(bais); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> ois.rea<a href="https://www.jb51.cc/tag/dob/" target="_blank" >dob</a>ject(); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Exception e) { } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">;}
}
3. 对象的 *** 作测试
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> args </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a> je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> main(String[] args) <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> InterruptedException { je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a></span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>("127.0.0.1",6379<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); s<a href="https://www.jb51.cc/tag/eto/" target="_blank" >eto</a>bject(); Thread.sleep(</span>1000<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); Person person </span>=g<a href="https://www.jb51.cc/tag/eto/" target="_blank" >eto</a>bject(100<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.keys(</span>"*"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(person!=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(person.get<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(person.get<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>()); } delOject(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.keys(</span>"*"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)); person </span>= g<a href="https://www.jb51.cc/tag/eto/" target="_blank" >eto</a>bject(100<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(person!=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(person.get<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(person.get<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>()); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">{ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(</span>"key not exist"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> Person g<a href="https://www.jb51.cc/tag/eto/" target="_blank" >eto</a>bject(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">byte</span>[] person = je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.get(("person:" +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>).getBytes()); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Person) Seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>zeUtil.unseria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ze(person); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> s<a href="https://www.jb51.cc/tag/eto/" target="_blank" >eto</a>bject() { Person person </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Person(100,"alan"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.set(</span>"person:100"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.getBytes(),Seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>zeUtil.seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ze(person)); person </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Person(101,"bruce"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.set(</span>"person:101"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.getBytes(),Seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>zeUtil.seria<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ze(person)); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> delOject(){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">bo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>ean</span> isExist=je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.exists("person:100"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.getBytes()); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(isExist){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(</span>"delete the key"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); je<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>.del(</span>"person:100"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.getBytes()); }}
}
总结以上是内存溢出为你收集整理的redis java对象 *** 作全部内容,希望文章能够帮你解决redis java对象 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)