高效缓存 Smart_cache

序言

因为要做文件解析,如果将大量文件数据存入数据库或者去数据库里查肯定不是一个明智的选择,与数据库交互的次数就足以影响性能。又不想去引入其他依赖。所以Smart_cache应运而生
gitte 地址:smart_cahe

HOW TO USE

使用的话比较简单,我简单的提供了几个案例:

	@Test
	public void cacheTest() throws ExecutionException, InterruptedException {
		Cache<String,String> cache = new Cache<>();
		String s3 = cache.get("123");
		System.out.println(s3);
		cache.put("123","456");
		cache.put("456","789");
		String s = cache.get("123");
		System.out.println(s);
		String s1 =  cache.get("123");
		System.out.println(s1);
		String s2 =  cache.get("123");
		System.out.println(s2);
	}
	
	@Test
	public void cacheTestWithTimeOut() throws ExecutionException, InterruptedException {
		Cache<String,String> cache = new Cache<>();
		cache.put("123","456",10);
		Thread.sleep(10000);
		cache.put("123","789",10);
		String s = cache.get("123");
		System.out.println(s);
		String s1 =  cache.get("123");
		System.out.println(s1);
		String s2 =  cache.get("123");
		System.out.println(s2);
	}
	
	@Test
	public void clearTest() throws ExecutionException, InterruptedException {
		Cache<String,String> cache = new Cache<>();
		cache.put("123","456",10);
		String s = cache.get("123");
		System.out.println(s);
		cache.clear();
		System.out.println(cache.get("123"));
	}
	
	@Test
	public void clearTestTime() throws ExecutionException, InterruptedException {
		Cache<String,String> cache = new Cache<String,String>(10L);
		for (int i = 0; i < 100; i++) {
			cache.put(String.valueOf(i),UUID.randomUUID().toString(),6L);
		}
		cache.put("123","456");
		Thread.sleep(10000L);
		for (int i = 0; i < 100; i++) {
			String s = cache.get(String.valueOf(i));
			System.err.println(s);
		}
		System.out.println(cache.get("123"));
	}
	
}

主要功能介绍

Smart_cache 断小而精悍,它是线程安全的,并且可以指定缓存过期时间,定期清理过期缓存等功能。当然了如果有复杂而重复的计算,使用它更高效。代码结构比较简单。且好用。Have a try!

关于后续

Smart_cache 完成的比较仓促,一定还有很多不足的地方,在此我希望大家可以提出建议来,我好改进。
个人后续会设置缓存的多种模式:如 FIFO / HASH 等。
如果大家有什么疑问,可以给我留言,我会一一解答。再次感谢!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值