Spring使用Redis、SpringBoot使用Redis、java使用Redis、无框架使用Redis

最近公司有个老项目要加缓存处理业务,怕以后又遇上老古董项目又要重写一遍觉得有必要记录一下,下面直接上代码:

如果你的项目没有使用Spring Boot,而是使用传统的Spring框架,你可以按照以下步骤来完成Spring集成Redis:

  1. 添加Redis依赖:在项目的构建文件(如Maven的pom.xml)中添加Redis的相关依赖。常用的Redis客户端库包括Jedis和Lettuce。

 依赖坐标:                    

<!-- Spring Core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.9</version>
</dependency>

<!-- Spring Data Redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.5.3</version>
</dependency>

<!-- Jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.7.0</version>
</dependency>
 

  1. 配置Redis连接信息:创建一个配置类,用于配置Redis的连接信息。可以使用Spring提供的注解@Configuration@Bean。在该类中,配置Redis连接池和连接工厂,设置主机名、端口号、密码等连接信息。

@Configuration
public class RedisConfig {

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 配置连接池属性
        // jedisPoolConfig.setMaxTotal(100);
        // jedisPoolConfig.setMaxIdle(20);
        // jedisPoolConfig.setMinIdle(5);
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName("127.0.0.1");
        config.setPort(6379);
        config.setPassword("123");

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);

        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}

2.使用Redis:在应用程序中使用注解@Autowired将RedisTemplate注入到需要使用Redis的类中,然后使用RedisTemplate进行操作。

@Service
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void setData(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object getData(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void deleteData(String key) {
        redisTemplate.delete(key);
    }
}

通过以上步骤,你可以在Spring中成功集成Redis,并使用RedisTemplate对象进行各种操作。请根据实际需求和情况进行适当的配置和使用

------------------------------------------如果是springboot项目集成就想对简单

在Java Spring框架中,可以通过以下步骤来集成Redis:

1. 添加Redis依赖:在项目的构建文件(如Maven的pom.xml)中,添加Redis的相关依赖。常用的Redis客户端库包括Jedis和Lettuce。

2. 配置Redis连接信息:在Spring的配置文件(如application.properties或application.yml)中,配置Redis的连接信息,包括主机名、端口号、密码等。

3. 创建Redis连接工厂:使用Spring提供的连接工厂类(如JedisConnectionFactory或LettuceConnectionFactory),创建与Redis的连接工厂对象。设置连接信息和其他相关属性。

4. 配置RedisTemplate:使用Spring提供的RedisTemplate类,配置Redis的操作模板。可以设置连接工厂、序列化器等。RedisTemplate提供了对Redis的各种操作方法,如字符串操作、哈希操作、列表操作等。

5. 使用Redis:在应用程序的代码中,通过注入RedisTemplate或直接引用RedisTemplate对象,使用其提供的方法来操作Redis。可以进行数据的读取、写入、删除等操作。

下面是一个简单的示例,演示了如何在Spring中集成Redis:

1. 添加依赖:

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```

2. 配置连接信息:

```properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
```

3. 创建Redis连接工厂:

```java
@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}
```

4. 使用Redis:

```java
@Service
public class MyService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void setData(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object getData(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void deleteData(String key) {
        redisTemplate.delete(key);
    }
}
```

通过以上步骤,你可以在Java Spring中成功集成Redis,并使用RedisTemplate对象进行各种操作。请根据实际需求和情况进行适当的配置和使用。

-----------------3.如果项目更老没有spring,但有Maven的情况下(手搓吧崩溃)

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

可直接在main方法里测试

Jedis jedis = new Jedis("127.0.0.1", 6379);
//        jedis.auth("123");
//        System.out.println(jedis.ping());
//        jedis.select(0);
//        System.out.println(jedis.get("nacosTest"));

----Redis操作对象测试:

Jedis jedis2 = JedisConfig.getJedis();
for (long i = 0; i < 2; i++) {
    CommonEmployeeDTO commonEmployeeDTO =new CommonEmployeeDTO();
    commonEmployeeDTO.setEmployeeId(1234+i);
    commonEmployeeDTO.setDeptName("测试部门"+1);
    commonEmployeeDTO.setEmployeeCode("987632"+i);
    commonEmployeeDTO.setEmployeeName("张三"+i);
    String json = new Gson().toJson(commonEmployeeDTO);
    // 将JSON字符串添加到List的右侧
    jedis2.rpush("noInformation", json);
}

// 获取List中的所有元素
List<String> elements2 = jedis2.lrange("noInformation", 0, -1);
for (String element : elements2) {
    // 将JSON字符串反序列化为对象
    CommonEmployeeDTO commonEmployeeDTO = new Gson().fromJson(element, CommonEmployeeDTO.class);
    //移除
    String userJson = new Gson().toJson(commonEmployeeDTO);
    jedis2.lrem("noInformation", 0, userJson);
}
List<String> elements3 = jedis.lrange("noInformation", 0, -1);
System.out.println(elements3.size());
jedis2.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: springcloud是一个开源的微服务框架,它基于Spring Boot,并提供了一整套解决方案,用于构建分布式系统中的各个微服务。通过使用springcloud,我们可以轻松实现服务注册与发现、负载均衡、断路器、配置中心等功能,简化了微服务开发和管理的复杂度。 springboot是一个基于Spring的轻量级开发框架,它通过开箱即用的原则,提供了一种快速构建应用程序的方式。使用springboot,我们可以简化繁琐的配置,只需少量的代码即可实现一个功能完整的应用程序,并且可以方便地和其他Spring生态的框架进行集成。 OAuth2是一种授权协议,用于保护Web应用程序、移动应用程序和API的资源。通过OAuth2协议,用户可以授权第三方应用程序访问他们的资源,而无需提供他们的密码。它提供了一种安全且可扩展的机制来处理用户身份验证和授权,并且被广泛应用于各种应用程序中。 Spring Security是一个Java框架,用于提供身份验证和访问控制的功能。它可以轻松地集成到Spring应用程序中,提供了一套强大的API和安全策略,用于保护应用程序免受各种攻击,包括身份验证和授权、会话管理、密码加密等。 Redis是一种内存数据存储系统,它以键值对的形式存储数据,并支持多种数据结构,如字符串、列表、集合、有序集合等。Redis具有高速、持久化和可扩展性等特点,可用于缓存、消息队列、分布式锁等各种场景。在使用Spring框架开发时,我们可以使用Redis作为缓存层,提高应用程序的性能和响应速度。 综上所述,Spring Cloud提供了构建和管理微服务的解决方案,Spring Boot简化了应用程序的开发,OAuth2和Spring Security提供了安全和授权的功能,而Redis作为内存数据存储系统,为应用程序提供了可扩展的缓存和数据存储能力。这些技术和框架相互协作,可以帮助开发者更快速、更安全地构建分布式系统。 ### 回答2: Spring Cloud是一个用于构建分布式系统的开发工具包,它提供了多个子项目来解决分布式系统的常见问题,例如服务注册与发现、配置管理、断路器、负载均衡等。Spring Boot是用于简化Spring应用程序开发的工具,它提供了一种自动配置的方式来快速搭建和运行Spring应用。OAuth2是一个开放标准,用于授权访问特定资源,它允许用户使用某个网站的授权信息来访问其他网站上的受保护资源。Spring Security是一个全面的身份验证和授权框架,它提供了一套安全服务,用于保护Web应用程序中的资源。Redis是一个高性能的键值存储系统,它常被用作缓存、队列、消息中间件等。 结合以上几个技术,可以构建一个基于Spring Cloud的分布式系统,使用Spring Boot快速搭建各个服务,使用Spring Security进行身份验证和授权管理。而OAuth2可以用于保护系统中的资源,通过认证服务器进行用户认证和授权,使得只有授权的用户才能访问相应的资源。Spring Security与OAuth2可以集成使用,通过Spring Security提供的权限管理功能来管理不同角色对资源的访问权限。同时,将Redis作为缓存服务器,可用于提高系统的性能和响应速度。 总之,Spring Cloud、Spring Boot、OAuth2、Spring Security和Redis等技术可以在构建分布式系统时发挥重要作用,帮助我们快速搭建实现各个功能模块,并提供高性能和安全性。 ### 回答3: Spring Cloud是一套基于Spring Boot的微服务框架,它提供了在分布式系统中构建和管理各种微服务的解决方案。它具有服务注册与发现、负载均衡、熔断、服务网关等功能,可以方便地实现微服务架构。 Spring Boot是一个用于快速开发基于Spring框架的应用程序的工具,它简化了Spring应用程序的配置和部署流程。它提供了自动化配置、内嵌服务器、开箱即用的特性,使得我们只需要关注业务逻辑的开发而不用过多关注框架的配置。 OAuth2是一种开放标准的授权协议,它使得用户可以通过授权的方式将与用户相关的信息共享给第三方应用程序。它使用令牌的方式进行授权,具有安全性高、可扩展性好的优点,常用于实现单点登录和授权管理。 Spring Security是一个用于在Java应用程序中提供身份验证和访问控制的框架。它可以与Spring Boot和Spring Cloud集成,提供了认证、授权、密码加密等功能,帮助我们更好地保护应用程序的安全。 Redis是一种高性能的键值存储系统,它支持多种数据结构,如字符串、列表、哈希表等。它具有高并发读写、持久化、分布式等特点,常用于缓存、消息队列、会话管理等场景。 综上所述,Spring Cloud提供了构建微服务的解决方案,Spring Boot简化了Spring应用程序的开发,OAuth2实现了授权管理,Spring Security提供了身份验证和访问控制,而Redis则可以用于缓存和数据存储。这些技术的结合可以帮助我们构建安全、高效的分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值