mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(wildcard): optimize and add caching (#1634)
* fix(wildcard): avoid excessive allocations every loop * are you going to Scarborough Fair? * ruby ruby ruby ruby * ride on, little murphy * shirley? * to the moon * reggie are you there? * code 99 * my doctorate is in Art History * helps to be consistent * tidy * slow and steady gets the clam * oysters were better anyway * DIAL TONE
This commit is contained in:
parent
bc0f4cc055
commit
982f7ddf68
13 changed files with 177 additions and 60 deletions
71
pkg/regexcache/regex.go
Normal file
71
pkg/regexcache/regex.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package regexcache
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/jellydator/ttlcache/v3"
|
||||
)
|
||||
|
||||
var cache = ttlcache.New[string, *regexp.Regexp](
|
||||
ttlcache.WithTTL[string, *regexp.Regexp](5 * time.Minute),
|
||||
)
|
||||
|
||||
func init() {
|
||||
go cache.Start()
|
||||
}
|
||||
|
||||
func MustCompilePOSIX(pattern string) *regexp.Regexp {
|
||||
item := cache.Get(pattern)
|
||||
if item != nil {
|
||||
return item.Value()
|
||||
}
|
||||
|
||||
reg := regexp.MustCompilePOSIX(pattern)
|
||||
cache.Set(pattern, reg, ttlcache.NoTTL)
|
||||
return reg
|
||||
}
|
||||
|
||||
func MustCompile(pattern string) *regexp.Regexp {
|
||||
item := cache.Get(pattern)
|
||||
if item != nil {
|
||||
return item.Value()
|
||||
}
|
||||
|
||||
reg := regexp.MustCompile(pattern)
|
||||
cache.Set(pattern, reg, ttlcache.NoTTL)
|
||||
return reg
|
||||
}
|
||||
|
||||
func CompilePOSIX(pattern string) (*regexp.Regexp, error) {
|
||||
item := cache.Get(pattern)
|
||||
if item != nil {
|
||||
return item.Value(), nil
|
||||
}
|
||||
|
||||
reg, err := regexp.CompilePOSIX(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cache.Set(pattern, reg, ttlcache.DefaultTTL)
|
||||
return reg, nil
|
||||
}
|
||||
|
||||
func Compile(pattern string) (*regexp.Regexp, error) {
|
||||
item := cache.Get(pattern)
|
||||
if item != nil {
|
||||
return item.Value(), nil
|
||||
}
|
||||
|
||||
reg, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cache.Set(pattern, reg, ttlcache.DefaultTTL)
|
||||
return reg, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue