package com.atguigu.boot.demo; import redis.clients.jedis.Jedis; import java.util.Iterator; import java.util.List; import java.util.Set; public class jedisDemo { public static void main(String[] args) { // 通过connection获得,通过指定Ip和端口号进行连接 Jedis jedis = new Jedis("192.168.92.129", 6379); // 指定访问服务器的密码 jedis.auth("abc123"); // 获得了jedis客户端,可以像JDBC一样操作了 System.out.println(jedis.ping()); // 获取keys * Set<String> keys = jedis.keys("*"); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } jedis.set("k3","Hello"); System.out.println(jedis.get("k3")); System.out.println(jedis.lpush("list3", "1", "2", "3", "HEllo", "hehe")); List<String> list3 = jedis.lrange("list3", 0L, -1L); for (int i = 0; i < list3.size(); i++) { System.out.println(list3.get(i)); } for(String s : list3){ System.out.println(s); } long count = jedis.expire("list3", 20); System.out.println(count); } }
package com.atguigu.boot.demo; import redis.clients.jedis.Jedis; import java.util.Iterator; import java.util.List; import java.util.Set; public class jedisDemo { public static void main(String[] args) { // 通过connection获得,通过指定Ip和端口号进行连接 Jedis jedis = new Jedis("192.168.92.129", 6379); // 指定访问服务器的密码 jedis.auth("abc123"); // 获得了jedis客户端,可以像JDBC一样操作了 System.out.println(jedis.ping()); // 获取keys * Set<String> keys = jedis.keys("*"); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } jedis.set("k3","Hello"); System.out.println(jedis.get("k3")); System.out.println(jedis.lpush("list3", "1", "2", "3", "HEllo", "hehe")); List<String> list3 = jedis.lrange("list3", 0L, -1L); for (int i = 0; i < list3.size(); i++) { System.out.println(list3.get(i)); } for(String s : list3){ System.out.println(s); } long count = jedis.expire("list3", 20); System.out.println(count); } }
<?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.atguigu</groupId> <artifactId>boot3-01</artifactId> <version>1.0-SNAPSHOT</version> <!-- 所有SpringBoot都必须继承--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.4</version> </parent> <dependencies> <!-- redis相关场景--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.4.3</version> </dependency> </dependencies> <!-- SpringBoot打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
<?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.atguigu</groupId> <artifactId>boot3-01</artifactId> <version>1.0-SNAPSHOT</version> <!-- 所有SpringBoot都必须继承--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.4</version> </parent> <dependencies> <!-- redis相关场景--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.4.3</version> </dependency> </dependencies> <!-- SpringBoot打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1) [root@localhost ~]# redis-server /myredis/redis6379.conf [root@localhost ~]# redis-cli -a abc123 -p 6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> ping PONG 127.0.0.1:6379> keys * 1) "k12" 2) "ksas" 3) "k10000" 4) "email" 5) "user:001" 6) "list" 7) "k3" 8) "k4" 9) "k2" 10) "key" 11) "k200" 12) "k11" 13) "k5" 14) "k6" 15) "k7" 16) "balance" 17) "k300" 18) "count" 19) "k1" 20) "k100" 127.0.0.1:6379> lpush list2 1 2 3 4 5 6 7 8 9 (integer) 9 127.0.0.1:6379> lrange list2 0 -1 1) "9" 2) "8" 3) "7" 4) "6" 5) "5" 6) "4" 7) "3" 8) "2" 9) "1" 127.0.0.1:6379> lrange list3 0 -1 1) "hehe" 2) "\xe5\x93\x88\xe5\x93\x88" 3) "3" 4) "2" 5) "1" 6) "hehe" 7) "\xe5\x93\x88\xe5\x93\x88" 8) "3" 9) "2" 10) "1" 127.0.0.1:6379> lrange list3 0 -1 1) "hehe" 2) "HEllo" 3) "3" 4) "2" 5) "1" 6) "hehe" 7) "\xe5\x93\x88\xe5\x93\x88" 8) "3" 9) "2" 10) "1" 11) "hehe" 12) "\xe5\x93\x88\xe5\x93\x88" 13) "3" 14) "2" 15) "1"
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]# redis-server /myredis/redis6379.conf
[root@localhost ~]# redis-cli -a abc123 -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "k12"
2) "ksas"
3) "k10000"
4) "email"
5) "user:001"
6) "list"
7) "k3"
8) "k4"
9) "k2"
10) "key"
11) "k200"
12) "k11"
13) "k5"
14) "k6"
15) "k7"
16) "balance"
17) "k300"
18) "count"
19) "k1"
20) "k100"
127.0.0.1:6379> lpush list2 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> lrange list2 0 -1
1) "9"
2) "8"
3) "7"
4) "6"
5) "5"
6) "4"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lrange list3 0 -1
1) "hehe"
2) "\xe5\x93\x88\xe5\x93\x88"
3) "3"
4) "2"
5) "1"
6) "hehe"
7) "\xe5\x93\x88\xe5\x93\x88"
8) "3"
9) "2"
10) "1"
127.0.0.1:6379> lrange list3 0 -1
1) "hehe"
2) "HEllo"
3) "3"
4) "2"
5) "1"
6) "hehe"
7) "\xe5\x93\x88\xe5\x93\x88"
8) "3"
9) "2"
10) "1"
11) "hehe"
12) "\xe5\x93\x88\xe5\x93\x88"
13) "3"
14) "2"
15) "1"