锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. 项目中如何引进Redis,在项目中配置Redis缓存

项目中如何引进Redis,在项目中配置Redis缓存

0
  • 软件开发
  • 发布于 2024-09-25
  • 0 次阅读
黄健
黄健

首先,需要引入的包 Maven

<!-- fastjson json -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.52</version>
</dependency>

然后配置 applicationContext-redis.xml 文件:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd"
       default-lazy-init="false">



<context:property-placeholder location="classpath:redis.properties"
                                  ignore-unresolvable="true"/>
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="1000"/>
        <property name="minIdle" value="100"/>
        <property name="maxTotal" value="100"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>

<bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="192.168.0.1"  // Redis缓存的服务器地址
          p:port="6379"
          p:pool-config-ref="jedisPoolConfig"
          p:use-pool="true"
          p:database="0"/>  // ${redis.database}

<bean id="RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connection-factory-ref="jedisConnectionFactory"
          p:keySerializer-ref="stringRedisSerializer"
          p:valueSerializer-ref="JdkSerializationRedisSerializer"/>

<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="JdkSerializationRedisSerializer"
          class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>
</beans>

然后 添加redis.properties文件(不是必须的)

redis.host=255.255.255.255  // redis缓存地址
redis.port=6379
redis.pass=
#maxIdle
redis.maxIdle=30
#minIdle
redis.minIdle=10
#maxTotal
redis.maxTotal=100
#timeout
redis.timeout=5000
redis.testOnBorrow=false
redis.testOnReturn=false
redis.database=0

这样配置就完成了。

在Java中如何使用引入配置的Redis缓存:

        @Resource(name = "RedisTemplate")
    private ListOperations<String, TestLogPO> listOperations;

    @Resource(name = "RedisTemplate")
    private RedisTemplate redisTemplate;

        listOperations.leftPush(TEST_TEMPLATE_KEY, TestLogPO);
        redisTemplate.expireAt(TEST_TEMPLATE_KEY, endTime);

当然这位只是引入一个Redis缓存,如果你的redis有多个,并且不在同一个服务器下,这时你需要再次引入redis

需要在applicationContext-redis.xml 文件中添加引入Redis配置:

在配置文件中添加一个bean标签:

<bean id="Test2JedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="255.255.255.255" // 随便写的
          p:port="6379" 
          p:pool-config-ref="jedisPoolConfig"
          p:use-pool="true" 
          p:database="2"/>



还要添加一个:
<bean id="Test2RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connection-factory-ref="Test2JedisConnectionFactory"
          p:keySerializer-ref="Test2StringRedisSerializer"
          p:valueSerializer-ref="Test2JdkSerializationRedisSerializer"/>

<bean id="Test2StringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean id="Test2JdkSerializationRedisSerializer"
          class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>

在Java中如何使用引入配置的Redis缓存:

       @Resource(name = "Test2RedisTemplate")
    private ListOperations<String, TestLogPO> test2ListOperations;

    @Resource(name = "Test2RedisTemplate")
    private RedisTemplate test2RedisTemplate;

        test2ListOperations.leftPush(TEST_TEMPLATE_KEY, TestLogPO);
        test2RedisTemplate.expireAt(TEST_TEMPLATE_KEY, endTime);

结束!作者是菜鸟,如有错误请指导改正

原文链接: https://onlyou.blog.csdn.net//article/details/100633012

标签: #redis 48
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.