目录
1、添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.0.2</version>
</dependency>
?示例:
public class JdeisTest {
public static void main(String[] args) {
//连接redis
Jedis jedis = new Jedis("192.168.200.129",6379);
//密码
jedis.auth("123456");
System.out.println(jedis.ping());
//keys
Set<String> keys = jedis.keys("*");
System.out.println(keys);
//string
jedis.set("jedis","hello-jedis");
System.out.println(jedis.get("jedis"));
jedis.expire("jedis",10L);
//list
jedis.lpush("list","list1","list2","list3","list4","list5");
List<String> list = jedis.lrange("list", 0, -1);
for (String s : list) {
System.out.println(s);
}
//set
jedis.sadd("set","set1");
jedis.sadd("set","set2");
jedis.sadd("set","set3");
Set<String> set = jedis.smembers("set");
for (String s : set) {
System.out.println(s);
}
//从存储在键处的设置值中删除指定成员
long set1 = jedis.srem("set", "1");
//hash
jedis.hset("hash","username","lisi");
System.out.println(jedis.hget("hash","username"));
Map map = new HashMap();
map.put("age","20");
map.put("address","杭州");
jedis.hmset("hash2", map);
List<String> hash = jedis.hmget("hash2","age","address");
for (String s : hash) {
System.out.println(s);
}
//zset
jedis.zadd("zset",10d,"zset1");
jedis.zadd("zset",10d,"zset2");
List<String> zset = jedis.zrange("zset", 0, -1);
for (String member : zset) {
System.out.println(member);
}
}
}
?结果:
优点:
缺点:
添加依赖
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.2.6.RELEASE</version>
</dependency>
public class LettuceTest {
public static void main(String[] args) {
//使用构建器链式编程来builder我们RedisURI
RedisURI uri = RedisURI.builder()
.withHost("192.168.200.129")
.withPort(6379).
withAuthentication("default", "123456")
.build();
//创建连接交白
RedisClient redisClient = RedisClient.create(uri);
StatefulRedisConnection<String, String> connect = redisClient.connect();
//通过connect 创建操作的command
RedisCommands<String, String> command = connect.sync();
List<String> keys = command.keys("*");
System.out.println("keys:" + keys);
command.set("name", "三张");
System.out.println(command.get("name"));
//list
command.lpush("list","list1","list2","list3","list4","list5");
List<String> list = command.lrange("list", 0, -1);
for (String s : list) {
System.out.println(s);
}
//set
command.sadd("set","set1");
command.sadd("set","set2");
command.sadd("set","set3");
Set<String> set = command.smembers("set");
for (String s : set) {
System.out.println(s);
}
//hash
command.hset("hash","username","lisi");
System.out.println(command.hget("hash","username"));
Map map = new HashMap();
map.put("age","20");
map.put("address","杭州");
command.hmset("hash2", map);
List<KeyValue<String, String>> hmget = command.hmget("hash2", "age", "address");
for (KeyValue<String, String> stringStringKeyValue : hmget) {
System.out.println(stringStringKeyValue);
}
//zset
command.zadd("zset",10d,"zset1");
command.zadd("zset",10d,"zset2");
List<String> zset = command.zrange("zset", 0, -1);
for (String member : zset) {
System.out.println(member);
}
//释放资源
connect.close();
redisClient.close();
}
}
优点:
缺点:
创建一个springboot工程
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
配置redis文件
spring:
redis:
port: 6379 #端口号
host: 192.168.200.129 #虚拟机IP地址
password: 123456 #密码
database: 0 #redis默认数据库
timeout: 5000
?写测试方法
public class RedisTemplateTest {
private static StringRedisTemplate redisTemplate;
@Autowired()
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
RedisTools.redisTemplate = redisTemplate;
}
public static void stringDemo() {
// 设置字符串值
redisTemplate.opsForValue().set("key1", "value1");
// 获取字符串值
String value = redisTemplate.opsForValue().get("key1");
System.out.println("String value: " + value);
}
public static void listDemo() {
// 在列表左侧插入元素
redisTemplate.opsForList().leftPush("list1", "value1");
redisTemplate.opsForList().leftPush("list1", "value2");
// 获取列表范围内的元素
List<String> values = redisTemplate.opsForList().range("list1", 0, -1);
System.out.println("List values: " + values);
}
public static void setDemo() {
// 向集合中添加元素
redisTemplate.opsForSet().add("set1", "value1");
redisTemplate.opsForSet().add("set1", "value2");
// 获取集合所有元素
Set<String> values = redisTemplate.opsForSet().members("set1");
System.out.println("Set values: " + values);
}
public static void hashDemo() {
// 设置哈希字段的值
redisTemplate.opsForHash().put("hash1", "field1", "value1");
redisTemplate.opsForHash().put("hash1", "field2", "value2");
// 获取哈希字段的值
String value = (String) redisTemplate.opsForHash().get("hash1", "field1");
System.out.println("Hash value: " + value);
}
// 添加成员到有序集合
public static void addToZSet() {
// 添加成员到有序集合
redisTemplate.opsForZSet().add("myset", "Alice", 90);
redisTemplate.opsForZSet().add("myset", "Bob", 85);
redisTemplate.opsForZSet().add("myset", "Charlie", 95);
redisTemplate.opsForZSet().add("myset", "David", 80);
}
// 获取有序集合的成员列表
public static Set<String> getZSetMembers() {
return redisTemplate.opsForZSet().range("myset", 0, -1);
}
// 获取有序集合的成员列表及分数
public static Set<ZSetOperations.TypedTuple<String>> getZSetWithScores() {
return redisTemplate.opsForZSet().rangeWithScores("myset", 0, -1);
}
}
?写controller测试
@GetMapping("/")
public String index(){
RedisTools.get("k1");
RedisTools.stringDemo();
RedisTools.listDemo();
RedisTools.setDemo();
RedisTools.hashDemo();
RedisTools.addToZSet();
Set<String> zSetMembers = RedisTools.getZSetMembers();
System.out.println(zSetMembers);
Set<ZSetOperations.TypedTuple<String>> zSetWithScores = ·
RedisTools.getZSetWithScores();
for (ZSetOperations.TypedTuple<String> tuple : zSetWithScores) {
String member = tuple.getValue();
double score = tuple.getScore();
System.out.println("Member: " + member + ", Score: " + score);
}
System.out.println("------------------");
}
启动项目:访问8080