正则表达式 guava

最近我看到了一个关于Google Guava的不错的介绍 ,我们在我们的项目中得出结论,使用它的缓存功能真的很有趣。 让我们看一下regexp Pattern类及其编译功能 。 在代码中我们经常可以看到,每次使用正则表达式时,程序员都会使用相同的参数重复调用上述Pattern.compile()函数,从而一次又一次地编译相同的正则表达式。 但是,可以做的是缓存此类编译的结果–让我们看一下RegexpUtils实用程序类:

RegexpUtils.java

package pl.grzejszczak.marcin.guava.cache.utils;import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import static java.lang.String.format;public final class RegexpUtils {private RegexpUtils() {throw new UnsupportedOperationException("RegexpUtils is a utility class - don't instantiate it!");}private static final LoadingCache<String, Pattern> COMPILED_PATTERNS =CacheBuilder.newBuilder().build(new CacheLoader<String, Pattern>() {@Overridepublic Pattern load(String regexp) throws Exception {return Pattern.compile(regexp);}});public static Pattern getPattern(String regexp) {try {return COMPILED_PATTERNS.get(regexp);} catch (ExecutionException e) {throw new RuntimeException(format("Error when getting a pattern [%s] from cache", regexp), e);}}public static boolean matches(String stringToCheck, String regexp) {return doGetMatcher(stringToCheck, regexp).matches();}public static Matcher getMatcher(String stringToCheck, String regexp) {return doGetMatcher(stringToCheck, regexp);}private static Matcher doGetMatcher(String stringToCheck, String regexp) {Pattern pattern = getPattern(regexp);return pattern.matcher(stringToCheck);}}

如您所见,如果没有找到,则使用带有CacheBuilder的Guava的LoadingCache来填充具有新编译模式的缓存。 由于缓存已编译的模式(如果已经进行了编译),将不会再次重复(在我们的情况下,因为我们没有任何到期设置)。 现在一个简单的测试

GuavaCache.java

package pl.grzejszczak.marcin.guava.cache;import com.google.common.base.Stopwatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.grzejszczak.marcin.guava.cache.utils.RegexpUtils;import java.util.regex.Pattern;import static java.lang.String.format;public class GuavaCache {private static final Logger LOGGER = LoggerFactory.getLogger(GuavaCache.class);public static final String STRING_TO_MATCH = "something";public static void main(String[] args) {runTestForManualCompilationAndOneUsingCache(1);runTestForManualCompilationAndOneUsingCache(10);runTestForManualCompilationAndOneUsingCache(100);runTestForManualCompilationAndOneUsingCache(1000);runTestForManualCompilationAndOneUsingCache(10000);runTestForManualCompilationAndOneUsingCache(100000);runTestForManualCompilationAndOneUsingCache(1000000);}private static void runTestForManualCompilationAndOneUsingCache(int firstNoOfRepetitions) {repeatManualCompilation(firstNoOfRepetitions);repeatCompilationWithCache(firstNoOfRepetitions);}private static void repeatManualCompilation(int noOfRepetitions) {Stopwatch stopwatch = new Stopwatch().start();compileAndMatchPatternManually(noOfRepetitions);LOGGER.debug(format("Time needed to compile and check regexp expression [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));}private static void repeatCompilationWithCache(int noOfRepetitions) {Stopwatch stopwatch = new Stopwatch().start();compileAndMatchPatternUsingCache(noOfRepetitions);LOGGER.debug(format("Time needed to compile and check regexp expression using Cache [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));}private static void compileAndMatchPatternManually(int limit) {for (int i = 0; i < limit; i++) {Pattern.compile("something").matcher(STRING_TO_MATCH).matches();Pattern.compile("something1").matcher(STRING_TO_MATCH).matches();Pattern.compile("something2").matcher(STRING_TO_MATCH).matches();Pattern.compile("something3").matcher(STRING_TO_MATCH).matches();Pattern.compile("something4").matcher(STRING_TO_MATCH).matches();Pattern.compile("something5").matcher(STRING_TO_MATCH).matches();Pattern.compile("something6").matcher(STRING_TO_MATCH).matches();Pattern.compile("something7").matcher(STRING_TO_MATCH).matches();Pattern.compile("something8").matcher(STRING_TO_MATCH).matches();Pattern.compile("something9").matcher(STRING_TO_MATCH).matches();}}private static void compileAndMatchPatternUsingCache(int limit) {for (int i = 0; i < limit; i++) {RegexpUtils.matches(STRING_TO_MATCH, "something");RegexpUtils.matches(STRING_TO_MATCH, "something1");RegexpUtils.matches(STRING_TO_MATCH, "something2");RegexpUtils.matches(STRING_TO_MATCH, "something3");RegexpUtils.matches(STRING_TO_MATCH, "something4");RegexpUtils.matches(STRING_TO_MATCH, "something5");RegexpUtils.matches(STRING_TO_MATCH, "something6");RegexpUtils.matches(STRING_TO_MATCH, "something7");RegexpUtils.matches(STRING_TO_MATCH, "something8");RegexpUtils.matches(STRING_TO_MATCH, "something9");}}}

我们正在运行一系列测试,并检查它们的执行时间。 请注意,由于应用程序不是独立运行的,因此这些测试的结果并不精确,因此许多条件都可能影响执行时间。 我们有兴趣显示一定程度的问题,而不是显示准确的执行时间。 对于给定的迭代次数(1,10,100,1000,10000,100000,1000000),我们要么编译10个正则表达式,要么使用Guava的缓存检索已编译的Pattern,然后将它们与要匹配的字符串进行匹配。 这些是日志:

pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [1] ms, no of iterations [1]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [35] ms, no of iterations [1]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [1] ms, no of iterations [10]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [0] ms, no of iterations [10]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [8] ms, no of iterations [100]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [3] ms, no of iterations [100]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [10] ms, no of iterations [1000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [10] ms, no of iterations [1000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [83] ms, no of iterations [10000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [33] ms, no of iterations [10000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [800] ms, no of iterations [100000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [279] ms, no of iterations [100000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [7562] ms, no of iterations [1000000]
pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [3067] ms, no of iterations [1000000]

您可以在Guava / Cache目录下的此处找到源,或转到URL https://bitbucket.org/gregorin1987/too-much-coding/src

参考:来自我们的JCG合作伙伴 Marcin Grzejszczak(位于Blog 上)的正则表达式模式的Google Guava Cache, 用于编码成瘾者博客。

翻译自: https://www.javacodegeeks.com/2013/04/google-guava-cache-with-regular-expression-patterns.html

正则表达式 guava

正则表达式 guava_带有正则表达式模式的Google Guava Cache相关推荐

  1. 带有正则表达式模式的Google Guava Cache

    最近我看到了一个关于Google Guava的精彩演讲 ,我们在我们的项目中得出结论,使用它的缓存功能真的很有趣. 让我们看一下regexp Pattern类及其编译功能 . 在代码中经常可以看到,每 ...

  2. (翻译)Google Guava Cache

    翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...

  3. mysql caching_Spring Caching抽象和Google Guava Cache

    mysql caching Spring为缓存昂贵的方法调用提供了强大的现成支持. 这里详细介绍了缓存抽象. 我的目标是使用Spring Guava Cache涵盖Spring现在提供的4.0+版本的 ...

  4. Spring Caching抽象和Google Guava Cache

    Spring为缓存昂贵的方法调用提供了强大的开箱即用支持. 这里详细介绍了缓存抽象. 我的目的是要介绍Spring现在为框架的4.0+版本提供的较新的缓存实现之一-使用Google Guava Cac ...

  5. guava_使用Google Guava Cache进行本地缓存

    guava 很多时候,我们将不得不从数据库或另一个Web服务获取数据或从文件系统加载数据. 在涉及网络呼叫的情况下,将存在固有的网络延迟,网络带宽限制. 解决此问题的方法之一是在应用程序本地拥有一个缓 ...

  6. 使用Google Guava Cache进行本地缓存

    很多时候,我们将不得不从数据库或另一个Web服务获取数据或从文件系统加载数据. 在涉及网络呼叫的情况下,将存在固有的网络等待时间,网络带宽限制. 解决此问题的方法之一是在应用程序本地拥有一个缓存. 如 ...

  7. Google Guava Cache高效本地缓存

    目录 Guava Cache使用需求和场景 需求 场景 缓存设置 缓存的并发级别 缓存的初始容量设置 设置最大存储 缓存清除策略 基于存活时间的清除策略 基于容量的清除策略 基于权重的清除 策略 显式 ...

  8. Google guava cache源码解析1--构建缓存器(3)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 下面介绍在LocalCache(CacheBuilder, CacheLoader)中调用的一些方法: Ca ...

  9. Google guava cache源码解析1--构建缓存器(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. CacheBuilder-->maximumSize(long size) /*** 指定cache中 ...

最新文章

  1. android获取图片缩略图,Android系获取图片和视频的缩略图
  2. 邮箱的创建及配置:Exchange2003系列之二
  3. 批量(导入导出)迁移AD中的用户信息和密码到新环境中,同时保持用户在MOSS中的权限【addusers,ldifde,copypwd,UserInfo,tp_SystemID】...
  4. Delphi开发步骤经验谈(C++亦实用)
  5. 处理上百万条的数据库如何提高处理查询速度
  6. oracle 强制 断开,ORA-01092: ORACLE 例程终止。强行断开连接
  7. linux系统中怎么复制,linux下如何屏幕拷贝?
  8. phpVX活码系统源码
  9. classic example2
  10. 计算机专业英语mooc,专业英语(软件)-中国大学mooc-题库零氪
  11. php计算百分比加成_百分比计算器
  12. python杨辉三角函数_Python算法之六:杨辉三角
  13. java ----一个函数传回多个值的总结
  14. 嵌入式软件设计必看书籍
  15. 使用PHP实现文件上传
  16. 计算机出现蓝屏cpu很烫,win7系统电脑蓝屏罪魁祸首CPU超频的解决方法
  17. DELL强化版《最终幻想11》认证电脑出炉
  18. 2021查询高考成绩提前批分数线,快看看!2021高考部分省市“预测分数线”出炉!两点需要注意...
  19. 关于opencv读取图片,无法正常显示
  20. 搭建IPv6签到服务器,并使用FRP获取IPv6信息

热门文章

  1. JQuery AJAX请求结果的null为key时无法进入success方法
  2. 浅谈流处理算法 (1) – 蓄水池采样
  3. 大型网站架构利器-CDN技术
  4. javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——applicationContext.xml
  5. RabbitMQ(mall学习)
  6. android微信分享之视频、URL地址、表情分享
  7. nginx,excel模板下载
  8. d3 i5 神舟精盾k480n_神舟精盾k480n i5 d3和精盾i5 d1哪个好?
  9. Mac 环境变量配置
  10. Http协议的请求和响应