《若依ruoyi》第四十一章:若依redis查询与清理缓存前后端代码

继续上一篇文章

1、根据缓存集合,查询集合下的所有key

如上图,login_tokens右侧展示的健名列表,一个token值对应一个用户登录。

前端获取API数据代码

/** 查询缓存键名列表 */getCacheKeys(row) {  
const cacheName = row !== undefined ? row.cacheName : this.nowCacheName;  
if (cacheName === "") {    
    return;
  }  
  this.subLoading = true;
  listCacheKey(cacheName).then(response => {    
  this.cacheKeys = response.data;    
  this.subLoading = false;    
  this.nowCacheName = cacheName;
  });
},

业务处理逻辑:

1、判断cacheName是否被选择,首先需要选择一个集合,如果是没有选中,该值为null,则不继续执行。

2、界面显示加载中,因网络通讯有延迟,所以需要提示用加载中,执行代码this.subLoading

3、listCacheKey调用API接口获取数据

4、将数据赋值给cacheKeys,代码是:this.cacheKeys = response.data;

<el-table
  v-loading="subLoading"
  :data="cacheKeys"
  :height="tableHeight"
  highlight-current-row
  @row-click="handleCacheValue"
  style="width: 100%">
  <el-table-column
    label="序号"
    width="60"
    type="index"
  ></el-table-column>
  <el-table-column
    label="缓存键名"
    align="center"
    :show-overflow-tooltip="true"
    :formatter="keyFormatter"
  >
  </el-table-column>
  <el-table-column
    label="操作"
    width="60"
    align="center"
    class-name="small-padding fixed-width"
  >
    <template slot-scope="scope">
      <el-button
        size="mini"
        type="text"
        icon="el-icon-delete"
        @click="handleClearCacheKey(scope.row)"
      ></el-button>
    </template>
  </el-table-column>
</el-table>

列表显示如上代码。其中el-table的数据变量是cacheKeys,这个数据来源是从接口listCacheKey获取。对应上述的赋值代码this.cacheKeys = response.data;

2、后台接口代码

java代码路径

@PreAuthorize("@ss.hasPermi('monitor:cache:list')")
@GetMapping("/getKeys/{cacheName}")
public AjaxResult getCacheKeys(@PathVariable String cacheName)
{
    Set<String> cacheKeys = redisTemplate.keys(cacheName + "*");    
    return AjaxResult.success(cacheKeys);
}

实现业务逻辑:使用redisTemplate.keys获取集合列表。

3、获取某一个key的值

前端请求代码

/** 查询缓存内容详细 */
handleCacheValue(cacheKey) {
  getCacheValue(this.nowCacheName, cacheKey).then(response => {    
      this.cacheForm = response.data;
  });
},

4、后端接口代码

@PreAuthorize("@ss.hasPermi('monitor:cache:list')")
@GetMapping("/getValue/{cacheName}/{cacheKey}")
public AjaxResult getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey)
{
    String cacheValue = redisTemplate.opsForValue().get(cacheKey);
    SysCache sysCache = new SysCache(cacheName, cacheKey, cacheValue);    
    return AjaxResult.success(sysCache);
}

其中前端将两个参数拼接成为url,java后端通过拆解url获取对应的集合和key,详细参见上述代码的{cacheName} 和{cacheKey}

4.1 下面对@PathVariable进行详细解说

@PathVariable 映射 URL 绑定的占位符

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过

@PathVariable(“xxx”) 绑定到操作方法的入参中。

一般与@RequestMapping(method = RequestMethod.GET)一起使用

@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String name){
    return userService.selectUser(name);
}

1、若方法参数名称和需要绑定的url中变量名称一致时,可以简写:

@RequestMapping("/getUser/{name}")
public User getUser(@PathVariable String name){
    return userService.selectUser(name);
}

2、若方法参数名称和需要绑定的url中变量名称不一致时,写成:

@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String userName){
    return userService.selectUser(userName);
}

未来计划

1、ruoyi非分离版本拆解

2、ruoyi-vue-pro:讲解工作流

3、ruoyi-vue-pro:支付模块,电商模块

4、基于ruoyi-vue-pro项目开发

5、JEECG低代码开发平台

请关注我,本星球会持续推出更多的开源项目代码解析,如有更好的意见请留言回复或者私信。

来源: 互联网
本文观点不代表源码解析立场,不承担法律责任,文章及观点也不构成任何投资意见。

赞 ()

相关推荐

发表回复

评论列表

点击查看更多

    联系我们

    在线咨询: QQ交谈

    微信:13450247865

    邮件:451255340#qq.com

    工作时间:周一至周五,9:30-18:30,节假日休息

    微信