aio-libs / aio-libs/aiocache

weakref rather than str(class_instance) due to id reuse

未關閉
#641 16 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
1.4k
分支
181
PR 合併指標
30 天內沒有已合併 PR

描述

See [this on SO](https://stackoverflow.com/a/52181584) and links within, or a simple test case as follows:

```python
class Thing():
pass

thing_strs = set()
for idx in range(100):
thing = Thing()
print(f"{idx}: {str(thing)}")
assert str(thing) not in thing_strs, f"{str(thing)} already used (idx={idx})"
thing_strs.add(str(thing))
```

This prints:

```
0: <__main__.Thing object at 0x7fdfe8f74f90>
1: <__main__.Thing object at 0x7fdfe8f74fd0>
2: <__main__.Thing object at 0x7fdfe8f74f90>
Traceback (most recent call last):
File "", line 8, in
AssertionError: <__main__.Thing object at 0x7fdfe8f74f90> already used (idx=2)
```

This means that the cache is not scoped properly. It sounds like using [weakref](https://docs.python.org/3/library/weakref.html) is the better option if possible.

[This](https://github.com/aio-libs/aiocache/blob/master/aiocache/decorators.py#L142) is where it is used currently. Note that this actually applies to all references in the args/kwargs, not just the `self` object.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

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

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