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"