黑馬程序員Redis入門(mén)到實(shí)戰(zhàn)教程,深度透析redis底層原理+redis分布式
2023-08-05 17:15 作者:bili_48232325622 | 我要投稿

P69 回應(yīng)下面的兄弟,Jmeter批量登陸測(cè)試,是直接接口測(cè)試,看看攔截器就知道了(代碼在下方)
只需要redis中hashMap如下格式:Key:RedisConstans.LOGIN_TOKEN_KEY + token?
value:用戶信息(UserDTO)
就沒(méi)問(wèn)題了。
代碼如下:
@Test public void bulkLogin() { //查詢所有用戶 List<User> users = userService.list(); //獲取resource目錄下的文件路徑(用于存放生成的token) String path = new File("").getAbsolutePath() + "\\src\\main\\resources\\tokens.txt"; //String file = Objects.requireNonNull(BulkLogin.class.getClassLoader().getResource("tokens.txt")).getFile(); //創(chuàng)建字符寫(xiě)入流 FileWriter fw = null; try { fw = new FileWriter(path); } catch (IOException e) { throw new RuntimeException(e); } //創(chuàng)建字符寫(xiě)入緩沖區(qū) BufferedWriter bw = new BufferedWriter(fw); users.forEach(user -> { //8.保存用戶信息到redis中 //8.1隨機(jī)生成token作為登錄令牌 String token = UUID.randomUUID().toString(); //8.2將user對(duì)象轉(zhuǎn)為hashMap存儲(chǔ) UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class); Map<String, Object> map = BeanUtil.beanToMap(userDTO, new HashMap<>(), CopyOptions.create() .setIgnoreNullValue(true) .setFieldValueEditor((field, fieldValue) -> fieldValue.toString()) ); //System.out.println(map); //導(dǎo)入redis stringRedisTemplate.opsForHash().putAll(RedisConstans.LOGIN_TOKEN_KEY + token, map); //設(shè)置有效期,30分鐘 stringRedisTemplate.expire(RedisConstans.LOGIN_TOKEN_KEY, 30, TimeUnit.MINUTES); //這一行代碼是同時(shí)獲取Map中的key和value,當(dāng)然和這個(gè)批量登陸功能沒(méi)關(guān)系 //Set<Map.Entry<String, Object>> set = map.entrySet(); try { //開(kāi)始寫(xiě)入 bw.write(token + "\n"); //強(qiáng)制刷新 bw.flush(); } catch (IOException e) { throw new RuntimeException(e); } }); System.out.println("運(yùn)行成功!"); }
標(biāo)簽: