zoukankan      html  css  js  c++  java
  • Redis String类型的API使用

    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;
    
    @Before
    public void before() {
    jedis = RedisUtils.getJedis();
    jedis.flushDB();
    }
    
    @After
    public void after() {
    jedis.close();
    }
    
    /**
    * 简单key/value设置
    */
    @Test
    public void jedis_set_get() {
    // 设置key
    jedis.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存储
    */
    @Test
    public void jedis_incr_incrBy() {
    
    System.out.println("------------incrBy10------------");
    // 加10
    Long r1 = jedis.incrBy("top", 10);// redis中没有int类型,里面存储的是string,在进行int加减时候将string转int然后再转string存储
    System.out.println(r1);
    System.out.println("------------incr------------");
    // 加1
    r1 = jedis.incr("top");
    System.out.println(r1);
    
    System.out.println("------------incrBy2------------");
    r1 = jedis.incrBy("top", 2);
    System.out.println(r1);
    }
    
    @Test
    public 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);
    }
    
    @Test
    public void jedis_getset() {
    
    /**
    * 先获取在设置
    */
    String r1 = jedis.getSet("daxin", "first");
    System.out.println(r1);
    r1 = jedis.getSet("daxin", "first");
    System.out.println(r1);
    
    }
    
    @Test
    public void jedis_setex() throws Exception {
    String r1 = jedis.setex("loginstate", 5, "yes");
    System.out.println(r1);//返回OK
    System.out.println(jedis.get("loginstate"));
    Thread.sleep(6000);//睡眠
    System.out.println(jedis.get("loginstate"));//过期
    
    }
    /**
    * 只有在 key 不存在时设置 key 的值。
    * @throws Exception
    */
    @Test
    public 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);
    
    }
    
    
    
    @Test
    public 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 开始。
    */
    @Test
    public void jedis_range() {
    jedis.set("top", "top-k");
    jedis.setrange("top", 2, "*");//此处是覆盖,不是插入
    System.out.println(jedis.get("top"));
    }
    
    /**
    * 返回 key 所储存的字符串值的长度。
    */
    @Test
    public void jedis_strlen() {
    jedis.set("top", "top-k");
    System.out.println(jedis.strlen("top"));
    }
    
    /**
    * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
    */
    @Test
    public 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")
    @Test
    public 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"));
    }
    
    }
  • 相关阅读:
    7.21 高博教育 数组 内存
    【基础扎实】Python操作Excel三模块
    PAT 甲级 1012 The Best Rank
    PAT 甲级 1011  World Cup Betting
    PAT 甲级 1010 Radix
    链式线性表——实验及提升训练
    循环程序设计能力自测
    链表应用能力自测
    PAT 甲级 1009 Product of Polynomials
    1008 Elevator (20分)
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7512672.html
Copyright © 2011-2022 走看看