当前位置: 首页 > news >正文

在java中使用Redis

一 操作步骤

1.1导入Spring Data Redis 的maven坐标

在这里插入图片描述

1.2配置Redis数据源

  redis:
    host: localhost
    port: 6379
    database: 0

1.3编写配置类,创建RedisTemplate对象

在这里插入图片描述

package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    @Bean
    /**
     * 配置并返回一个RedisTemplate实例
     * 该实例用于与Redis数据库进行交互和操作,通过本方法配置连接工厂和序列化方式
     *
     * @param redisConnectionFactory Redis连接工厂,用于建立与Redis服务器的连接
     * @return 返回配置好的RedisTemplate实例,用于执行对Redis数据库的操作
     */
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
     log.info("开始创建RedisTemplate实例...");
        // 创建一个RedisTemplate实例
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置Redis连接工厂,指定如何连接到Redis服务器
        template.setConnectionFactory(redisConnectionFactory);
        // 设置键的序列化方式为StringRedisSerializer,确保键以字符串形式存储和读取
        template.setKeySerializer(new StringRedisSerializer());
        // 设置值的序列化方式为GenericJackson2JsonRedisSerializer,确保值以JSON形式序列化和反序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 返回配置好的RedisTemplate实例
        return template;
    }
    
}

在这里插入图片描述

1.4通过RedisTemplate对象操作Redis

在测试类中操作Redis
在这里插入图片描述

package com.sky.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

@SpringBootTest
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    public void testRedis() {
        redisTemplate.opsForValue().set("hello", "world");
        System.out.println(redisTemplate.opsForValue().get("hello"));
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));

    }
}

在这里插入图片描述

相关文章:

  • C 语言输入输出详解
  • 从零构建大语言模型全栈开发指南:第三部分:训练与优化技术-3.2.3预训练任务设计:掩码语言建模(MLM)与下一句预测(NSP)
  • 伊利工业旅游4.0,近距离感受高品质的魅力
  • Navicat连接postgresql时出现“datlastsysoid does not exist”报错的问题
  • Ruoyi-vue前后端分离系统部署
  • 如何在linux中部署dns服务 主备dns (详细全过程)
  • vue中使用threejs的加载纹理没有效果
  • 安卓-关于setOnTouchListener和setOnClickListener冲突的问题
  • 2025_0327_生活记录
  • vue配置.eslintrc、.prettierrc详解
  • 如何让 AI 搜索为您的企业可见性战略助力飞升!
  • 外贸独立站相关知识扫盲
  • 网路传输层UDP/TCP
  • 【YOLOE: Real-Time Seeing Anything】predict_visual_prompt.py视觉推理代码分析(检测版本)
  • 【漏洞修复】为了修复ARM64 Android10系统的第三方库漏洞,将ARM64 Android16的系统库直接拷贝到Android10系统如何?
  • 【HTML】验证与调试工具
  • webpack配置详解+项目实战
  • Mathtype无法插入到Word中
  • 详解Spark executor
  • (C语言)习题练习 (指针与二维数组)的长度计算
  • 南昌网站优化公司/zac博客seo
  • 国家工商网官网登录入口/seo怎么刷关键词排名
  • 通州个人做网站/seo联盟
  • 青岛公司网站建设公司排名/台州百度推广优化
  • 南京网站开发个人/seo优化软件购买
  • 网站建设后台管理便捷/新闻热点素材