java进阶知识--初识Jedis

一、Jedis代码操作

 1.1 什么是Jedis?

    概述:Jedis是一款用java操作redis数据库的工具。

 1.2 如何使用?

    1. 下载jedis的jar包,如:jedis-2.7.0.jar
    2. 使用步骤:
      * 获取连接
      Jedis jedis = new Jedis("IP",端口号);
      * 操作
      jedis.set(key,value);
      * 关闭连接
      jedis.close();

 1.3 Jedis操作各种redis中的数据结构

    1) 字符串类型 string :最多可以容纳数据长度512M

/**
* set
* get
*/
//1. 获取连接
Jedis jedis = new Jedis();	//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// 存储
jedis.set("username","wfx");
// 获取
String username = jedis.get("username");
System.out.println(username);

// 可以使用setex()方法存储可以指定过期时间的 key value
jedis.setex("activecode",20,"jedis");	// 将activecode:jedis键值对存入redis,并且20秒后自动删除该键值对

//3. 关闭连接
jedis.close();

    2) 哈希类型 hash :map格式。它是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。每个hash可以存储232 - 1 键值对(40多亿)

/**
* hset
* hget
* hgetAll
*/
//1. 获取连接
Jedis jedis = new Jedis();	// 如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// 存储hash
jedis.hset("user","name","wfx");
jedis.hset("user","age","18");
jedis.hset("user","gender","male");

// 获取hash
String name = jedis.hget("user", "name");
System.out.println(name);

// 获取hash的所有map中的数据
Map<String, String> user = jedis.hgetAll("user");
// keyset
Set<String> keySet = user.keySet();
for (String key : keySet) {
	//获取value
	String value = user.get(key);
	System.out.println(key + ":" + value);
}

//3. 关闭连接
jedis.close();

    3) 列表类型 list :linkedlist格式。支持重复元素,常用的操作是添加一个元素到列表的头部(左边)或者尾部(右边),或者获得列表的某一个片段

/**
* lpush / rpush
* lpop / rpop
* lrange key start end : 范围获取
* /
 //1. 获取连接
Jedis jedis = new Jedis();	// 如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// list 存储
jedis.lpush("mylist","a","b","c");	// 从左边存
jedis.rpush("mylist","a","b","c");	// 从右边存

// list 范围获取
List<String> mylist = jedis.lrange("mylist", 0, -1);	// -1代表所有,从0开始到末尾获取所有
System.out.println(mylist);

// list 弹出(删除)
String element1 = jedis.lpop("mylist");	// c
System.out.println(element1);

String element2 = jedis.rpop("mylist");	// c
System.out.println(element2);

// list 范围获取
List<String> mylist2 = jedis.lrange("mylist", 0, -1);
System.out.println(mylist2);

//3. 关闭连接
jedis.close();

    4) 集合类型 set :不允许重复元素,无序

/*
* sadd
* smembers:获取所有元素
*/
//1. 获取连接
Jedis jedis = new Jedis();	// 如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
 // set 存储
jedis.sadd("myset","java","php","c++");

// set 获取
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);

//3. 关闭连接
jedis.close();

    5) 有序集合类型 sortedset

    • 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。但元素有顺序。
    • 不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
    • 有序集合的成员是唯一的,但分数(score)却可以重复。
/**
* zadd
* zrange
*/
//1. 获取连接
Jedis jedis = new Jedis();	// 如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// sortedset 存储
jedis.zadd("mysortedset",10,"java");
jedis.zadd("mysortedset",20,"python");
jedis.zadd("mysortedset",30,"c++");

// sortedset 获取
Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);
System.out.println(mysortedset);

//3. 关闭连接
jedis.close();

二、jedis连接池--JedisPool

 2.1 使用步骤

    1. 导入相应jar包,创建JedisPool连接池对象
    2. 调用方法 getResource()方法获取Jedis连接

//0.创建一个配置对象,给连接池设置参数
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10);

//1.创建Jedis连接池对象
JedisPool jedisPool = new JedisPool(config,"localhost",6379);

//2.获取连接
Jedis jedis = jedisPool.getResource();

//3. 使用
jedis.set("hehe","heihei");

//4. 关闭 归还到连接池中
jedis.close();

 2.2 连接池工具类

    对连接池进行参数设置等操作,我们可以通过工具类对上面的代码进行优化。

 jedis.properties文件

  host=127.0.0.1
  port=6379
  maxTotal=50
  maxIdle=10

public class JedisPoolUtils {

	private static JedisPool jedisPool;

	static{
		// 读取配置文件
		InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
		// 创建Properties对象
		Properties pro = new Properties();
		// 关联文件
		try {
			pro.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 获取数据,设置到JedisPoolConfig中
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
		config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

		// 初始化JedisPool
		jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));

	}
	 /**
	 * 获取连接方法
	 */
	public static Jedis getJedis(){
		return jedisPool.getResource();
	}
}

    jedis.properties详细配置说明

#最大活动对象数     
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=xxxxxx  
#redis服务器的Port    
redis.port=6379

   

原文地址:https://www.cnblogs.com/sun9/p/13636502.html