Redis的安装与使用

 2023-09-15 阅读 29 评论 0

摘要:下面讲解下windows上面redis的安装与使用,以及在springboot环境下,怎么使用API来快速上手redis。Redis官方是没有提供Windows版本的,因为目前Linux版本已经相当稳定,而且用户量很大,无需开发windows版本,反而会带来兼容性等问题。

下面讲解下windows上面redis的安装与使用,以及在springboot环境下,怎么使用API来快速上手redis。Redis官方是没有提供Windows版本的,因为目前Linux版本已经相当稳定,而且用户量很大,无需开发windows版本,反而会带来兼容性等问题。

文章目录

  • windows上面redis的安装与使用
  • redis在springboot程序中的使用


windows上面redis的安装与使用

redis在github上面的下载地址

在这里插入图片描述
在这里插入图片描述

redis 使用?启动服务器之后的场景

在这里插入图片描述

在客户端对redis进行操作

在这里插入图片描述


redis在springboot程序中的使用

导入依赖

<!-- redis-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- SpringBootTest-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>
<!-- <scope>test</scope>-->
</dependency>

Java配置

package cn.wideth.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;/*** 编写配置类,构造RedisTemplate* 这个springboot已经帮我们配了,* 但是默认object,我想改成string*/
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置key的序列化方式template.setKeySerializer(RedisSerializer.string());// 设置value的序列化方式template.setValueSerializer(RedisSerializer.json());// 设置hash的key的序列化方式template.setHashKeySerializer(RedisSerializer.string());// 设置hash的value的序列化方式template.setHashValueSerializer(RedisSerializer.json());template.afterPropertiesSet();return template;}}

redis怎么安装。在程序中调用相关的API来进行程序操作

package cn.wideth.util.other;import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class TestRedis {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void operateStr(){System.out.println(redisTemplate);// 存入key为hello ,value为worldredisTemplate.opsForValue().set("hello","world");// 获取key为helloString username = (String) redisTemplate.opsForValue().get("hello");System.out.println(username);}}

程序结果

在这里插入图片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/62713.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息