redis.clients
jedis
2.9.0
基本操作
Jedis jedis = new Jedis("192.168.1.109",6379);
System.out.println(jedis.incr("counter"));
// hash
jedis.hset("myhash","f1", "f2");
jedis.hset("myhash","f2","v2");
System.out.println(jedis.hgetAll("myhash"));
// list
jedis.rpush("mylist","1");
jedis.rpush("mylist","2");
jedis.rpush("mylist","3");
System.out.println(jedis.lrange("mylist",0,-1));
// set
jedis.sadd("myset","a");
jedis.sadd("myset","b");
jedis.sadd("myset","c");
System.out.println(jedis.smembers("myset"));
// zset
jedis.zadd("myzadd",20,"tom");
jedis.zadd("myzadd",30,"baozi");
Set result = jedis.zrangeWithScores("myzadd",0,-1);
result.forEach(n -> {
System.out.println(n.getScore()+":"+new String(n.getElement()));
});
事务
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class Jedis0 {
public static final Jedis jedis = new Jedis("192.168.44.133",6379);
/**
* 事前设置
*/
public static void before(){
jedis.set("balance", "100");
jedis.set("debt", "0");
System.out.println("初始值:balance "+jedis.get("balance")+",debt "+jedis.get("debt"));
}
public static boolean tranMethod() throws InterruptedException {
//监控balance
jedis.watch("balance");
//取值
int balance = Integer.parseInt(jedis.get("balance"));
int debt = Integer.parseInt(jedis.get("debt"));
int decr = 10;//实际这个值是从外部获取的,这里为了方便。
//判断 如果余额小于要取得值退出
if (balance < decr){
System.out.println("余额不足以减去: balance "+jedis.get("balance")+",debt "+jedis.get("debt"));
return false;
}
// //模拟堵塞
// Thread.sleep(5000);
// //模拟watch之后被其他线程修改
// if ("OK".equalsIgnoreCase(jedis.set("balance", "20"))){
// System.out.println("监视到balance被修改:balance "+jedis.get("balance")+",debt "+jedis.get("debt"));
// return false;
// }//被其他线程修改之后只能重试
//开启事务 操作
Transaction transaction = jedis.multi();
transaction.decrBy("balance", decr);
transaction.incrBy("debt",decr);
transaction.exec();
System.out.println("事务操作成功:balance "+jedis.get("balance")+",debt "+jedis.get("debt"));
return true;
}
public static void main(String[] args) throws InterruptedException {
before();
System.out.println(tranMethod());
}
}
JedisPool
GenericObjectPoolConfig的重要属性:
参数名 | 含义 | 默认值 |
---|---|---|
maxActive | 连接池最大连接数 | 8 |
maxIdle | 连接池最大空闲数 | 8 |
minIdle | 连接池最小空闲数 | 0 |
jmxEnabled | 是否开启jmx监控 | true |
blockWhenExhausted | 连接池用尽后是否等待。此参数为true,maxWaitMillis才生效 | true |
maxWaitMillis | 连接池资源耗尽后,调用者最大等待时间(毫秒) | -1:一直等 |
testOnBorrow | 借用连接池连接时会ping,无效链接会被移除 | false |
testOnReturn | 归还连接池连接时会ping,无效链接会被移除 | false |
minEvictableIdleTimeMillis | 连接的最小空闲时间 | 1000L * 60L * 30毫秒 =30分钟 |
numTeestsPerEvictionRun | 空闲连接检测时每次采样数 | 3 |
testWhileIdle | 借用连接池连接时,检测空闲超时的连接会被移出 | false |
timeBetweenEvictionRunsMillis | 空闲连接的检测周期 | -1:不做检测 |
@Bean
public JedisUtil jedisUtil() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(20);
config.setMaxWaitMillis(3000);
config.setTestOnBorrow(true);
JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
return new JedisUtil(jedisPool);
}
Jedis 使用Lua
eval:
Jedis jedis = new Jedis("192.168.1.109",6379);
String key = "hello";
String val = "baozi";
// 设置值
String setScript = "redis.call('set',KEYS[1],ARGV[1])";
jedis.eval(setScript, 1, key, val);
// 取值
String getScript = "return redis.call('get',KEYS[1])";
Object result = jedis.eval(getScript, 1, key);
System.out.println(result);
evalsha:
Jedis jedis = new Jedis("192.168.1.109",6379);
String key = "hello";
String val = "baozi";
// 设置值
String setScript = "redis.call('set',KEYS[1],ARGV[1])";
String setScriptSha = jedis.scriptLoad(setScript);
jedis.evalsha(setScriptSha, 1, key, val);
// 取值
String getScript = "return redis.call('get',KEYS[1])";
String getScriptSha = jedis.scriptLoad(getScript);
Object result = jedis.evalsha(getScriptSha, 1, key);
System.out.println(result);
Jedis Pipeline
示例一: 批量删除
List list = Arrays.asList("user:1","myset","myhash",
"hello","counter","myzadd","mylist");
Jedis jedis = new Jedis("192.168.1.109",6379);
//生成pipeline对象
Pipeline pipeline = jedis.pipelined();
//pipeline执行命令,此时并没有真的执行
list.forEach(n -> pipeline.del(n));
//真正执行
pipeline.sync();
示例二: 执行并返回执行结果
Jedis jedis = new Jedis("192.168.1.109",6379);
Pipeline pipeline = jedis.pipelined();
pipeline.set("hello","world");
pipeline.incr("counter");
List