java操作Redis
1 环境准备1.1. 引入依赖1.2.创建jedis对象1.3.事务 2 操作key相关API3操作String相关API4操作List相关API5操作Set的相关API6 操作ZSet相关API7 操作Hash相关API
使用Java来操作Redis,Jedis是Redis官方推荐使用的Java连接redis的客户端。
1 环境准备
1.1. 引入依赖
<!--引入jedis连接依赖--><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.70</version></dependency>
1.2.创建jedis对象
public static void main(String[] args) { //1.创建jedis对象 Jedis jedis = new Jedis("192.168.75.101", 6379);//1.redis服务必须关闭防火墙 2.redis服务必须开启远程连接 jedis.select(0);//选择操作的库默认0号库 //2.执行相关操作 //3.释放资源 jedis.close(); }
开放端口6379
firewall-cmd --zone=public --add-port=6379/tcp --permanet1
重启防火墙服务
systemctl restart firewalld.service
阿里云服务器控制台配置安全组
重启redis-server
[root@AlibabaECS bin]# redis-server myconfig/redis.conf
操作命令
TestPing.java
public class TestPing { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.xx.xxx", 6379); String response = jedis.ping(); System.out.println(response); // PONG }}
断开连接
1.3.事务
public class TestTX { public static void main(String[] args) { Jedis jedis = new Jedis("198.168.xxx.xx", 6379); JSONObject jsonObject = new JSONObject(); jsonObject.put("hello", "world"); jsonObject.put("name", "kuangshen"); // 开启事务 Transaction multi = jedis.multi(); String result = jsonObject.toJSONString(); // jedis.watch(result) try { multi.set("user1", result); multi.set("user2", result); // 执行事务 multi.exec(); }catch (Exception e){ // 放弃事务 multi.discard(); } finally { // 关闭连接 System.out.println(jedis.get("user1")); System.out.println(jedis.get("user2")); jedis.close(); } }}
2 操作key相关API
private Jedis jedis; @Before public void before(){ this.jedis = new Jedis("192.168.XXX.XXX", 7000); } @After public void after(){ jedis.close(); } //测试key相关 @Test public void testKeys(){ //删除一个key jedis.del("name"); //删除多个key jedis.del("name","age"); //判断一个key是否存在exits Boolean name = jedis.exists("name"); System.out.println(name); //设置一个key超时时间 expire pexpire Long age = jedis.expire("age", 100); System.out.println(age); //获取一个key超时时间 ttl Long age1 = jedis.ttl("newage"); System.out.println(age1); //随机获取一个key String s = jedis.randomKey(); //修改key名称 jedis.rename("age","newage"); //查看可以对应值的类型 String name1 = jedis.type("name"); System.out.println(name1); String maps = jedis.type("maps"); System.out.println(maps); }
3操作String相关API
//测试 String 相关 @Test public void testString(){ //set jedis.set("name","秦羽"); //get String s = jedis.get("name"); System.out.println(s); //mset jedis.mset("content","tj","address","xqq"); //mget List<String> mget = jedis.mget("name", "content", "address"); mget.forEach(v-> System.out.println("v = " + v)); //getset String set = jedis.getSet("name", "小明"); System.out.println(set); //............ }
4操作List相关API
//测试 List 相关 @Test public void testList(){ //lpush jedis.lpush("names1","张三","王五","赵柳","win7"); //rpush jedis.rpush("names1","xiaomingming"); //lrange List<String> names1 = jedis.lrange("names1", 0, -1); names1.forEach(name-> System.out.println("name = " + name)); //lpop rpop String names11 = jedis.lpop("names1"); System.out.println(names11); //llen jedis.linsert("lists", BinaryClient.LIST_POSITION.BEFORE,"xiaohei","xiaobai"); //........ }
5操作Set的相关API
//测试SET相关@Testpublic void testSet(){ //sadd jedis.sadd("names","zhangsan","lisi"); //smembers jedis.smembers("names"); //sismember jedis.sismember("names","qinyu"); //...}
6 操作ZSet相关API
//测试 ZSET 相关@Testpublic void testZset(){ //zadd jedis.zadd("names",10,"张三"); //zrange jedis.zrange("names",0,-1); //zcard jedis.zcard("names"); //zrangeByScore jedis.zrangeByScore("names","0","100",0,5); //..}
7 操作Hash相关API
//测试 HASH 相关@Testpublic void testHash(){ //hset jedis.hset("maps","name","zhangsan"); //hget jedis.hget("maps","name"); //hgetall jedis.hgetAll("mps"); //hkeys jedis.hkeys("maps"); //hvals jedis.hvals("maps"); //....}