aio-libs / aio-libs/propcache

add an addtional class that can define a custom cache attribute

未關閉
#200 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
Python
星號
36
分支
23
平均合併
2 小時 24 分鐘
30 天內合併 PR
10

描述

### Is your feature request related to a problem?

I wanted to add custom cache slot locations so that users could have something a bit more beginner friendly to work with.

### Describe the solution you'd like

I had a concept already written for this to exist but I wanted to try adding in a 2 new classes that could help users build caches in different defined slots instead of being limited to only `_cache` and `__dict__`
```py
class under_cached_slot_property(Generic[_T]):
"""Use as a class method decorator.

It operates almost the exact same was as under_cached_property but allow
for a custom slot instead of "_cache" to be used.
"""

def __init__(self, wrapped: Callable[[Any], _T], cache_name:str) -> None:
self.wrapped = wrapped
self.__doc__ = wrapped.__doc__
self.name = wrapped.__name__
self.cache_name = cache_name

@overload
def __get__(self, inst: None, owner: Optional[type[object]] = None) -> Self: ...

@overload
def __get__(
self, inst: Any, owner: Optional[type[object]] = None
) -> _T: ...

def __get__(
self, inst: Optional[Any], owner: Optional[type[object]] = None
) -> Union[_T, Self]:
_cache = getattr(inst, self.cache_name) # type: dict[str, Any]
if inst is None:
return self
try:
return _cache[self.name] # type: ignore[no-any-return]
except KeyError:
val = self.wrapped(inst)
_cache[self.name] = val
return val

def __set__(self, inst: Any, value: _T) -> None:
raise AttributeError("cached property is read-only")

class under_cache_name:
"""Uses a provided cache to create a `under_cached_slot_property` with. a given name"""
def __init__(self, name:str) -> None:
self.name = name

def __call__(self, wrapped: Callable[..., _T]) -> under_cached_attribute_property[_T]:
return under_cached_attribute_property(wrapped)

```

How it would work is pretty simplistic to understand also. The reason the second class is not a function is to
make it faster for cython to compute. A little dumb example will suffice with what I am trying to explain.
```py
from dataclasses import dataclass, field

reify = under_cached_name("_my_cache")

@dataclass(slots=True) # this will work with __slots__.
class MyTool:
i: int
_my_cache: dict[str, Any] = field(default_factory=dict, init=Fasle)

@reify
def cached_item(self) -> int:
return self.i + 10
```

### Describe alternatives you've considered

I'm willing to maintain this new object myself if rejected and put it into it's own little library along with cython optimizations included but also have it include it's own pure python fallback module.

### Additional context

_No response_

### Code of Conduct

- [x] I agree to follow the aio-libs Code of Conduct

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。