@@ -10,10 +10,7 @@ import (
1010 "time"
1111
1212 "github.com/AspieSoft/go-regex/v8/common"
13- "github.com/AspieSoft/go-syncterval"
14- "github.com/AspieSoft/go-ttlcache"
1513 "github.com/GRbit/go-pcre"
16- "github.com/pbnjay/memory"
1714)
1815
1916type PCRE pcre.Regexp
@@ -38,27 +35,63 @@ var regCompBGRef *regexp.Regexp = regexp.MustCompile(`%!([0-9]+|o|c)!%`)
3835var regComplexSel * Regexp
3936var regEscape * Regexp
4037
41- var cache * ttlcache.Cache [string , * Regexp ] = ttlcache .New [string , * Regexp ](2 * time .Hour , 1 * time .Hour )
42- var compCache * ttlcache.Cache [string , []byte ] = ttlcache .New [string , []byte ](2 * time .Hour , 1 * time .Hour )
38+ // var cache *ttlcache.Cache[string, *Regexp] = ttlcache.New[string, *Regexp](2 * time.Hour, 1 * time.Hour)
39+ var cache common.CacheMap [* Regexp ] = common .NewCache [* Regexp ]()
40+ var compCache common.CacheMap [[]byte ] = common .NewCache [[]byte ]()
4341
4442func init () {
4543 regComplexSel = Comp (`(\\|)\$([0-9]|\{[0-9]+\})` )
4644 regEscape = Comp (`[\\\^\$\.\|\?\*\+\(\)\[\]\{\}\%]` )
4745
4846 go func (){
49- // clear cache items older than 10 minutes if there are only 200MB of free memory
50- syncterval .New (10 * time .Second , func () {
51- if common .FormatMemoryUsage (memory .FreeMemory ()) < 200 {
52- cache .ClearEarly (10 * time .Minute )
53- compCache .ClearEarly (5 * time .Minute )
47+ for {
48+ time .Sleep (10 * time .Minute )
49+
50+ // default: remove cache items have not been accessed in over 2 hours
51+ cacheTime := 2 * time .Hour
52+
53+ // SysFreeMemory returns the total free system memory in megabytes
54+ mb := common .SysFreeMemory ()
55+ if mb < 200 && mb != 0 {
56+ // low memory: remove cache items have not been accessed in over 10 minutes
57+ cacheTime = 10 * time .Minute
58+ }else if mb < 500 && mb != 0 {
59+ // low memory: remove cache items have not been accessed in over 30 minutes
60+ cacheTime = 30 * time .Minute
61+ }else if mb < 2000 && mb != 0 {
62+ // low memory: remove cache items have not been accessed in over 1 hour
63+ cacheTime = 1 * time .Hour
64+ }else if mb > 64000 {
65+ // high memory: remove cache items have not been accessed in over 12 hour
66+ cacheTime = 12 * time .Hour
67+ }else if mb > 32000 {
68+ // high memory: remove cache items have not been accessed in over 6 hour
69+ cacheTime = 6 * time .Hour
70+ }else if mb > 16000 {
71+ // high memory: remove cache items have not been accessed in over 3 hour
72+ cacheTime = 3 * time .Hour
5473 }
55- })
74+
75+ cache .DelOld (cacheTime )
76+ compCache .DelOld (cacheTime )
77+
78+ time .Sleep (10 * time .Second )
79+
80+ // clear cache if were still critically low on available memory
81+ if mb := common .SysFreeMemory (); mb < 10 && mb != 0 {
82+ cache .DelOld (0 )
83+ compCache .DelOld (0 )
84+ }
85+ }
5686 }()
5787}
5888
5989// this method compiles the RE string to add more functionality to it
6090func compRE (re string , params []string ) string {
61- if val , ok := compCache .Get (re ); ok {
91+ if val , err := compCache .Get (re ); val != nil || err != nil {
92+ if err != nil {
93+ return ""
94+ }
6295 return string (regCompParam .ReplaceAllFunc (val , func (b []byte ) []byte {
6396 if b [1 ] == '{' && b [len (b )- 1 ] == '}' {
6497 b = b [2 :len (b )- 1 ]
@@ -162,7 +195,7 @@ func compRE(re string, params []string) string {
162195 return []byte {}
163196 })
164197
165- compCache .Set (re , reB )
198+ compCache .Set (re , reB , nil )
166199
167200 return string (regCompParam .ReplaceAllFunc (reB , func (b []byte ) []byte {
168201 if b [1 ] == '{' && b [len (b )- 1 ] == '}' {
@@ -185,49 +218,56 @@ func compRE(re string, params []string) string {
185218func Comp (re string , params ... string ) * Regexp {
186219 re = compRE (re , params )
187220
188- if val , ok := cache .Get (re ); ok {
221+ if val , err := cache .Get (re ); val != nil || err != nil {
222+ if err != nil {
223+ panic (err )
224+ }
189225 return val
190- } else {
191- reg := pcre .MustCompile (re , pcre .UTF8 )
226+ }
192227
193- // commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
194- // reg := pcre.MustCompileParse(re)
195- // reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
196- // reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
197- // reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
198- // reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
228+ reg := pcre .MustCompile (re , pcre .UTF8 )
199229
200- compRe := Regexp {RE : reg , len : int64 (len (re ))}
230+ // commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
231+ // reg := pcre.MustCompileParse(re)
232+ // reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
233+ // reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
234+ // reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
235+ // reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
201236
202- cache .Set (re , & compRe )
203- return & compRe
204- }
237+ compRe := Regexp {RE : reg , len : int64 (len (re ))}
238+
239+ cache .Set (re , & compRe , nil )
240+ return & compRe
205241}
206242
207243// CompTry tries to compile or returns an error
208244func CompTry (re string , params ... string ) (* Regexp , error ) {
209245 re = compRE (re , params )
210246
211- if val , ok := cache .Get (re ); ok {
212- return val , nil
213- } else {
214- reg , err := pcre .Compile (re , pcre .UTF8 )
247+ if val , err := cache .Get (re ); val != nil || err != nil {
215248 if err != nil {
216249 return & Regexp {}, err
217250 }
251+ return val , nil
252+ }
218253
219- // commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
220- // reg := pcre.MustCompileParse(re)
221- // reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
222- // reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
223- // reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
224- // reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
254+ reg , err := pcre .Compile (re , pcre .UTF8 )
255+ if err != nil {
256+ cache .Set (re , nil , err )
257+ return & Regexp {}, err
258+ }
225259
226- compRe := Regexp {RE : reg , len : int64 (len (re ))}
260+ // commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
261+ // reg := pcre.MustCompileParse(re)
262+ // reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
263+ // reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
264+ // reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
265+ // reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
227266
228- cache .Set (re , & compRe )
229- return & compRe , nil
230- }
267+ compRe := Regexp {RE : reg , len : int64 (len (re ))}
268+
269+ cache .Set (re , & compRe , nil )
270+ return & compRe , nil
231271}
232272
233273
0 commit comments