redis缓存使用

一、添加依赖

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、配置application.properties

1
2
3
4
5
#####################redis做缓存####################
spring.cache.type=redis
spring.redis.host=192.168.175.13
spring.redis.port=6379
spring.redis.password=123456

三、使用

3.1、自定义缓存管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package  com.example.demo.config;
 
import  java.lang.reflect.Method;
import  java.util.HashMap;
import  java.util.Map;
 
import  org.springframework.cache.CacheManager;
import  org.springframework.cache.annotation.CachingConfigurerSupport;
import  org.springframework.cache.interceptor.KeyGenerator;
import  org.springframework.context.annotation.Bean;
import  org.springframework.context.annotation.Configuration;
import  org.springframework.data.redis.cache.RedisCacheManager;
import  org.springframework.data.redis.core.RedisTemplate;
 
/**
  * redis自定义缓存管理器
  *
  * @Author: 我爱大金子
  * @Description: redis自定义缓存管理器
  * @Date: Create in 17:02 2017/7/3
  */
@Configuration
@EnableCaching
public  class  RedisCacheConfiguration  extends  CachingConfigurerSupport {
 
    /**
     * 自定义缓存管理器
     * @Author: 我爱大金子
     * @Description: 自定义缓存管理器
     * @Date: 17:06 2017/7/3
     * @param redisTemplate
     * @return
     */
    @Bean
    public  CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
       RedisCacheManager cacheManager =  new  RedisCacheManager(redisTemplate);
       // 设置默认的过期时间,20秒
       cacheManager.setDefaultExpiration( 20 );
       Map<String, Long> expires =  new  HashMap<String, Long>();
       // 单独设置liuyCache的过期时间为60秒
       expires.put( "liuyCache" , 60L);
       cacheManager.setExpires(expires);
       return  cacheManager;
    }
    
    /**
     * 自定义key. 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
     */
    @Override
    public  KeyGenerator keyGenerator() {
       return  new  KeyGenerator() {
          @Override
          public  Object generate(Object o, Method method, Object... objects) {
             StringBuilder sb =  new  StringBuilder();
             sb.append(o.getClass().getName());
             sb.append(method.getName());
             for  (Object obj : objects) {
                sb.append(obj.toString());
             }
             return  sb.toString();
          }
       };
    }
 
}

3.2、使用做缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package  com.example.demo.cache;
 
import  com.example.demo.mapper.UserMapper;
import  com.example.demo.pojo.User;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.cache.annotation.CacheConfig;
import  org.springframework.cache.annotation.CacheEvict;
import  org.springframework.cache.annotation.CachePut;
import  org.springframework.cache.annotation.Cacheable;
import  org.springframework.stereotype.Service;
 
import  java.util.List;
 
/**
  * 使用redis缓存
  *
  * @Author: 我爱大金子
  * @Description: 使用redis缓存
  * @Date: Create in 17:09 2017/7/3
  */
@CacheConfig (cacheNames =  "liuyCache" )
@Service
public  class  UserService {
     @Autowired
     private  UserMapper userMapper;
 
     /**查询用户*/
     @Cacheable (value = { "user" })
     public  List<User> select(User user){
         System.out.println( "查询功能,缓存找不到,直接读库, user="  + user);
         return  userMapper.select(user);
     }
 
     /**添加用户*/
     @CacheEvict (value = { "user" }, allEntries =  true )
     public  int  insert(User user) {
         int  result = userMapper.insert(user);
         System.out.println( "添加功能,清除缓存,直接写库, id="  + user.getId());
         return  result;
     }
 
     /**根据用户id删除*/
     @CacheEvict (value = { "user" }, allEntries =  true )
     public  int  deleteById(Integer id){
         System.out.println( "据用户id删除,清除缓存,直接写库, id="  + id);
         return  userMapper.deleteById(id);
     }
 
     /**根据用户id更新*/
     @CacheEvict (value = { "user" }, allEntries =  true )
     public  int  updateById(User user){
         System.out.println( "根据用户id更新,清除缓存,直接写库, id="  + user.getId());
         return  userMapper.updateById(user);
     }
     /**根据id查询用户*/
     @Cacheable (value = { "user" })
     public  User selectByPrimaryKey(Integer id){
         System.out.println( "据id查询用户,缓存找不到,直接读库, id="  + id);
         return  userMapper.selectByPrimaryKey(id);
     }
}


说明:

 @Cacheable:取缓存

 @CachePut:修改缓存

 @CacheEvict:删除缓存,allEntries:true表示清除value中的全部缓存,默认为false。

四、测试

访问:http://localhost:8989/druid/sql.html 

 wKiom1lbOJ-Ba5OQAABihKkg8XI098.jpg

访问:http://localhost:8989/user/index 

 wKiom1lbOUODV3CdAAAvH0Qx7Ys921.jpg

查看redis:

 wKioL1lbOcjQXFoPAAA7i3WZ9es242.jpg

idea控制台:

 wKiom1lbOgqSEBqAAAAgdYrBnr4918.jpg


第二次访问:http://localhost:8989/user/index后,查看sql监控,如下:

 wKiom1lbOmiBFDpgAACSEPjzgnA844.jpg

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值