《转》基于redis的缓存机制的思考和优化

不错的文章,转给大家看看。

原文地址:http://blog.csdn.net/qq_18860653/article/details/54893095。再次感谢原博主。

相对我们对于redis的使用场景都已经想当的熟悉。对于大量的数据,为了缓解接口(数据库)的压力,我们对查询的结果做了缓存的策略。一开始我们的思路是这样的。

1.执行查询

2.缓存中存在数据 -> 查询缓存 

3.缓存中不存在数据 -> 查询实时接口


对此,我简单模拟了我们的缓存机制 。

这是一个查询实时的服务

[java]  view plain  copy
  1. <span style="font-size:14px;">package yyf.Jedis.toolsByRedis.cacheCacheTools;  
  2.   
  3. /** 
  4.  * 模拟服务 
  5.  * @author yuyufeng 
  6.  * 
  7.  */  
  8. public class BaseService {  
  9.     public String query(String req) {  
  10.           
  11.         return "hello:" + req;  
  12.     }  
  13. }  
  14. </span>  

从代码中我们可以看到,这个服务反应应该是非常快的。

[java]  view plain  copy
  1. <span style="font-size:14px;">package yyf.Jedis.toolsByRedis.cacheCacheTools;  
  2.   
  3. import redis.clients.jedis.Jedis;  
  4. import redis.clients.jedis.JedisPool;  
  5. import redis.clients.jedis.JedisPoolConfig;  
  6.   
  7. public class CacheCacheToolTest {  
  8.     static JedisPool jedisPool;  
  9.     static {  
  10.         JedisPoolConfig config = new JedisPoolConfig();  
  11.         config.setMaxTotal(100);  
  12.         config.setMaxIdle(5);  
  13.         config.setMaxWaitMillis(1000);  
  14.         config.setTestOnBorrow(false);  
  15.   
  16.         jedisPool = new JedisPool(config, "127.0.0.1"63791000);  
  17.         Jedis jedis = jedisPool.getResource();  
  18.         jedisPool.returnResource(jedis);  
  19.   
  20.     }  
  21.   
  22.     public static void main(String[] args) {  
  23.         for (int i = 0; i < 5; i++) {  
  24.             new Thread(){@Override  
  25.             public void run() {  
  26.                 //执行查询  
  27.                 query();  
  28.             }}.start();  
  29.               
  30.         }  
  31.   
  32.     }  
  33.   
  34.     public static void query() {  
  35.         BaseService bs = new BaseService();  
  36.         Jedis jedis = jedisPool.getResource();  
  37.         String req = "test123";  
  38.         String res;  
  39.         if (jedis.get(req) == null) {  
  40.             System.out.println("##查询接口服务");  
  41.             res = bs.query(req);  
  42.             jedis.setex(req, 10, res);  
  43.         } else {  
  44.             System.out.println("##查询缓存");  
  45.             res = jedis.get(req);  
  46.         }  
  47.         System.out.println(res);  
  48.         jedisPool.returnResource(jedis);  
  49.     }  
  50.   
  51. }  
  52. </span>  

当5个并发进来的时候,第一个查询实时服务,其余的查询缓存。

[java]  view plain  copy
  1. <span style="font-size:14px;">##查询接口服务  
  2. hello:test123  
  3. ##查询缓存  
  4. ##查询缓存  
  5. ##查询缓存  
  6. hello:test123  
  7. hello:test123  
  8. hello:test123  
  9. ##查询缓存  
  10. hello:test123  
  11. </span>  

看到结果,我们似乎觉得这个查询非常的合理,当时当我们的实时接口查询速度很慢的时候,就暴露出问题来了。

[java]  view plain  copy
  1. <span style="font-size:14px;">package yyf.Jedis.toolsByRedis.cacheCacheTools;  
  2.   
  3. /** 
  4.  * 模拟服务 
  5.  * @author yuyufeng 
  6.  * 
  7.  */  
  8. public class BaseService {  
  9.     public String query(String req) {  
  10.         try {  
  11.             Thread.sleep(1000);  
  12.         } catch (InterruptedException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         return "hello:" + req;  
  16.     }  
  17. }  
  18. </span>  

[java]  view plain  copy
  1. <span style="font-size:14px;">##查询接口服务  
  2. ##查询接口服务  
  3. ##查询接口服务  
  4. ##查询接口服务  
  5. ##查询接口服务  
  6. hello:test123  
  7. hello:test123  
  8. hello:test123  
  9. hello:test123  
  10. hello:test123  
  11. </span>  
结果是,全部都查询的接口服务。这样会导致并发一高,缓存就相当于作用非常小了。


如果在查询实时过程时,对于相同的请求,能够让其等待,那么效率会有大大的提升:(为了模拟,加锁处理)

[java]  view plain  copy
  1. public static void main(String[] args) {  
  2.         beginTime = System.currentTimeMillis();  
  3.         for (int i = 0; i < 5; i++) {  
  4.             new Thread(){@Override  
  5.             public void run() {  
  6.                 //执行查询  
  7.                 synchronized (args) {  
  8.                     query();  
  9.                 }  
  10.                   
  11.                 //System.out.println(System.currentTimeMillis()-beginTime);  
  12.             }}.start();  
  13.               
  14.         }  
  15.   
  16.     }  

[java]  view plain  copy
  1. ##查询缓存  
  2. hello:test123  
  3. ##查询缓存  
  4. hello:test123  
  5. ##查询缓存  
  6. hello:test123  
  7. ##查询缓存  
  8. hello:test123  
  9. ##查询缓存  
  10. hello:test123  

现在就都是查询缓存了。其实对于查询并发这样做是比好的。打个比方:

一堆人需要从一个出口出去,这个出口有一个小门已经可以通过,还有一个大门未打开,需要从小门出去打开。这个大门非常大(redis查询速度非常快)。如果大批的人同时出去(高并发),那么必然在小门挤很长的时间。此时,如果现有一个人去把大门先打开,那么后面的人(包括本来要挤小门的人)可以直接从大门出去,效率肯定是后面的划算。


对于查询实时一次比较慢的情况下,可以先让一个线程进去。让其它线程等待。


当然,这样并不完美。当缓存失效,那么查询就会卡顿一下。为了保证用户能一直流畅的查询,我有如下两种方案:

1.在缓存存在的时间里的进行异步查询去更新缓存。

2.使用二级缓存,并且当一级缓存失效的时候,会去读取二级缓存,二级缓存异步更新。(二级缓存的时间可以很长)


下面是第一种策略的代码模拟:

[java]  view plain  copy
  1. public static void query() {  
  2.         BaseService bs = new BaseService();  
  3.         Jedis jedis = jedisPool.getResource();  
  4.         String req = "test123";  
  5.         String res;  
  6.         if (jedis.get(req) == null) {  
  7.             System.out.println("##查询接口服务");  
  8.             res = bs.query(req);  
  9.             jedis.setex(req, 100, res);  
  10.         } else {  
  11.             System.out.println("##查询缓存");  
  12.             res = jedis.get(req);  
  13.             System.out.println("缓存剩余时间:"+jedis.ttl(req));  
  14.             // 当时间超过10秒,异步更新数据到缓存  
  15.             if (jedis.ttl(req) < 90) {  
  16.                 //模拟得到推送,接受推送,执行  
  17.                 new Thread() {  
  18.                     @Override  
  19.                     public void run() {  
  20.                         String res = bs.query(req);  
  21.                         jedis.setex(req, 100, res);  
  22.                         System.out.println("异步更新数据:"+req);  
  23.                     }  
  24.                 }.start();  
  25.   
  26.             }  
  27.   
  28.         }  
  29.         System.out.println(res);  
  30.         jedisPool.returnResource(jedis);  
  31.     }  


运行结果:
[java]  view plain  copy
  1. ##查询缓存  
  2. 缓存剩余时间:67  
  3. hello:test123  
  4. ##查询缓存  
  5. 缓存剩余时间:67  
  6. hello:test123  
  7. ##查询缓存  
  8. 缓存剩余时间:67  
  9. hello:test123  
  10. ##查询缓存  
  11. 缓存剩余时间:67  
  12. hello:test123  
  13. ##查询缓存  
  14. 缓存剩余时间:67  
  15. hello:test123  
  16. 异步更新数据:test123  
  17. 异步更新数据:test123  
  18. 异步更新数据:test123  
  19. 异步更新数据:test123  
  20. 异步更新数据:test123  

为了保证一段时间内,更新一个缓存只执行一次,做如下锁

[java]  view plain  copy
  1. public static void main(String[] args) {  
  2.         beginTime = System.currentTimeMillis();  
  3.         for (int i = 0; i < 5; i++) {  
  4.             new Thread() {  
  5.                 @Override  
  6.                 public void run() {  
  7.                     // 执行查询  
  8.                         query();  
  9.                     // System.out.println(System.currentTimeMillis()-beginTime);  
  10.                 }  
  11.             }.start();  
  12.   
  13.         }  
  14.   
  15.     }  
  16.   
  17.     public static void query() {  
  18.         BaseService bs = new BaseService();  
  19.         Jedis jedis = jedisPool.getResource();  
  20.         String req = "test123";  
  21.         String res;  
  22.         System.out.println(jedis.get(req));  
  23.         if (jedis.get(req) == null) {  
  24.             System.out.println("##查询接口服务");  
  25.             res = bs.query(req);  
  26.             jedis.setex(req, 100, res);  
  27.         } else {  
  28.             System.out.println("##查询缓存");  
  29.             res = jedis.get(req);  
  30.             System.out.println("缓存剩余时间:"+jedis.ttl(req));  
  31.             // 当时间超过10秒,异步更新数据到缓存  
  32.             if (jedis.ttl(req) < 90) {  
  33.                 //模拟得到推送,接受推送,执行  
  34.                 new Thread() {  
  35.                     @Override  
  36.                     public void run() {  
  37.                           
  38.                         //保证5秒内,一条数据只更新一次  
  39.                         Long incr = jedis.incr("incr-flag-"+req);  
  40.                         jedis.expire("incr-flag-"+req, 5);  
  41.                           
  42.                         if(1 == incr){  
  43.                             String resT = bs.query(req);  
  44.                             jedis.setex(req, 100, resT);  
  45.                             System.out.println("异步更新数据:"+req);  
  46.                         }  
  47.                     }  
  48.                 }.start();  
  49.   
  50.             }  
  51.   
  52.         }  
  53.         jedisPool.returnResource(jedis);  
  54.     }  

运行两次,间隔10秒。运行结果:

[java]  view plain  copy
  1. hello:test123  
  2. ##查询缓存  
  3. hello:test123  
  4. hello:test123  
  5. hello:test123  
  6. hello:test123  
  7. ##查询缓存  
  8. ##查询缓存  
  9. ##查询缓存  
  10. ##查询缓存  
  11. 异步更新数据:test123  


这样,即可保证一次查询比较耗时的情况下,用户能流畅的查询。用户体验大大提升


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值