![15d35478a5dd87e257edb936fc2de815.png](https://i-blog.csdnimg.cn/blog_migrate/33a19bbf1e142ce254e995f987f15b11.jpeg)
Lettuce直接操作redis
1 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qianfeng</groupId>
<artifactId>reids-lettuce</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
2 测试类
package com.qianfeng.redis.lettuce;
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O = /O
// ____/`---'____
// . ' | |// `.
// / ||| : |||//
// / _||||| -:- |||||-
// | | - /// | |
// | _| ''---/'' | |
// .-__ `-` ___/-. /
// ___`. .' /--.-- `. . __
// ."" '< `.____<|>_/___.' >'"".
// | | : `- `.;` _ /`;.`/ - ` : | |
// `-. _ __ /__ _/ .-` / /
// ======`-.____`-.________/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖镇楼 BUG辟易
// 佛曰:
// 写字楼里写字间,写字间里程序员;
// 程序人员写程序,又拿程序换酒钱。
// 酒醒只在网上坐,酒醉还来网下眠;
// 酒醉酒醒日复日,网上网下年复年。
// 但愿老死电脑间,不愿鞠躬老板前;
// 奔驰宝马贵者趣,公交自行程序员。
// 别人笑我忒疯癫,我笑自己命太贱;
//
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* Created by jackiechan on 2019-04-21/09:14
*
* @Author jackiechan
*
*
* uri的语法
* 单独的redis:
* redis :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]
*
* 单独的redis ssl:
* redis :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]
*
* 单独的redis unix的socket
* redis-socket :// path [?[timeout=timeout[d|h|m|s|ms|us|ns]][&_database=database_]]
*
* redis哨兵模式
*
* redis-sentinel :// [: password@] host1[: port1] [, host2[: port2]] [, hostN[: portN]] [/ database][?[timeout=timeout[d|h|m|s|ms|us|ns]] [&_sentinelMasterId=sentinelMasterId_] [&_database=database_]]
*/
public class Test {
public static void main(String[] args) {
operSingle();
}
public static void operSingle(){
// RedisURI redisURI = RedisURI.create("redis://redis.qfjava.cn:8400");//指定redis服务器地址
// redisURI.setPassword("redis001"); //设置redis密码
RedisURI redisURI = RedisURI.create("redis://redis001@redis.qfjava.cn:8400");//可以使用这一行代替上面两行,指定redis的服务器地址和密码
RedisClient client = RedisClient.create(redisURI);
StatefulRedisConnection<String,String> connect = client.connect();//建立连接
/**
* 同步调用
*/
RedisCommands<String,String> commands = connect.sync();//开启同步操作
commands.set("hello","hello world");
String str = commands.get("hello");
//启动redis命令和jedis一样,都是命令的名字
System.out.println(str);
/**
* 异步调用
*/
RedisAsyncCommands<String,String> asyncCommands = connect.async();//开启异步操作
RedisFuture<String> future = asyncCommands.get("hello");
try {
String str1 = future.get();
System.out.println(str1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
connect.close();
client.shutdown();
}