前言:环境说明
Jedis Git地址:https://github.com/xetorthio/jedis
文章中用到的代码地址:https://git.oschina.net/ironman1987/RedisDemo.git
基于maven的配置如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
包结构
1、 简单的Jedis实例
单客户端链接redis进行操作。主要包括redis服务器连接(包括设置密码),基本数据类型的set get,过期策略等。
package org.test.demo.redisdemo;
import java.util.List;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class RedisSimpleDemo {
@Test
public void testDemo() throws InterruptedException {
Jedis redis = new Jedis("localhost", 6379);// 连接redis
// redis.auth("1234");// 验证密码,如果需要验证的话
// STRING 操作
// SET key value将字符串值value关联到key。
redis.set("name", "ironman");
redis.set("pass", "123456");
redis.set("address", "guangzhou");
// SETEX key seconds value将值value关联到key,并将key的生存时间设为seconds(以秒为单位)。
redis.setex("testex", 5, "5s");
System.out.println("5s start:testex="+redis.get("testex"));
Thread.sleep(5000);//间隔5s
System.out.println("5s end:testex="+redis.get("testex"));
// MSET key value [key value ...]同时设置一个或多个key-value对。
redis.mset("a", "111", "b", "222","a", "333");
System.out.println("a="+redis.get("a"));
// redis.flushAll();清空所有的key
System.out.println(redis.dbSize());// dbSize是多少个key的个数
// APPEND key value如果key已经存在并且是一个字符串,APPEND命令将value追加到key原来的值之后。
redis.append("a", "00");// 如果key已经存在并且是一个字符串,APPEND命令将value追加到key原来的值之后。
// GET key 返回key所关联的字符串值
System.out.println("a="+redis.get("a"));
// MGET key [key ...] 返回所有(一个或多个)给定key的值
List list = redis.mget("a", "b");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
执行结果如下:
2、 连接池用法
为了提高程序的使用效率,可以对redis的连接做连接池处理,连接池采用apache的标准连接池接口。
同时为了提高维护性,将常用参数配置到外部资源配置文件。
常用配置如下:
#最大连接数, 默认8个
redis.pool.maxTotal=1024
#最大空闲连接数, 默认8个
redis.pool.maxIdle=200
#获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
redis.pool.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379
#auth
redis.auth=1234
更多参数说明,会在接下来的代码中,如果哪些参数需要经常修改,都可将参数写入外部配置资源文件。
完整代码如下:
package org.test.demo.redisdemo;
import java.util.ResourceBundle;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisPool {
private static JedisPool jedisPool = null;
static{
ResourceBundle bundle = ResourceBundle.getBundle("redis");
// System.out.println(bundle.getString("redis.ip"));
try {
JedisPoolConfig config = new JedisPoolConfig();
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
//是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
config.setJmxNamePrefix("pool");
//是否启用后进先出, 默认true
config.setLifo(true);
//最大空闲连接数, 默认8个
config.setMaxIdle(8);
//最大连接数, 默认8个
config.setMaxTotal(8);
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
config.setMaxWaitMillis(-1);
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
config.setMinEvictableIdleTimeMillis(1800000);
//最小空闲连接数, 默认0
config.setMinIdle(0);
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
config.setNumTestsPerEvictionRun(3);
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
config.setSoftMinEvictableIdleTimeMillis(1800000);
//在获取连接的时候检查有效性, 默认false
config.setTestOnBorrow(Boolean.parseBoolean(bundle.getString("redis.pool.testOnBorrow")));
//在空闲时检查有效性, 默认false
config.setTestWhileIdle(Boolean.parseBoolean(bundle.getString("redis.pool.testOnReturn")));
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
config.setTimeBetweenEvictionRunsMillis(-1);
jedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.parseInt(bundle.getString("redis.port")), 3000, bundle.getString("redis.auth"));
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
@Test
public void testMain() {
Jedis jedis = RedisPool.getJedis();
//do something
System.out.println(jedis.get("name"));
RedisPool.close(jedis);
}
}
好的,各位!该篇文章讲解了在java中如何使用redis,那么下篇文章我们来学习redis的应用场景实例。