# SpringBoot+Redis+Hash存储产品数据

hash结构是一个 key key value 第一个key是reids的key 第二个key是hash的key value是值

SpringBoot+Redis+Hash存储产品数据

##第一步:设置hash的key乱码和value的序列化问题
自定义一个redistemplate的配置类

package com.kuangstudy.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 创建一个json的序列化方式
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置value用jackjson进行处理
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // 设置key用string序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置hash的键
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // 设置hash的value序列化
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

第二步:使用hash完成产品的相关API操作

package com.kuangstudy.controller.hash;
import com.kuangstudy.entity.Product;
import com.kuangstudy.vo.R;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
public class ProductHashController {
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/hash/product/create")
    public R createProduct(Product product) {
        // 1: 将产品存入到DB数据库中
        // 2: 确定HASH的key
        String key = "course:hash:" + product.getId();
        // 3 :把产品信息转换成map
        Map<String, Object> productMap = R.beanToMap(product);
        // 4: 将产品信息存入到hash中 putall等价于: 批量的 hmset key field1 value1 field2 value2 ....
        this.redisTemplate.opsForHash().putAll(key, productMap);
        // 5: 获取hash中的值
		// 单个单个获取
        Object title = this.redisTemplate.opsForHash().get(key, "title");
        Object price = this.redisTemplate.opsForHash().get(key, "price");
        Object description = this.redisTemplate.opsForHash().get(key, "description");
        log.info("从redis缓存中获取的值是:{},{},{}", title, price, description);

		// 取特定属性的值
        // multiGet类似于:hmget key field1 field2 ...fieldn
        List<Object> list = this.redisTemplate.opsForHash().multiGet(key, Arrays.asList("title", "price", "description"));
        log.info("从redis缓存中获取的值是:{}", list);
        return R.ok();
    }
}

第三步:产品价格涨价实现

package com.kuangstudy.controller.hash;
import com.kuangstudy.entity.Product;
import com.kuangstudy.vo.R;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
public class ProductHashController {
    @Autowired
    private RedisTemplate redisTemplate;
 
    @PostMapping("/hash/product/addprice")
    @ApiOperation("产品涨价")
    public R addprice(Integer id, Double price) {
        // 1: 将产品存入到DB数据库中
        // 2: 确定HASH的key
        String key = "course:hash:" + id;
        // 3 :把产品信息转换成map 类似于:hincrbyfloat key field value
		// price 是要涨多少钱
        this.redisTemplate.opsForHash().increment(key, "price", price);
        // 4:获取涨价的产品价格
        Object newprice = this.redisTemplate.opsForHash().get(key, "price");
        return R.ok().data("newprice", newprice);
    }
}

测试:

127.0.0.1:6379> hget course:hash:1 id
"1"
127.0.0.1:6379> hget course:hash:1 price
"27.5"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值