Jedis、Lettuce、RedisTemplate连接中间件

发布于:2024-07-04 ⋅ 阅读:(20) ⋅ 点赞:(0)

jedis就像jdbc一样,用于两个端直接的连接。

1.创建Spring项目

这里不过多赘述...

2.导入连接工具jedis

在pom文件中导入jedis的依赖。

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.3.1</version>//版本号根据需要选择
        </dependency>

3.创建jedis对象

//redis所在主机的IP地址和监听的端口

Jedis jedis = new Jedis(IP地址, 端口号);

//指定访问服务器的密码

jedis.auth("132132qa..");

4.测试是否连通

jedis.ping()  
System.out.println(jedis.ping());//若输出为PONG表示连通

之后就可以通过jedis进行操作啦——

Set<String> set= jedis.keys("*");
jedis.set("name", "qyyb");
Map<String, String> map =new HashMap<>();
jedis.hset("tengjie",map);

....................

Jedis和Lettuce区别

Jedis客户端连接时,都需要创建实例,造成很大开销。线程之间也不安全,一个线程修改会对别的线程影响。

Lettuce底层使用Netty,保证只创建一个Lettuce连接,所有线程共享,故也不会存在数据修改对别的线程影响。

RedisTemplate使用

pom配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

properties文件配置

SwaggerConfig配置

 注入bean并操作

显示中文:启动redis加上--raw

感谢观看——


网站公告

今日签到

点亮在社区的每一天
去签到