Skip to content

Commit

Permalink
fix(cache) 优化缓存示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Apr 11, 2024
1 parent 70e2de5 commit bb401ef
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
package com.zja.controller;

import com.zja.dto.UserDTO;
import com.zja.model.UserDTO;
import com.zja.service.CacheDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.zja.dto;
package com.zja.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;
Expand All @@ -13,6 +16,9 @@
*/
@ApiModel(value = "用户信息")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class UserDTO implements Serializable {
@ApiModelProperty(value = "用户id")
private String id;
Expand Down
27 changes: 27 additions & 0 deletions starter-cache/src/main/java/com/zja/model/UserRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.zja.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
* @author [email protected]
* @data 2019/6/27 16:35
*/
@ApiModel(value = "用户信息")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class UserRequest implements Serializable {
@ApiModelProperty(value = "用户id")
private String id;
@ApiModelProperty(value = "用户名")
private String name;
}
55 changes: 44 additions & 11 deletions starter-cache/src/main/java/com/zja/service/CacheDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@
package com.zja.service;

import com.zja.constants.CacheKeyConstants;
import com.zja.model.UserDTO;
import com.zja.model.UserRequest;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

//可选的 @CacheConfig
import java.util.*;

// 可选的 @CacheConfig
//@CacheConfig(cacheNames = {"myCache"})
//@CacheConfig(cacheNames = CacheKeyConstants.EntityCacheKey.DATA)
@Service
public class CacheDataService {

/**
* @CachePut
* 功能:将数据存入数据库的同时对数据进行缓存。
* @CachePut 功能:将数据存入数据库的同时对数据进行缓存。
* value 指定缓存块名称,key 指定数据的索引
*/
@CachePut(value = "myCache", key = "#p0")
//@CacheEvict(value = "myCache", key = "#a") // 推荐删除,保证数据一致性
public int save(int a) {
System.out.println("进入【save2】方法");
return a;
return 1;
}

/**
* @Cacheable
* 功能:value值定位缓存块,通过key值从缓存中查找数据。
* @Cacheable 功能:value值定位缓存块,通过key值从缓存中查找数据。
* 实际应用:实际查找数据时,会先检索缓存,如果没找到再检索数据库,然后缓存。
*/
//@Caching
Expand All @@ -44,6 +47,37 @@ public int find(int a) {
return a;
}

// 无参数方方法缓存
@Cacheable(value = "user:find2", key = "find2")
public int find2(int a) {
System.out.println("进入【find】方法");
return a;
}

/**
* 根据请求对象 Object.hashCode() 缓存
*
* @return UserDTO
*/
@Cacheable(value = "user:findUserDTO", key = "#request.hashCode()")
public UserDTO findUserDTO(UserRequest request) {
System.out.println("进入【findUserDTO】方法");
return new UserDTO(request.getId(), request.getName(), new Date());
}

// list 请求对象缓存
@Cacheable(value = "user:findUserDTOList", key = "#requestList.hashCode()")
public List<UserDTO> findUserDTOList(List<UserRequest> requestList) {
System.out.println("进入【findUserDTO】方法");
return Arrays.asList(new UserDTO("1", "666", new Date()), new UserDTO("2", "888", new Date()));
}

@Cacheable(value = "user:findUserDTOList", key = "#objectMap.hashCode()")
public List<UserDTO> findUserDTOList(Map<String,Object> objectMap) {
System.out.println("进入【findUserDTO】方法");
return Arrays.asList(new UserDTO("1", "666", new Date()), new UserDTO("2", "888", new Date()));
}

/**
* 功能:将数据存入数据库的同时对数据进行缓存。
* value指定缓存块名称
Expand All @@ -53,12 +87,11 @@ public int find(int a) {
//@CacheEvict(value = "myCache", key = "#a") // 推荐删除,保证数据一致性
public int update(int a) {
System.out.println("进入【update】方法");
return a;
return 2;
}

/**
* @CacheEvict
* 功能:在指定的缓存块搜索数据,存在则从缓存中移除。
* @CacheEvict 功能:在指定的缓存块搜索数据,存在则从缓存中移除。
* 实际应用:与数据库访问接口配合使用,如果数据存在于数据表中,会同时移除数据库中的数据。
*/
@CacheEvict(value = "myCache", key = "#a")
Expand All @@ -67,9 +100,9 @@ public int delete(int a) {
return a;
}

//方法调用后 清空所有缓存
// 方法调用后 清空所有缓存
@CacheEvict(value = "myCache", allEntries = true)
//方法调用前 清空所有缓存
// 方法调用前 清空所有缓存
//@CacheEvict(value="myCache",beforeInvocation=true)
public void delectAll() {
System.out.println("进入【delectAll】方法");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.zja.service;

import com.zja.model.UserRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author: zhengja
* @since: 2024/04/11 10:44
*/
@SpringBootTest
public class CacheDataServiceTest {

@Autowired
CacheDataService service;

// 简单 保存缓存、更新缓存、查找缓存、删除缓存
@Test
public void test_1() {
service.save(1); // 进入方法,保存数据,并缓存数据
int i = service.find(1); // 不进入方法,直接从缓存中取数据
System.out.println(i);

service.update(1); // 进入方法,更新数据,并更新缓存数据
int i2 = service.find(1); // 不进入方法,直接从缓存中取数据
System.out.println(i2);

service.delete(1); // 进入方法,删除数据,并删除缓存
service.delectAll(); // 进入方法,删除全部数据,并删除全部缓存
}

// 对象
@Test
public void test_2() {
service.findUserDTO(new UserRequest("1", "李四")); // 进入方法,返回数据,并进行缓存数据
service.findUserDTO(new UserRequest("1", "李四")); // 不进入方法,直接从缓存中取数据

service.findUserDTO(new UserRequest("2", "李四")); // 进入方法,返回数据,并进行缓存数据
}

// list
@Test
public void test_3() {
List<UserRequest> requestList = new ArrayList<>();
requestList.add(new UserRequest("1", "李四"));
service.findUserDTOList(requestList); // 进入方法,返回数据,并进行缓存数据
service.findUserDTOList(requestList); // 不进入方法,直接从缓存中取数据

List<UserRequest> requestList2 = new ArrayList<>();
requestList2.add(new UserRequest("1", "李四"));
requestList2.add(new UserRequest("2", "李四"));
service.findUserDTOList(requestList2); // 进入方法,返回数据,并进行缓存数据
}

// map
@Test
public void test_4() {
Map<String, Object> objectMap = new HashMap<>();
objectMap.put("1", "李四");

service.findUserDTOList(objectMap);// 进入方法,返回数据,并进行缓存数据
service.findUserDTOList(objectMap);// 不进入方法,直接从缓存中取数据

Map<String, Object> objectMap2 = new HashMap<>();
objectMap2.put("2", "李四");
service.findUserDTOList(objectMap2);// 进入方法,返回数据,并进行缓存数据
}

}

0 comments on commit bb401ef

Please sign in to comment.