redis java对象 *** 作

redis java对象 *** 作,第1张

概述使用Jedis客户端1. java 对象,需序列化public class Person implements Serializable {private int id;private String name;public Person(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public String getName() {return name;}2. 序列化类public class SerializeUtil {public static byte[] serialize(Object object) {ObjectOutputStream oos = null;ByteArrayOutputStream baos = null;try {// 序列化baos = new ByteArrayOutputStream();oos = new ObjectOutputStream(baos);oos.writeObject(object);byte[] bytes = baos.toByteArray();return bytes;} catch (Exception e) {}return null;}public static Object unserialize(byte[] bytes) {ByteArrayInputStream bais = null;try {// 反序列化bais = new ByteArrayInputStream(bytes);ObjectInputStream ois = new ObjectInputStream(bais);return ois.readObject();} catch (Exception e) {}return null;}}3. 对象的 *** 作测试public class SerializeTest {/*** @param args*/private static Jedis jedis;public static void main(String[] args) throws InterruptedException {jedis=new Jedis("127.0.0.1",6379);setObject();Thread.sleep(1000);Person person =getObject(100);System.out.println(jedis.keys("*"));if(person!=null){System.out.println(person.getId());System.out.println(person.getName());}delOject();System.out.println(jedis.keys("*"));person = getObject(100);if(person!=null){System.out.println(person.getId());System.out.println(person.getName());}else{System.out.println("key not exist");}}public static Person getObject(int id) {byte[] person = jedis.get(("person:" + id).getBytes());return (Person) SerializeUtil.unserialize(person);}public static void setObject() {Person person = new Person(100, "alan");jedis.set("person:100".getBytes(), SerializeUtil.serialize(person));person = new Person(101, "bruce");jedis.set("person:101".getBytes(), SerializeUtil.serialize(person));}public static void delOject(){boolean isExist=jedis.exists("person:100".getBytes());if(isExist){System.out.println("delete the key");jedis.del("person:100".getBytes());}}} 

使用Jedis客户端

1. java 对象,需序列化

Person </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; <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;"&gt;private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;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;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; <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;"&gt;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;"&gt; <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;"&gt;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;"&gt; <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;"&gt;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;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; <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;"&gt;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;"&gt; 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;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; <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;"&gt;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;"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;byte</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;[] 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;"&gt;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;"&gt;; ByteArrayOutputStream baos </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 序列化</span>

<span >
baos = <span >new<span > ByteArrayOutputStream();

        oos </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; ObjectOutputStream(baos);        oos.writeObject(object);        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;byte</span>[] bytes =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; baos.toByteArray();        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; bytes;    } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (Exception e) {    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;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;"&gt;byte</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;[] 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;"&gt;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;"&gt;;    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; {        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 反序列化</span>

<span >
bais = <span >new<span > ByteArrayinputStream(bytes);

        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;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (Exception e) {    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;;}

}

3. 对象的 *** 作测试

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@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;"&gt; args </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;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;"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; 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;"&gt;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;"&gt;); 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;"&gt;); 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;"&gt;); 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;"&gt;)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span>(person!=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;){ 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;"&gt;)); 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;"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span>(person!=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;){ 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;"&gt;else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;{ 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;"&gt;); }}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;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;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; <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;"&gt;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;"&gt; <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;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (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;"&gt;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;"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt; 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;"&gt;new</span> Person(100,"alan"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;); 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;"&gt;.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;"&gt;new</span> Person(101,"bruce"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;); 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;"&gt;.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;"&gt;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;"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt; delOject(){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;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;"&gt;.getBytes()); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;(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;"&gt;); 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;"&gt;.getBytes()); }}

}

总结

以上是内存溢出为你收集整理的redis java对象 *** 作全部内容,希望文章能够帮你解决redis java对象 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1169112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存