Some improvements in cachedmethods

This commit is contained in:
Miroslav Stampar
2025-12-30 13:04:13 +01:00
parent 1614084709
commit 503c7b62bd
3 changed files with 27 additions and 21 deletions

View File

@@ -42,28 +42,34 @@ def cachedmethod(f):
@functools.wraps(f)
def _f(*args, **kwargs):
parts = (
f.__module__ + "." + f.__name__,
"^".join(repr(a) for a in args),
"^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs))
)
try:
key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff
except (struct.error, ValueError): # https://github.com/sqlmapproject/sqlmap/issues/4281 (NOTE: non-standard Python behavior where hexdigest returns binary value)
result = f(*args, **kwargs)
else:
lock, cache = _method_locks[f], _cache[f]
# NOTE: fast-path
if kwargs:
key = hash((f, args, tuple(map(type, args)), frozenset(kwargs.items()))) & 0x7fffffffffffffff
else:
key = hash((f, args, tuple(map(type, args)))) & 0x7fffffffffffffff
except TypeError:
# NOTE: failback slow-path
parts = (
f.__module__ + "." + f.__name__,
"^".join(repr(a) for a in args),
"^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs))
)
try:
key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff
except (struct.error, ValueError):
return f(*args, **kwargs)
with lock:
if key in cache:
return cache[key]
lock, cache = _method_locks[f], _cache[f]
result = f(*args, **kwargs)
with lock:
if key in cache:
return cache[key]
with lock:
cache[key] = result
result = f(*args, **kwargs)
return result
with lock:
cache[key] = result
return result

View File

@@ -19,7 +19,7 @@ from lib.core.enums import OS
from thirdparty import six
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
VERSION = "1.9.12.34"
VERSION = "1.9.12.35"
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)
@@ -191,7 +191,7 @@ MAX_TECHNIQUES_PER_VALUE = 2
MAX_BUFFERED_PARTIAL_UNION_LENGTH = 1024
# Maximum size of cache used in @cachedmethod decorator
MAX_CACHE_ITEMS = 256
MAX_CACHE_ITEMS = 1024
# Suffix used for naming meta databases in DBMS(es) without explicit database name
METADB_SUFFIX = "_masterdb"