缓存穿透能力测试(Bee)

200 篇文章 0 订阅
118 篇文章 1 订阅

 缓存穿透是指缓存和数据库中都没有的数据,而用户不断发起请求,如发起为id为“-1”的数据或id为特别大不存在的数据。这时的用户很可能是攻击者,攻击会导致数据库压力过大。

可见缓存穿透会对系统造成多在的隐患。 若一个操作数据库的ORM工具,能有抵御缓存穿透的能力,变会使我们的系统更加安全,也避免我们自己开发该功能耗时耗力!

以下,测试一个具有抵御缓存穿透能力的ORM框架Bee.

测试一级缓存和二级Redis缓存的抵御缓存穿透的能力。

/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package org.teasoft.exam.bee.osql.special;

import org.teasoft.bee.osql.Suid;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.exam.bee.osql.entity.TestUser;
import org.teasoft.honey.osql.core.HoneyConfig;
import org.teasoft.honey.osql.shortcut.BF;

/**
 * 测试缓存穿透.Bee默认能抵御缓存穿透
 * @author Kingstar
 * @since  1.11
 */
public class NullUseCache {
	public static void main(String[] args) {
		test();
	}

	public static void test() {
		try {
			
			HoneyConfig.getHoneyConfig().cache_timeout=10;  //20毫秒
			HoneyConfig.getHoneyConfig().cache_levelTwoTimeout=6; //6秒
//			HoneyContext.setConfigRefresh(true); //没有触发机会
			
//			Cache cache=new BeeExtRedisCache();
//			BeeFactory.getHoneyFactory().setCache(cache);
			
			Suid suid = BF.getSuid();
			TestUser testUser=new TestUser();
			testUser.setId(0L);
			
			suid.select(testUser);
			suid.select(testUser); // test whether use cache
			
			
			SuidRich suidRich = BF.getSuidRich();
			suidRich.selectById(new TestUser(), -1L);
			suidRich.selectById(new TestUser(), -1L);
			
			//放在这没用.要放到第一个查询前.
//			HoneyConfig.getHoneyConfig().cache_timeout=20;  //20毫秒
//			HoneyConfig.getHoneyConfig().cache_levelTwoTimeout=6; //6秒
			HoneyContext.setConfigRefresh(true); //没有触发机会
			
			//测试二级缓存的抵御缓存穿透能力
			suidRich.selectById(new TestUser(), -1L);
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			suidRich.selectById(new TestUser(), -1L);
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值