SpringBoot(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

 v阅读目录

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-cache</artifactId>        </dependency>
复制代码

激活启动类注解@EnableCaching

SpringBoot(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

v实战演练

2.1 添加service层

复制代码
package com.demo.service;  import com.demo.pojo.UserDetails;  /**  * Created by toutou on 2019/1/20.  */public interface CacheService {     UserDetails getUserDetailsByUid(int uid);      UserDetails updateUserInfo(UserDetails userDetails);      int delUserInfoById(int uid); }
复制代码
复制代码
package com.demo.service;  import com.demo.dao.UserDetailsMapper; import com.demo.pojo.UserDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service;  /**  * Created by toutou on 2019/1/20.  */ @Service public class CacheServiceImpl implements CacheService{      @Autowired     UserDetailsMapper userDetailsMapper;      @Override     @Cacheable(value = "user_details", key = "#uid", unless="#result == null")     public UserDetails getUserDetailsByUid(int uid){         System.out.println(" Cacheable 有请求过来了");         UserDetails userDetails = userDetailsMapper.getUserDetailsByUid(uid);         return userDetails;     }      @Override     @CachePut(value = "user_details", key = "#user.id")     public UserDetails updateUserInfo(UserDetails user){         System.out.println(" CachePut 有请求过来了");         if(userDetailsMapper.updateByPrimaryKeySelective(user) > 0) {             // 这里也可以直接在updateByPrimaryKeySelective的方法里,修改后直接查询出该记录返回UserDetails实例,看需求。            user = userDetailsMapper.getUserDetailsByUid(user.getId());             return user;         }else{             

                    
                
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信