-
新增依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> 複製程式碼
-
application.properties
spring.redis.host=127.0.0.1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
複製程式碼 -
Aplication
@SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } 複製程式碼
-
mapper
@Component //為了消除Controller裡面的紅線警告 @Mapper @CacheConfig(cacheNames = "users") public interface UserMapper { @Cacheable User get(Long id); void insert(User user); void update(User user); List<User> list(UserQueryObject qo); void delete(Long id); User findByUsername(String username); Set<String> getRoleNamesByUserId(Long userId); Set<String> getPermissionsNameByRoleNames(@Param("roleNames") Set<String> roleNames); } 複製程式碼
在需要快取的地方新增@Cacheable註解