66import java .io .File ;
77import java .util .ArrayList ;
88import java .util .List ;
9+ import java .util .concurrent .ExecutionException ;
10+ import java .util .concurrent .TimeUnit ;
911import java .util .regex .Pattern ;
1012
13+ import com .google .common .cache .CacheBuilder ;
14+ import com .google .common .cache .CacheLoader ;
15+ import com .google .common .cache .LoadingCache ;
16+ import com .google .common .util .concurrent .UncheckedExecutionException ;
1117import org .apache .commons .lang .StringUtils ;
1218
1319import com .github .dockerjava .core .exception .GoLangFileMatchException ;
@@ -52,6 +58,11 @@ private GoLangFileMatch() {
5258
5359 private static final String PATTERN_CHARS_TO_ESCAPE = "\\ .[]{}()*+-?^$|" ;
5460
61+ private static final LoadingCache <String , Pattern > PATTERN_CACHE = CacheBuilder .newBuilder ()
62+ .expireAfterAccess (1 , TimeUnit .HOURS )
63+ .maximumSize (10_000 )
64+ .build (CacheLoader .from (GoLangFileMatch ::buildPattern ));
65+
5566 public static boolean match (List <String > patterns , File file ) {
5667 return !match (patterns , file .getPath ()).isEmpty ();
5768 }
@@ -74,7 +85,11 @@ public static List<String> match(List<String> patterns, String name) {
7485 }
7586
7687 public static boolean match (String pattern , String name ) {
77- return buildPattern (pattern ).matcher (name ).matches ();
88+ try {
89+ return PATTERN_CACHE .get (pattern ).matcher (name ).matches ();
90+ } catch (ExecutionException | UncheckedExecutionException e ) {
91+ throw new GoLangFileMatchException (e .getCause ().getMessage ());
92+ }
7893 }
7994
8095 private static Pattern buildPattern (String pattern ) {
0 commit comments