<div id="article_content" class="article_content tracking-ad" data-mod="popu_307" data-dsm="post">
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
<span style="background-color:rgb(199,237,204)">一、操作string 、list 、map 对象</span></div>
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
<span style="background-color:rgb(199,237,204)">1、引入jar:</span></div>
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
jedis-2.1.0.jar</div>
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
</div>
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
2、代码</div>
<div style="font-size:14px; font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; line-height:21px">
<div><span style="font-family:'Courier New'; color:rgb(63,95,191); font-size:10pt">/**</span></div>
<div><span style="font-family:'Courier New'; color:rgb(63,95,191); font-size:10pt"> * <span style="color:rgb(127,159,191)"><strong>@param</strong></span> args</span></div>
<div><span style="font-family:'Courier New'; color:rgb(63,95,191); font-size:10pt"> */</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> <span style="color:rgb(127,0,85)"><strong>static</strong></span> <span style="color:rgb(127,0,85)"><strong>void</strong></span> main(String[] args) {</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//连接<span style="text-decoration:underline">redis</span>服务</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> Jedis jedis = <span style="color:rgb(127,0,85)"><strong>new</strong></span> Jedis(<span style="color:rgb(42,0,255)">"192.168.88.15"</span>,6379);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//密码验证-如果你没有设置<span style="text-decoration:underline">redis</span>密码可不验证即可使用相关命令</span></span></div>
<div><span style="font-family:'Courier New'; color:rgb(63,127,95); font-size:10pt">// jedis.auth("<span style="text-decoration:underline">abcdefg</span>");</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//简单的key-value 存储</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.set(<span style="color:rgb(42,0,255)">"redis"</span>, <span style="color:rgb(42,0,255)">"myredis"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.get(<span style="color:rgb(42,0,255)">"redis"</span>));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//在原有值得基础上添加,如若之前没有该key,则导入该key</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//之前已经设定了<span style="text-decoration:underline">redis</span>对应"<span style="text-decoration:underline">myredis</span>",此句执行便会使<span style="text-decoration:underline">redis</span>对应"<span style="text-decoration:underline">myredisyourredis</span>"</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.append(<span style="color:rgb(42,0,255)">"redis"</span>, <span style="color:rgb(42,0,255)">"yourredis"</span>); </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.append(<span style="color:rgb(42,0,255)">"content"</span>, <span style="color:rgb(42,0,255)">"rabbit"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//<span style="text-decoration:underline">mset</span> 是设置多个key-value值 参数(key1,value1,key2,value2,...,<span style="text-decoration:underline">keyn</span>,<span style="text-decoration:underline">valuen</span>)</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//<span style="text-decoration:underline">mget</span> 是获取多个key所对应的value值 参数(key1,key2,key3,...,<span style="text-decoration:underline">keyn</span>) 返回的是个list</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.mset(<span style="color:rgb(42,0,255)">"name1"</span>,<span style="color:rgb(42,0,255)">"yangw"</span>,<span style="color:rgb(42,0,255)">"name2"</span>,<span style="color:rgb(42,0,255)">"demon"</span>,<span style="color:rgb(42,0,255)">"name3"</span>,<span style="color:rgb(42,0,255)">"elena"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.mget(<span style="color:rgb(42,0,255)">"name1"</span>,<span style="color:rgb(42,0,255)">"name2"</span>,<span style="color:rgb(42,0,255)">"name3"</span>));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//map</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> Map<String,String> user = <span style="color:rgb(127,0,85)"><strong>new</strong></span> HashMap<String,String>();</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> user.put(<span style="color:rgb(42,0,255)">"name"</span>, <span style="color:rgb(42,0,255)">"cd"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> user.put(<span style="color:rgb(42,0,255)">"password"</span>, <span style="color:rgb(42,0,255)">"123456"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//map存入<span style="text-decoration:underline">redis</span></span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.hmset(<span style="color:rgb(42,0,255)">"user"</span>, user);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//<span style="text-decoration:underline">mapkey</span>个数</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"len:%d"</span>, jedis.hlen(<span style="color:rgb(42,0,255)">"user"</span>)));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//map中的所有键值</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"keys: %s"</span>, jedis.hkeys(<span style="color:rgb(42,0,255)">"user"</span>) ));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//map中的所有value</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"values: %s"</span>, jedis.hvals(<span style="color:rgb(42,0,255)">"user"</span>) ));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//取出map中的name字段值</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> List<String> rsmap = jedis.hmget(<span style="color:rgb(42,0,255)">"user"</span>, <span style="color:rgb(42,0,255)">"name"</span>,<span style="color:rgb(42,0,255)">"password"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(rsmap);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//删除map中的某一个键值 password</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.hdel(<span style="color:rgb(42,0,255)">"user"</span>, <span style="color:rgb(42,0,255)">"password"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.hmget(<span style="color:rgb(42,0,255)">"user"</span>, <span style="color:rgb(42,0,255)">"name"</span>, <span style="color:rgb(42,0,255)">"password"</span>));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//list</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.del(<span style="color:rgb(42,0,255)">"listDemo"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.lrange(<span style="color:rgb(42,0,255)">"listDemo"</span>, 0, -1));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.lpush(<span style="color:rgb(42,0,255)">"listDemo"</span>, <span style="color:rgb(42,0,255)">"A"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.lpush(<span style="color:rgb(42,0,255)">"listDemo"</span>, <span style="color:rgb(42,0,255)">"B"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.lpush(<span style="color:rgb(42,0,255)">"listDemo"</span>, <span style="color:rgb(42,0,255)">"C"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.lrange(<span style="color:rgb(42,0,255)">"listDemo"</span>, 0, -1));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(jedis.lrange(<span style="color:rgb(42,0,255)">"listDemo"</span>, 0, 1));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//set</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.sadd(<span style="color:rgb(42,0,255)">"sname"</span>, <span style="color:rgb(42,0,255)">"wobby"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.sadd(<span style="color:rgb(42,0,255)">"sname"</span>, <span style="color:rgb(42,0,255)">"kings"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.sadd(<span style="color:rgb(42,0,255)">"sname"</span>, <span style="color:rgb(42,0,255)">"demon"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"set num: %d"</span>, jedis.scard(<span style="color:rgb(42,0,255)">"sname"</span>)));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"all members: %s"</span>, jedis.smembers(<span style="color:rgb(42,0,255)">"sname"</span>)));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"is member: %B"</span>, jedis.sismember(<span style="color:rgb(42,0,255)">"sname"</span>, <span style="color:rgb(42,0,255)">"wobby"</span>)));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"rand member: %s"</span>, jedis.srandmember(<span style="color:rgb(42,0,255)">"sname"</span>)));</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">//删除一个对象</span></span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> jedis.srem(<span style="color:rgb(42,0,255)">"sname"</span>, <span style="color:rgb(42,0,255)">"demon"</span>);</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(String.<span style="font-style:italic">format</span>(<span style="color:rgb(42,0,255)">"all members: %s"</span>, jedis.smembers(<span style="color:rgb(42,0,255)">"sname"</span>))); </span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"> }</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span> </div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt">二、将自定义对象保存到redis中:<br>
</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span> </div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt">1、自定义pojo 实现Serializable 接口:</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span> </div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>package<span style="color:windowtext; font-weight:normal"> cn.mingyuan.redis;</span></strong></span></div>
<div></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.Serializable;</span></strong></span></div>
<div></div>
<div><span style="color:rgb(63,95,191); font-size:10pt">/**</span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * 测试用<span style="text-decoration:underline">pojo</span>,实现了<span style="text-decoration:underline">Serializable</span>,以便进行系列化操作</span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * </span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * <span style="color:rgb(127,159,191)"><strong>@author</strong></span> <span style="text-decoration:underline">mingyuan</span></span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * </span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> */</span></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>public<span style="color:windowtext; font-weight:normal"> </span>class<span style="color:windowtext; font-weight:normal"> Person </span>implements<span style="color:windowtext; font-weight:normal"> Serializable {</span></strong></span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>private</strong></span> <span style="color:rgb(127,0,85)"><strong>static</strong></span> <span style="color:rgb(127,0,85)"><strong>final</strong></span> <span style="color:rgb(127,0,85)"><strong>long</strong></span> <span style="color:rgb(0,0,192)"><em>serialVersionUID</em></span> = -3562550857760039655L;</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> </span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>private</strong></span> String <span style="color:rgb(0,0,192)">name</span>;</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>private</strong></span> <span style="color:rgb(127,0,85)"><strong>int</strong></span> <span style="color:rgb(0,0,192)">age</span>;</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> Person(){}</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> Person(String name, <span style="color:rgb(127,0,85)"><strong>int</strong></span> age) {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>super</strong></span>();</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>this</strong></span>.<span style="color:rgb(0,0,192)">name</span> = name;</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>this</strong></span>.<span style="color:rgb(0,0,192)">age</span> = age;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> String getName() {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>return</strong></span> <span style="color:rgb(0,0,192)">name</span>;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> <span style="color:rgb(127,0,85)"><strong>void</strong></span> setName(String name) {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>this</strong></span>.<span style="color:rgb(0,0,192)">name</span> = name;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> <span style="color:rgb(127,0,85)"><strong>int</strong></span> getAge() {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>return</strong></span> <span style="color:rgb(0,0,192)">age</span>;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> <span style="color:rgb(127,0,85)"><strong>void</strong></span> setAge(<span style="color:rgb(127,0,85)"><strong>int</strong></span> age) {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>this</strong></span>.<span style="color:rgb(0,0,192)">age</span> = age;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(100,100,100)">@Override</span></span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> String toString() {</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>return</strong></span> <span style="color:rgb(42,0,255)">"Person [name="</span> + <span style="color:rgb(0,0,192)">name</span> + <span style="color:rgb(42,0,255)">", age="</span> + <span style="color:rgb(0,0,192)">age</span> + <span style="color:rgb(42,0,255)">"]"</span>;</span></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt">}</span></div>
</div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span> </div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt">2、测试类:</span></div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span> </div>
<div><span style="font-family:'Courier New'; color:windowtext; font-size:10pt"></span>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>package<span style="color:windowtext; font-weight:normal"> cn.mingyuan.redis;</span></strong></span></div>
<div></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.ByteArrayInputStream;</span></strong></span></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.ByteArrayOutputStream;</span></strong></span></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.IOException;</span></strong></span></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.ObjectInputStream;</span></strong></span></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> java.io.ObjectOutputStream;</span></strong></span></div>
<div></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>import<span style="color:windowtext; font-weight:normal"> redis.clients.jedis.Jedis;</span></strong></span></div>
<div></div>
<div><span style="color:rgb(127,0,85); font-size:10pt"><strong>public<span style="color:windowtext; font-weight:normal"> </span>class<span style="color:windowtext; font-weight:normal"> Test {</span></strong></span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(63,95,191)">/**</span></span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * <span style="color:rgb(127,159,191)"><strong>@param</strong></span> args</span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * <span style="color:rgb(127,159,191)"><strong>@throws</strong></span> IOException</span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> * <span style="color:rgb(127,159,191)"><strong>@throws</strong></span> ClassNotFoundException</span></div>
<div><span style="color:rgb(63,95,191); font-size:10pt"> */</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>public</strong></span> <span style="color:rgb(127,0,85)"><strong>static</strong></span> <span style="color:rgb(127,0,85)"><strong>void</strong></span> main(String[] args) <span style="color:rgb(127,0,85)"><strong>throws</strong></span> IOException,</span></div>
<div><span style="color:windowtext; font-size:10pt"> ClassNotFoundException {</span></div>
<div><span style="color:rgb(63,127,95); font-size:10pt">// <span style="text-decoration:underline">Jedis</span> <span style="text-decoration:underline">redis</span> = new <span style="text-decoration:underline">Jedis</span>("192.168.88.15");</span></div>
<div><span style="color:windowtext; font-size:10pt"> Jedis redis = <span style="color:rgb(127,0,85)"><strong>new</strong></span> Jedis(<span style="color:rgb(42,0,255)">"192.168.88.15"</span>, 6379);</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">// connect可以不要,因为在执行set操作的时候会先进行判断客户端是否于服务器端建立了连接,若无,则启动连接过程</span></span></div>
<div><span style="color:windowtext; font-size:10pt"> redis.connect();</span></div>
<div><span style="color:windowtext; font-size:10pt"> String set = redis.set(<span style="color:rgb(42,0,255)">"mingyuan"</span>, <span style="color:rgb(42,0,255)">"1"</span>);</span></div>
<div><span style="color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(<span style="color:rgb(42,0,255)">" set result \t"</span> + set);</span></div>
<div><span style="color:windowtext; font-size:10pt"> redis.incr(<span style="color:rgb(42,0,255)">"mingyuan"</span>);</span></div>
<div><span style="color:windowtext; font-size:10pt"> String string = redis.get(<span style="color:rgb(42,0,255)">"mingyuan"</span>);</span></div>
<div><span style="color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(<span style="color:rgb(42,0,255)">" get result of key 'mingyuan' \t"</span> + string);</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(63,127,95)">// 下面是对对象进行存储的测试代码</span></span></div>
<div><span style="color:windowtext; font-size:10pt"> ByteArrayOutputStream bos = <span style="color:rgb(127,0,85)"><strong>new</strong></span> ByteArrayOutputStream();</span></div>
<div><span style="color:windowtext; font-size:10pt"> ObjectOutputStream oos = <span style="color:rgb(127,0,85)"><strong>new</strong></span> ObjectOutputStream(bos);</span></div>
<div><span style="color:windowtext; font-size:10pt"> </span></div>
<div><span style="color:windowtext; font-size:10pt"> Person person = <span style="color:rgb(127,0,85)"><strong>new</strong></span> Person(<span style="color:rgb(42,0,255)">"liudehua"</span> ,22);</span></div>
<div><span style="color:windowtext; font-size:10pt"> oos.writeObject(person);</span></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>byte</strong></span>[] byteArray = bos.toByteArray();</span></div>
<div><span style="color:windowtext; font-size:10pt"> oos.close();</span></div>
<div><span style="color:windowtext; font-size:10pt"> bos.close();</span></div>
<div><span style="color:windowtext; font-size:10pt"> String setObjectRet = redis.set(<span style="color:rgb(42,0,255)">"mingyuan"</span>.getBytes(), byteArray);</span></div>
<div><span style="color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(<span style="color:rgb(42,0,255)">" set object return \t"</span> + setObjectRet);</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> <span style="color:rgb(127,0,85)"><strong>byte</strong></span>[] bs = redis.get(<span style="color:rgb(42,0,255)">"mingyuan"</span>.getBytes());</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> ByteArrayInputStream bis = <span style="color:rgb(127,0,85)"><strong>new</strong></span> ByteArrayInputStream(bs);</span></div>
<div><span style="color:windowtext; font-size:10pt"> ObjectInputStream inputStream = <span style="color:rgb(127,0,85)"><strong>new</strong></span> ObjectInputStream(bis);</span></div>
<div><span style="color:windowtext; font-size:10pt"> Person readObject = (Person) inputStream.readObject();</span></div>
<div><span style="color:windowtext; font-size:10pt"> System.<span style="color:rgb(0,0,192)"><em>out</em></span>.println(<span style="color:rgb(42,0,255)">" read object \t"</span> + readObject.toString());</span></div>
<div><span style="color:windowtext; font-size:10pt"> inputStream.close();</span></div>
<div><span style="color:windowtext; font-size:10pt"> bis.close();</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> redis.disconnect();</span></div>
<div></div>
<div><span style="color:windowtext; font-size:10pt"> }</span></div>
<div></div>
<span style="color:windowtext; font-size:10pt">}</span></div>
</div>
</div>
java保存数据到redis java redis 存list
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
泰森多边形 python算法
艺学绘小编收集整理了如何画出超漂亮的极光绘画教程 在单独图层上瞎几把画几条蚯蚓。ctrl+T变个形,拉一下透视。 滤镜-模糊动感模糊。 第一次的模糊范围数值不要太大,保证最远处最细的曲线还能看得清走向为宜。 如图选择选区笔工具,这个工具相当于PS里的快速蒙版。设置一下 ,硬度调到最低,画笔浓度勾选笔压,做成喷枪笔触。用选择笔涂抹画
泰森多边形 python算法 c++画多边形 c++画曲线图 delphi framerect 画透明 图层