-
Jedis
2019-11-03 20:16:24Jedis 操作 Redis 中的数据结构3.1 Jedis 操作 String3.2 Jedis 操作 Hash3.3 Jedis 操作 List3.4 Jedis 操作 Set3.5 Jedis 操作 SortedSet4. Jedis 的连接池4.1 Jedis 的连接池的入门使用4.2 Jedis 的连接池工具类...文章目录
1. Jedis 的概念
Jedis 是一款让我们用 Java 操作 Redis 数据库的工具(类似于 JDBC)
2. Jedis 的入门使用
-
打开 Redis 的服务端和客户端
-
导入 Jedis 的 jar 包
jedis-2.7.0.jar
-
创建测试类
package com.zt.jedis.test; import org.junit.Test; import redis.clients.jedis.Jedis; public class JedisTest { @Test public void testJedis(){ // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.set("name","a"); // 3. 关闭连接 jedis.close(); } }
-
在 Redis 客户端获取数据
127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> get name "a"
3. Jedis 操作 Redis 中的数据结构
3.1 Jedis 操作 String
-
方法
- set(String key, Stirng value):存储一个数据
- get(String key):获取一个元素
- setex(String key, int seconds, String value):存储一个指定过期时间的元素
-
代码
public class JedisTest { @Test public void testString() throws InterruptedException { // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.set("name","a"); String name = jedis.get("name"); System.out.println(name); jedis.setex("age",5,"18"); System.out.println(jedis.get("age")); // 线程等待 10 秒 Thread.sleep(10000); System.out.println(jedis.get("age")); // 3. 关闭连接 jedis.close(); } }
运行结果
a 18 null
3.2 Jedis 操作 Hash
-
方法
- hset(String key, String field, String value):存储一个元素
- hget(String key, String field):获取一个元素
- hgetAll(String key):获取所有元素
-
代码
public class JedisTest { @Test public void testHash() { // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.hset("myHash", "name", "张三"); jedis.hset("myHash", "age", "18"); System.out.println(jedis.hget("myHash", "name")); Map<String, String> myHash = jedis.hgetAll("myHash"); System.out.println(myHash); // 3. 关闭连接 jedis.close(); } }
运行结果
张三 {name=张三, age=18}
3.3 Jedis 操作 List
-
方法
- lpush(String key, String… value):将元素添加到列表左边
- rpush(String key, String… value):将元素添加到列表右边
- lpop(String key):删除列表最左边元素,并将元素返回
- rpop(String key):删除列表最右边元素,并将元素返回
- lrange(String key, long start, long end):获取指定范围元素
-
代码
public class JedisTest { @Test public void testList() { // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.lpush("myList", "a", "b", "c"); jedis.rpush("myList", "a", "b", "c"); List<String> myList = jedis.lrange("myList", 0, -1); System.out.println(myList); String lpop = jedis.lpop("myList"); System.out.println(lpop); String rpop = jedis.rpop("myList"); System.out.println(rpop); List<String> myList2 = jedis.lrange("myList", 0, -1); System.out.println(myList2); // 3. 关闭连接 jedis.close(); } }
运行结果
[c, b, a, a, b, c] c c [b, a, a, b]
3.4 Jedis 操作 Set
-
方法
- sadd(String key, String… members):存储元素
- smembers(String key):获取所有元素
-
代码
public class JedisTest { @Test public void testSet() { // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.sadd("mySet","a","b","c"); Set<String> mySet = jedis.smembers("mySet"); System.out.println(mySet); // 3. 关闭连接 jedis.close(); } }
运行结果
[a, b, c]
3.5 Jedis 操作 SortedSet
-
方法
- zadd(String key, double score, String member):存储一个元素
- zrange(String key, long start, long end):获取指定范围元素
-
代码
public class JedisTest { @Test public void testSortedSet() { // 1. 获取连接 Jedis jedis = new Jedis("localhost"); // 2. 操作 jedis.zadd("mySortedSet",30,"a"); jedis.zadd("mySortedSet",40,"b"); jedis.zadd("mySortedSet",20,"c"); Set<String> mySortedSet = jedis.zrange("mySortedSet", 0, -1); System.out.println(mySortedSet); // 3. 关闭连接 jedis.close(); } }
运行结果
[c, a, b]
4. Jedis 的连接池
4.1 Jedis 的连接池的入门使用
-
导入 Jedis 和 Jedis 连接池的 jar 包
commons-pool2-2.3.jar jedis-2.7.0.jar
-
测试类
public class JedisTest { @Test public void testJedisPool(){ // 1.创建连接池配置对象 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 最大连接数 jedisPoolConfig.setMaxTotal(50); // 最大空闲状态连接数 jedisPoolConfig.setMaxIdle(10); // 2.创建 Jedis 连接池对象 JedisPool jedisPool = new JedisPool(jedisPoolConfig,"localhost",6379); // 3.获取连接 Jedis jedis = jedisPool.getResource(); // 4.操作 jedis.set("name", "a"); System.out.println(jedis.get("name")); // 5.关闭连接 jedis.close(); } }
运行结果
a
4.2 Jedis 的连接池工具类
-
将 Jedis 连接池的配置文件 jedis.properties 复制到 src 下
#最大活动对象数 redis.pool.maxTotal=1000 #最大能够保持idel状态的对象数 redis.pool.maxIdle=100 #最小能够保持idel状态的对象数 redis.pool.minIdle=50 #当池内没有返回对象时,最大等待时间 redis.pool.maxWaitMillis=10000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1. redis.pool.timeBetweenEvictionRunsMillis=30000 #向调用者输出“链接”对象时,是否检测它的空闲超时; redis.pool.testWhileIdle=true # 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3. redis.pool.numTestsPerEvictionRun=50 #redis服务器的IP redis.ip=127.0.0.1 #redis服务器的Port redis1.port=6379
-
连接池工具类
public class JedisPoolUtils { private static JedisPool jedisPool; // 配置连接池 static { Properties properties = new Properties(); InputStream resourceAsStream = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); try { properties.load(resourceAsStream); } catch (IOException e) { e.printStackTrace(); } JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("redis.pool.maxTotal"))); jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("redis.pool.maxIdle"))); jedisPool = new JedisPool(jedisPoolConfig, properties.getProperty("redis.ip"), Integer.parseInt(properties.getProperty("redis1.port"))); } /** * 返回一个连接 * @return */ public static Jedis getJedis() { return jedisPool.getResource(); } }
-
测试类
public class JedisTest { @Test public void testJedisPoolUtils() { // 1.获取连接 Jedis jedis = JedisPoolUtils.getJedis(); // 2.操作 jedis.set("hello","world"); System.out.println(jedis.get("hello")); // 3.关闭连接 jedis.close(); } }
运行结果
world
-
-
jedis
2019-05-06 20:30:45package com.imooc.jedis; import org.junit.Test;...import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisDemo1 { /**...package com.imooc.jedis; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisDemo1 { /** * jedis测试 */ @Test public void demo01() { //1.设置IP地址和端口 Jedis jedis = new Jedis("192.168.2.128",6379); //2.保存数据 jedis.set("name", "zhangsan"); //3.获取数据 String value = jedis.get("name"); System.out.println(value); //4.释放资源 jedis.close(); } /** * 连接池连接方式 */ @Test public void demo02() { //获得连接池的配置对象 JedisPoolConfig config = new JedisPoolConfig(); //设置最大连接数 config.setMaxTotal(30); //设置最大空闲连接数 config.setMaxIdle(10); //获取连接池 JedisPool jedispool = new JedisPool(config, "192.168.2.128", 6379); Jedis jedis = null; try { //获取核心对象 jedis = jedispool.getResource(); //设置数据 jedis.set("name", "张三"); //获取数据 String value = jedis.get("name"); System.out.println(value); }catch(Exception e) { e.printStackTrace(); }finally { if(jedis != null) { jedis.close(); } if(jedispool != null) { jedispool.close(); } } } }
注意事项:
1、Java建立简单的测试单元,但是运行时会报错,因为linux中没有将6379端口打开。
2、vim /etc/sysconfig/iptables。yy复制22端口那一行,p粘贴修改为6379。
3、重启防火墙service iptables restart
jedis需要的jar包
commons-pool2-2.3.jar
jedis-2.7.0.jar
链接:https://pan.baidu.com/s/1SmoofHfS-dFcnhKC8nQZ9Q
提取码:0of7
收藏数
18,575
精华内容
7,430
-
Springboot项目本地运行无问题而打成jar包出现问题Failed to auto-configure a DataSource
-
GPU 硬件虚拟化Hardware Virtualization
-
php实现简单的用户注册,登录,修改个人信息接口
-
PHP实现爬虫
-
jquery中怎么获取数组的长度
-
2021-01-26
-
Soul源码总结-01-25
-
对Properties of Digital Filters的简要补充
-
Linux系统之权限管理
-
【数据分析-随到随学】Hive详解
-
今日学习-lamba和优雅点击事件注册
-
使用 vue 遇到的坑
-
Cascading optofluidic phase modulators for performance enhancement in refractive adaptive optics
-
Ascend Pytorch算子功能验证
-
单调队列与单调栈
-
清除浮动
-
微信支付2021系列之扫码支付一学就会java版
-
游戏服务器之登陆流程
-
python办公自动化技巧
-
JDK8精简版 单独JRE运行环境