hutool.core.decorators

class-based 装饰器模块。

所有装饰器均支持有括号/无括号、同步/协程四种组合。

使用方式:

# 无括号
@TimeThis
def func(): ...

# 有括号
@ProfileDeco(sort_by="tottime", limit=5)
def func(): ...

# async 也适用
@CacheFunction(ttl=60)
async def fetch(url): ...

Module Contents

Classes

TimeThis

统计函数执行耗时并打印到标准输出。

ProfileDeco

cProfile 性能分析装饰器。

CacheFunction

函数缓存装饰器(dict + TTL)。

Memoize

记忆化装饰器(CacheFunction 子类)。

FuncOnce

函数只执行一次装饰器。

TtlLruCache

带 TTL 的 LRU 缓存装饰器。

NoneOnException

函数抛异常时返回 None。

API

class hutool.core.decorators.TimeThis(func: Optional[Callable] = None)

统计函数执行耗时并打印到标准输出。

支持无括号 @TimeThis 和有括号 @TimeThis() 两种用法, 同时支持同步和异步函数。

Examples:

@TimeThis
def slow():
    time.sleep(0.1)

@TimeThis()
def also_slow():
    time.sleep(0.1)

@TimeThis
async def async_slow():
    await asyncio.sleep(0.1)

Initialization

__call__(*args: Any, **kwargs: Any) Any
_sync_call(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any
class hutool.core.decorators.ProfileDeco(func: Optional[Callable] = None, *, sort_by: str = 'cumtime', limit: int = 10)

cProfile 性能分析装饰器。

每次调用被装饰的函数时,自动运行 cProfile 并打印统计信息。 支持无括号 @ProfileDeco 和有括号 @ProfileDeco(sort_by=...) 两种用法。

参数:
  • sort_by – 排序字段,默认 "cumtime"

  • limit – 打印行数,默认 10

Examples:

@ProfileDeco
def compute(): ...

@ProfileDeco(sort_by="tottime", limit=5)
def compute(): ...

@ProfileDeco
async def async_compute(): ...

Initialization

__call__(*args: Any, **kwargs: Any) Any
_sync_call(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any
class hutool.core.decorators.CacheFunction(func: Optional[Callable] = None, *, ttl: int = 300)

函数缓存装饰器(dict + TTL)。

使用字典缓存函数返回值,超过 TTL 秒后自动失效。 支持无括号 @CacheFunction 和有括号 @CacheFunction(ttl=60) 两种用法。

参数:

ttl – 缓存过期时间(秒),默认 300

Examples:

@CacheFunction(ttl=60)
def expensive(x):
    return x * 2

@CacheFunction
def also_expensive(x):
    return x * 2

@CacheFunction(ttl=60)
async def async_fetch(url):
    return await aio_get(url)

Initialization

__call__(*args: Any, **kwargs: Any) Any
_sync_call(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any
property cache: dict

获取内部缓存字典。

class hutool.core.decorators.Memoize(func: Optional[Callable] = None, *, ttl: int = 600)

Bases: hutool.core.decorators.CacheFunction

记忆化装饰器(CacheFunction 子类)。

CacheFunction 相同,语义上用于记忆化重复计算。 默认 TTL 为 600 秒。

参数:

ttl – 缓存过期时间(秒),默认 600

Examples:

@Memoize(ttl=600)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

Initialization

class hutool.core.decorators.FuncOnce(func: Optional[Callable] = None)

函数只执行一次装饰器。

首次调用后缓存结果,后续调用直接返回缓存值。 支持无括号 @FuncOnce 和有括号 @FuncOnce() 两种用法。

Examples:

@FuncOnce
def init():
    return expensive_setup()

@FuncOnce()
def also_init():
    return expensive_setup()

@FuncOnce
async def async_init():
    return await aio_setup()

Initialization

_sentinel = 'object(...)'
__call__(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any
class hutool.core.decorators.TtlLruCache(func: Optional[Callable] = None, *, maxsize: int = 128, ttl: int = 300)

带 TTL 的 LRU 缓存装饰器。

结合 functools.lru_cache 的 LRU 淘汰策略与 TTL 过期机制。 支持无括号 @TtlLruCache 和有括号 @TtlLruCache(maxsize=64, ttl=120) 两种用法。

参数:
  • maxsize – 最大缓存条目数,默认 128

  • ttl – 缓存过期时间(秒),默认 300

Examples:

@TtlLruCache(maxsize=64, ttl=120)
def expensive(x):
    return x * 2

@TtlLruCache
def also_expensive(x):
    return x * 2

@TtlLruCache(ttl=60)
async def async_fetch(url):
    return await aio_get(url)

Initialization

_build_cache() None

构建内部 lru_cache。

__call__(*args: Any, **kwargs: Any) Any
_get_key(args: tuple, kwargs: dict) tuple
_check_ttl(key: tuple) None
_sync_call(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any
cache_clear() None

清空缓存。

cache_info() Any

获取缓存命中信息。

class hutool.core.decorators.NoneOnException(func: Optional[Callable] = None)

函数抛异常时返回 None。

支持无括号 @NoneOnException 和有括号 @NoneOnException() 两种用法。

Examples:

@NoneOnException
def risky():
    raise ValueError("oops")

assert risky() is None

@NoneOnException
async def async_risky():
    raise ValueError("oops")

assert await async_risky() is None

Initialization

__call__(*args: Any, **kwargs: Any) Any
async _async_call(*args: Any, **kwargs: Any) Any