| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- # coding=utf-8
- """
- @project: maxkb
- @Author:wangliang181230
- @file: wildcard_compare.py
- @date:2026/3/30 12:11
- @desc:
- """
- import fnmatch
- import re
- from typing import List
- from application.flow.compare import Compare
- from common.cache.mem_cache import MemCache
- match_cache = MemCache('wildcard_to_regex', {
- 'TIMEOUT': 3600, # 缓存有效期为 1 小时
- 'OPTIONS': {
- 'MAX_ENTRIES': 500, # 最多缓存 500 个条目
- 'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存
- },
- })
- def translate_and_compile_and_cache(wildcard):
- match = match_cache.get(wildcard)
- if not match:
- regex = fnmatch.translate(wildcard)
- match = re.compile(regex).match
- match_cache.set(wildcard, match)
- return match
- class WildcardCompare(Compare):
- def support(self, node_id, fields: List[str], source_value, compare, target_value):
- if compare == 'wildcard':
- return True
- def compare(self, source_value, compare, target_value):
- # 转成正则,性能更高
- match = translate_and_compile_and_cache(str(target_value))
- return bool(match(str(source_value)))
|