天天看點

《若依ruoyi》第四十一章:若依-緩存管理,redis查詢,清理緩存

作者:源碼解析
繼續上一篇文章

1、根據緩存集合,查詢集合下的所有key

《若依ruoyi》第四十一章:若依-緩存管理,redis查詢,清理緩存

如上圖,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代碼路徑

《若依ruoyi》第四十一章:若依-緩存管理,redis查詢,清理緩存
@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);
}           

繼續閱讀