Redis学习之Jedis的使用

Jedis的使用

简单地说就是通过java来操作Redis,什么是Jedis,是官方推荐的java连接开发工具!使用java操作Redis中间件!如果你要使用java操作redis,那么一定要对Jedis十分熟悉,当然你也可以使用SpringBoot的无脑的RedisTemplate,但是它的底层还是Jedis,所有还是很有必要熟悉一下底层Jedis

1、引入maven依赖

	<!-- redis -->
	<dependency>
    	<groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.2.0</version>
    </dependency>
    
    <!-- fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
   		<version>1.2.62</version>
	</dependency>
        
    <!-- junit -->   
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

2、编码测试

  • 连接redis数据库
  • 命令操作
  • 关闭连接
/**
	 * 测试连接redis数据库
	 */
	@Test
	public void test01(){
		Jedis jedis = new Jedis("112.74.167.52", 6379);
		String ping = jedis.ping();  //正常返回PONG
		System.out.println(ping);
		
	}

结果返回拒绝连接

# 决绝连接
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at redis.clients.jedis.Connection.connect(Connection.java:181)
	... 6 more

原因是因为redis的配置文件redis.conf文件中绑定了本机才能连接,并且开启了保护模式

# redis.conf
bind 127.0.0.1 # 将绑定的主机的地址注释
protected-mode yes # 将保护模式修改为  no

再次测试连接返回PONG 连接成功

常用的API

所有的API命令,就是对应的Redis指令,一个都没有变化

Jedis操作Key

/**
	 * 测试redis的Key命令
	 */
	@Test
	public void testKey(){
		Jedis jedis = new Jedis("112.74.167.52", 6379);
		System.out.println("清空数据: "+jedis.flushDB());
		System.out.println("判断某个键是否存在:"+jedis.exists("name"));
		System.out.println("新增<'name','pihao'>的键值对: "+jedis.set("name", "pihao"));
		System.out.println("新增<'gender','male'>的键值对: "+jedis.set("gender", "male"));
		System.out.println("查看数据库中所有的key值: "+jedis.keys("*"));
		System.out.println("删除键gender: "+jedis.del("gender"));
		System.out.println("判断gender是否存在: "+jedis.exists("gender"));
		System.out.println("查看键name所存储的类型:"+jedis.type("name"));
		System.out.println("随机返回key空间的一个: "+jedis.randomKey());
		System.out.println("重命名key: "+jedis.rename("name", "myname"));
		System.out.println("取出改后的name: "+jedis.get("myname"));
		System.out.println("按索引选择数据库: "+jedis.select(0));
		System.out.println("删除当前数据库中所有key: "+jedis.flushDB());
		System.out.println("返回当前数据库中key的数目: "+jedis.dbSize());
		System.out.println("删除所有数据库中的所有key: "+jedis.flushAll());
		
	}

Jedis操作String

/**
	 *测试redis的String的命令
	 */
	@Test
	public void testString(){
		Jedis jedis = new Jedis("112.74.167.52", 6379);
		jedis.flushDB();
		
		System.out.println("============添加数据============");
		System.out.println(jedis.set("key1", "value1"));
		System.out.println(jedis.set("key2", "value2"));
		System.out.println(jedis.set("key3", "value3"));
		System.out.println("删除key2:"+jedis.del("key2"));
		System.out.println("获取key2:"+jedis.get("key2"));
		System.out.println("修改key1:"+jedis.set("key1", "valueChanged"));
		System.out.println("获取key1的值:"+jedis.get("key1"));
		System.out.println("在key3后面添加值:"+jedis.append("key3", "End"));
		System.out.println("获取追加后的key3的值:"+jedis.get("key3"));
		System.out.println("添加多个键值对:"+jedis.mset("key01","vlaue01","key02","value02","key03","value03"));
		System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
		System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04"));
		System.out.println("删除多个键值对:"+jedis.del("key01","key02"));
		System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
		
		jedis.flushDB();
		
		System.out.println("============新增键值对防止覆盖原先值(setnx 分布式锁常用)============");
		System.out.println(jedis.setnx("key1", "value1"));
		System.out.println(jedis.setnx("key2", "value2"));
		System.out.println(jedis.setnx("key2", "value2-new"));
		System.out.println(jedis.get("key1"));
		System.out.println(jedis.get("key2"));
		
		System.out.println("============新增键值对并设置有效时间setex============");
		System.out.println(jedis.setex("key3", 2, "value3"));
		System.out.println(jedis.get("key3"));
		try {
			Thread.sleep(2000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(jedis.get("key3"));
		
		System.out.println("============获取原值,更新为新值============");
		System.out.println(jedis.getSet("key2", "keysGetSet"));
		System.out.println(jedis.get("key2"));
		
		System.out.println("获得key2的值的字符串:"+jedis.getrange("key2", 2, 4)); //ysG
		
	}

剩下的set、hash、zset都是相同的使用方法,具体命令看我的另一篇博客 https://blog.csdn.net/weixin_44178366/article/details/107140483

Jedis操作事务

/**
	 * 测试redis的事务
	 */
	@Test
	public void testTX(){
		Jedis jedis = new Jedis("112.74.167.52", 6379);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", "pihao");
		jsonObject.put("gender", "male");
		String user = jsonObject.toJSONString();
		
		//开启事务
		Transaction multi = jedis.multi();
		//命令入队
		try {
			
			multi.set("user1", user);
			multi.set("user2", user);
			int i = 1/0;  //抛出异常
			
			multi.exec(); //执行事务
		} catch (Exception e) {
			//失败的话 放弃事务
			multi.discard();
			e.printStackTrace();
		} finally{
			System.out.println(jedis.get("user1"));
			System.out.println(jedis.get("user2"));
			jedis.close(); //关闭连接
		}
		
	}

你可能感兴趣的:(NoSQL,Redis)