Spring boot项目将数据存入Redis

Redis的简单使用

导入依赖

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

编写配置文件

# 指定要使用的Redis数据库索引 Redis默认有16个数据库(编号从0到15) 这里选择的是第9个数据库。
spring.redis.database=9
# 主机地址
spring.redis.host=127.0.0.1
# 端口号
spring.redis.port=6379
# 密码
spring.redis.password=密码
# 连接超时时间
spring.redis.timeout=1000

编写配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式String
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认的序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;


    }
}

这个配置类主要定义了与Redis交互实例redisTemplate的键值序列方式,这里key的序列化方式是String,value采用的是JSON格式

代码

/**
     * 将结果存入Redis
     * @param message
     * @return
     */
    @Override
    public String saveRedis(String message) {
        String key = message+"key";
        //先判断Redis中是否包含当前key
        Boolean b = redisTemplate.hasKey(key);
        if (b){ //直接在Redis里面取
            String value = (String) redisTemplate.opsForValue().get(key);
            log.info("在Redis里取的数据---> "+ value);
            return value;
        }else {
            //存入Redis 设置一小时过期时间
            redisTemplate.opsForValue().set(key,message,Duration.ofHours(1));
            log.info("直接返回的数据---> "+ message);
            return message;
        }
    }

运行结果

同一个数据多次请求的结果

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Springboot项目中,可以通过使用Spring Data Redis来操作Redis数据库。以下是将Json数据存入Redis中的hash中的代码示例: 1. 首先在pom.xml中引入Spring Data Redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties文件中配置Redis连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 ``` 3. 创建一个RedisTemplate对象,用于操作Redis数据库: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; ``` 4. 将Json数据转换为Map对象,并将Map对象存入Redis中的hash中: ```java String jsonStr = "{\"name\":\"张三\",\"age\":18,\"gender\":\"男\"}"; Map<String, String> map = new Gson().fromJson(jsonStr, new TypeToken<Map<String, String>>(){}.getType()); redisTemplate.opsForHash().putAll("person", map); ``` 5. 可以通过以下方式获取Redis中hash的值: ```java Map<Object, Object> resultMap = redisTemplate.opsForHash().entries("person"); ``` 其中,"person"为Redis中的hash键名,可以根据实际情况修改。 需要注意的是,RedisTemplate默认使用JdkSerializationRedisSerializer对数据进行序列化,如果要使用Json数据,需要设置序列化方式为Jackson2JsonRedisSerializer。可以在RedisConfig中进行配置: ```java @Configuration public class RedisConfig { @Autowired private RedisTemplate redisTemplate; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return redisTemplate; } @Bean public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379)); } } ``` 这样就可以在Springboot项目中将Json数据存入Redis中的hash中了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值