为什么Redis中key会惊现“乱码”?
最近在做一个秒杀项目,过程中大量应用到了redis。
而我在用ElasticJob进行数据化初始化到Redis数据库时发现这些key都出现了一段前缀“乱码”。
数据结构为Hash,可以观察到hashkey也带有前缀“乱码”
这究竟是怎么回事呢?原来问题出在这:
我使用的是RedisTemplate,而RedisTemplate会出现编码问题,需要自行配置,如果不想配置,可以使用StringRedisTemplate,因为StringRedisTemplate的源码中是有帮我们配置的,因此使用StringRedisTemplate即可完美解决“乱码”问题。
代码: 重点在12行
//让Spring扫描到
@Component
@Getter
@Setter
@Slf4j
public class MyInitSeckillProductsJob implements SimpleJob {
//从nacos取cron
@Value("${jobCron.initSeckillProduct}")
private String cron;
@Autowired
private SeckillProductFeignApi seckillProductFeignApi;
@Autowired//这里要用StringRedisTemplate
private StringRedisTemplate redisTemplate;
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">execute</span><span class="token punctuation">(</span>ShardingContext shardingContext<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token function">doWork</span><span class="token punctuation">(</span>shardingContext<span class="token punctuation">.</span><span class="token function">getShardingParameter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//shardingParameter 就是分片参数 time</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span>String time<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token comment">//key</span>
String SeckillProductKey <span class="token operator">=</span> JobRedisKey<span class="token punctuation">.</span>SECKILL_PRODUCT_LIST<span class="token punctuation">.</span><span class="token function">getRealKey</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
String StockCountKey<span class="token operator">=</span>JobRedisKey<span class="token punctuation">.</span>SECKILL_STOCK_COUNT_HASH<span class="token punctuation">.</span><span class="token function">getRealKey</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//1.先把之前的数据删除掉</span>
redisTemplate<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>SeckillProductKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//2.远程调用秒杀商品服务根据分片参数time查询出result</span>
Result<span class="token operator"><</span>List<span class="token generics function"><span class="token punctuation"><</span>SeckillProductVo<span class="token punctuation">></span></span><span class="token operator">></span> result <span class="token operator">=</span> seckillProductFeignApi<span class="token punctuation">.</span><span class="token function">selectSeckillProductVoByTime</span><span class="token punctuation">(</span>Integer<span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//对list进行判空</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>result <span class="token operator">!=</span> null <span class="token operator">&&</span> <span class="token operator">!</span>result<span class="token punctuation">.</span><span class="token function">hasError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token comment">//从result中取出data</span>
List<span class="token generics function"><span class="token punctuation"><</span>SeckillProductVo<span class="token punctuation">></span></span> data <span class="token operator">=</span> result<span class="token punctuation">.</span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>data <span class="token operator">!=</span> null <span class="token operator">&&</span> data<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>SeckillProductVo seckillProductVo <span class="token operator">:</span> data<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token comment">//3.将Vo对象存到redis中</span>
redisTemplate<span class="token punctuation">.</span><span class="token function">opsForHash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>SeckillProductKey<span class="token punctuation">,</span>seckillProductVo<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">,</span> JSON<span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>seckillProductVo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//4.将秒秒杀商品库存存到redis中</span>
redisTemplate<span class="token punctuation">.</span><span class="token function">opsForHash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>StockCountKey<span class="token punctuation">,</span>seckillProductVo<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">""</span><span class="token punctuation">,</span>seckillProductVo<span class="token punctuation">.</span><span class="token function">getStockCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"今日秒杀商品数据初始化完成"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
}
解决编码问题之后的效果:
最后如果想了解如何使用RedisTemplate配置请浏览:
Redis key 乱码问题(springboot)