Redis String類型的API使用

來源:互聯網
上載者:User

標籤:sim   表示   flush   --   gets   swa   port   開始   setnx   

package com.daxin.jedis_datastructure;import org.junit.After;import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;/*** Unit test for simple App.*/public class AppTest {Jedis jedis = null;@Beforepublic void before() {jedis = RedisUtils.getJedis();jedis.flushDB();}@Afterpublic void after() {jedis.close();}/*** 簡單key/value設定*/@Testpublic void jedis_set_get() {// 設定keyjedis.set("redis_key", "redis_value");System.out.println(jedis.get("redis_key"));// 追加到指定key的後面jedis.append("redis_key", "_redis_value");System.out.println(jedis.get("redis_key"));// 1,2參數不解釋// NX如果不存在的話,則設定,否則不設定。XX如果存在則設定// EX表示秒。PX表示毫秒// 最後一個參數表示多長時間到期jedis.set("redis_key", "123456789", "XX", "EX", 500L);System.out.println(jedis.get("redis_key"));jedis.set("123456789", "123456789", "NX", "EX", 500L);System.out.println(jedis.get("123456789"));}/*** redis中沒有int類型,裡面儲存的是string,在進行int加減時候將string轉int然後再轉string儲存*/@Testpublic void jedis_incr_incrBy() {System.out.println("------------incrBy10------------");// 加10Long r1 = jedis.incrBy("top", 10);// redis中沒有int類型,裡面儲存的是string,在進行int加減時候將string轉int然後再轉string儲存System.out.println(r1);System.out.println("------------incr------------");// 加1r1 = jedis.incr("top");System.out.println(r1);System.out.println("------------incrBy2------------");r1 = jedis.incrBy("top", 2);System.out.println(r1);}@Testpublic void jedis_decr_decrBy() {Long r1 = jedis.incrBy("top", 10);System.out.println(r1);// 減1操作r1 = jedis.decr("top");System.out.println(r1);// 減去4操作r1 = jedis.decrBy("top", 4);System.out.println(r1);}@Testpublic void jedis_getset() {/*** 先擷取在設定*/String r1 = jedis.getSet("daxin", "first");System.out.println(r1);r1 = jedis.getSet("daxin", "first");System.out.println(r1);}@Testpublic void jedis_setex() throws Exception {String r1 = jedis.setex("loginstate", 5, "yes");System.out.println(r1);//返回OKSystem.out.println(jedis.get("loginstate"));Thread.sleep(6000);//睡眠System.out.println(jedis.get("loginstate"));//到期}/*** 只有在 key 不存在時設定 key 的值。* @throws Exception*/@Testpublic void jedis_setnx() throws Exception {//只有在 key 不存在時設定 key 的值。Long r1 = jedis.setnx("top", "1");//傳回值1設定ook, 0失敗設定System.out.println(r1);r1 =jedis.setnx("top", "2");// 0失敗設定System.out.println(r1);}@Testpublic void jedis_mget() {//一次設定多個key/value,必須成對出現String r1 = jedis.mset("daxin","first","la","laji");System.out.println(r1);System.out.println(jedis.get("daxin"));System.out.println(jedis.get("la"));}/*** 用 value 參數覆寫給定 key 所儲存的字串值,從位移量 offset 開始。*/@Testpublic void jedis_range() {jedis.set("top", "top-k");jedis.setrange("top", 2, "*");//此處是覆蓋,不是插入System.out.println(jedis.get("top"));}/*** 返回 key 所儲存的字串值的長度。*/@Testpublic void jedis_strlen() {jedis.set("top", "top-k");System.out.println(jedis.strlen("top"));}/*** 同時設定一個或多個 key-value 對,若且唯若所有給定 key 都不存在。*/@Testpublic void jedis_msetnx() {jedis.set("top", "top-k");//注意:要所有的key都不存在才可以插入,否則全不插入jedis.msetnx("top","toptop","111","1111");System.out.println(jedis.get("top"));System.out.println(jedis.get("111"));}/*** 這個命令和 SETEX 命令相似,但它以毫秒為單位設定 key 的存留時間,而不是像 SETEX 命令那樣,以秒為單位。* @throws Exception */@SuppressWarnings("deprecation")@Testpublic void jedis_psetex() throws Exception {jedis.psetex("top", 1000*60, "一分鐘失效");System.out.println(jedis.get("top"));Thread.sleep(1000*60);System.out.println(jedis.get("top"));}}

 

Redis String類型的API使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.