java中redis的常用API示例
1.创建maven工程模块redis
2.导入对应的依赖
<!--导入jedis的包-->
<dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/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.62</version>
</dependency>
</dependencies>
3.启动redis服务和客户端
4、编码测试:
连接redis数据库
操作命令
断开连接!
package com.demo.redis;
import redis.clients.jedis.Jedis;
/**
* @className: TestPing
* @description: 测试连接
* @author:
* @create:
**/
public class TestPing {
public static void main(String[] args) {
//1.创建jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
//
System.out.println(jedis.ping());
jedis.close();
}
}
1.事务
package com.demo.redis;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
/**
* @className: TestTransational
* @description: 测试redis事务
* @author:
* @create:
**/
public class TestTransational {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
//清除当前数据库的数据.默认为db0
jedis.select(1);//选择redis数据库1
jedis.flushDB();//清除当前数据库数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "zhangsan");
jsonObject.put("age", "18");
//开启事务
Transaction multi = jedis.multi();
String user = jsonObject.toJSONString();
//jedis.watch(user);
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();
}
}
}
2.修改密码
package com.demo.redis;
import redis.clients.jedis.Jedis;
/**
* @className: TestPassword
* @description: 测试密码
* @author:
* @create:
**/
public class TestPassword {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
//验证密码,如果没有设置密码这段代码省略
//jedis.auth("root");
jedis.connect();//连接
jedis.disconnect();//断开连接
jedis.select(1);
jedis.flushDB();
jedis.close();
}
}
3.测试键
package com.demo.redis;
import redis.clients.jedis.Jedis;
import java.util.Set;
/**
* @className: TestKey
* @description: 测试键
* @author:
* @create:
**/
public class TestKey {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println("选择redis数据库:"+jedis.select(1));
System.out.println("清空数据当前数据库:"+jedis.flushDB());
System.out.println("新增键值对{\"name\":\"zhangsan\"}"+jedis.set("name","zhangsan"));
System.out.println("新增键值对{\"age\":\"18\"}"+jedis.set("age","18"));
System.out.println("1号redis数据库的所有键如下");
Set<String> keys = jedis.keys("*");
System.out.println(keys);
System.out.println("删除键name:"+jedis.del("name"));
System.out.println("判断键name是否存在:"+jedis.exists("name"));
System.out.println("判断键age是否存在:"+jedis.exists("age"));
System.out.println("查看键age所存储的值的类型:"+jedis.type("age"));
System.out.println("随机返回key空间的一个:"+jedis.randomKey());
System.out.println("重命名key:"+jedis.rename("age","renameAge"));
System.out.println("取出改后的renameAge:"+jedis.get("renameAge"));
System.out.println("删除当前选择数据库中的所有key:"+jedis.flushDB());
System.out.println("返回当前数据库中key的数目:"+jedis.dbSize());
System.out.println("删除所有数据库中的所有key:"+jedis.flushAll());
jedis.close();
}
}
4.测试字符串
package com.demo.redis;
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit