赞
踩
最近公司有个老项目要加缓存处理业务,怕以后又遇上老古董项目又要重写一遍觉得有必要记录一下,下面直接上代码:
如果你的项目没有使用Spring Boot,而是使用传统的Spring框架,你可以按照以下步骤来完成Spring集成Redis:
添加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>
配置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();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。