【Redis学习系列】Jedis

/ Redis / 没有评论 / 1380浏览

Redis Java

起初Redis和Java的交互是通过官方推荐的jedis来完成的;但是Springboot 2.x版本后官方已经不推荐时间jedis了。下面对jedis的使用做个简单的介绍。

引入maven依赖

        <!-- maven 的jedis依赖 -->
				<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.5.1</version>
        </dependency>
				<!-- 例子中会使用json所以引入阿里巴巴的fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>

Java代码示例

public class RedisTest {

    Jedis jedis = null;

    // 在调用测试方法之前;我们使用@Before来做统一的Jedis初始化
    @Before
    public void init(){
        jedis = new Jedis("118.190.102.176",6379);
        jedis.auth("huzd"); //认证
    }

  //测试String类型
    @Test
    public void testString(){
        System.out.println("flushdb:"+jedis.flushDB());
        System.out.println("是否存在key:hello:"+jedis.exists("hello"));
        System.out.println("删除key:hello:"+jedis.del("hello"));
        System.out.println("-----------添加STRING属性------------");
        jedis.set("name","huzd");
        jedis.set("age","34");
        jedis.set("sex","male");
        jedis.set("phone","15355558714");
        System.out.println("-----------mget打印值------------");
        List <String> mget = jedis.mget("name", "age", "sex", "phone");
        mget.stream().forEach(System.out::println);
        System.out.println("----------获取所有的key-------------");
        jedis.keys("*").stream().forEach(System.out::println);
        System.out.println("-----------------------");
        System.out.println("查看key:name的类型:"+jedis.type("name"));
        System.out.println("随机返回一个key:"+jedis.randomKey());
        System.out.println("先获取在设置age的值:"+jedis.getSet("age", "33"));
        System.out.println("rename key:"+jedis.rename("name", "username"));
        jedis.close();
    }

	 //测试list类型
    @Test
    public void testList(){
        System.out.println("flushdb:"+jedis.flushDB());
        jedis.lpush("citys","合肥","黄山","六安","蚌埠","宣城","马鞍山","芜湖");
        System.out.println("打印:citys对应的值:");
        jedis.lrange("citys",0,-1).stream().forEach(System.out::println);
        System.out.println("key:citys值长度:"+jedis.llen("citys"));
        jedis.close();
    }

  //测试事务
    @Test
    public void testTx() throws InterruptedException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("my","redis");
        jedis.watch("age");
        Transaction multi = jedis.multi();
//        multi.watch("age");
        multi.set("age","23");
        multi.set("phone","15305518714");
        multi.get("phone");
        multi.set("sm",jsonObject.toJSONString());
        multi.get("sm");
        Thread.sleep(30000);
        List <Object> exec = multi.exec();
        System.out.println(exec);
        if(null!=exec)exec.stream().forEach(System.out::println);
    }
  //测试事务的watch 启动testTx是在等待的时候;再次执行testTxWatch方法 查看事务提交情况。
    @Test
    public void testTxWatch() throws InterruptedException {
      jedis.set("age","26");
    }

    @After
    public void close(){
        jedis.close();
    }

}