-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GC support #138
base: main
Are you sure you want to change the base?
GC support #138
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -562,6 +562,7 @@ def factory( | |
# keyword-only arguments from here | ||
# building blocks | ||
coder, miss_value, user_interface, storage_class, | ||
maxsize=128, | ||
default_action=Ellipsis, | ||
coder_registry=Ellipsis, | ||
# callback | ||
|
@@ -638,7 +639,7 @@ class RingRope(RopeCore): | |
def __init__(self, *args, **kwargs): | ||
super(RingRope, self).__init__(*args, **kwargs) | ||
self.user_interface = self.user_interface_class(self) | ||
self.storage = self.storage_class(self, storage_backend) | ||
self.storage = self.storage_class(self, storage_backend, maxsize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does redis_hash failure related to this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, Can you tell me your opinion? |
||
_ignorable_keys = suggest_ignorable_keys( | ||
self.callable, ignorable_keys) | ||
_key_prefix = suggest_key_prefix(self.callable, key_prefix) | ||
|
@@ -747,9 +748,10 @@ class BaseStorage(object): | |
are mandatory; Otherwise not. | ||
""" | ||
|
||
def __init__(self, rope, backend): | ||
def __init__(self, rope, backend, maxsize): | ||
self.rope = rope | ||
self.backend = backend | ||
self.maxsize = maxsize | ||
|
||
@abc.abstractmethod | ||
def get(self, key): # pragma: no cover | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
|
||
import ring | ||
|
||
|
||
def test_dict_size_persistence_1(): | ||
cache = {} | ||
MAX_SIZE = 1 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE) | ||
def f_persistent_1(i): | ||
return i | ||
|
||
for i in range(MAX_SIZE * 100): | ||
f_persistent_1(i) | ||
assert len(cache) <= 1 | ||
assert len(cache) <= 1 | ||
|
||
def test_dict_size_persistence_default(): | ||
cache = {} | ||
|
||
@ring.dict(cache) | ||
def f_persistent_default(i): | ||
return i | ||
|
||
for i in range(1000): | ||
f_persistent_default(i) | ||
assert len(cache) <= 128 | ||
assert len(cache) <= 128 | ||
|
||
def test_dict_size_persistence_1000(): | ||
cache = {} | ||
MAX_SIZE = 1000 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE) | ||
def f_persistent_default(i): | ||
return i | ||
|
||
for i in range(MAX_SIZE * 100): | ||
f_persistent_default(i) | ||
assert len(cache) <= MAX_SIZE | ||
assert len(cache) <= MAX_SIZE | ||
|
||
def test_dict_size_persistent_with_delete(): | ||
cache = {} | ||
MAX_SIZE = 10 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE) | ||
def f_persistent_with_delete(i): | ||
return i | ||
|
||
for i in range(MAX_SIZE * 100): | ||
f_persistent_with_delete(i) | ||
assert len(cache) <= MAX_SIZE | ||
if i % 17 == 0: | ||
for pop_count in range(8): | ||
if len(cache) > 0: | ||
cache.popitem() | ||
|
||
|
||
assert len(cache) <= MAX_SIZE | ||
|
||
def test_dict_size_expire_1(): | ||
cache = {} | ||
MAX_SIZE = 1 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE, expire=1) | ||
def f_expire_1(i): | ||
return i | ||
|
||
for i in range(MAX_SIZE * 100): | ||
f_expire_1(i) | ||
assert len(cache) <= MAX_SIZE | ||
assert len(cache) <= MAX_SIZE | ||
|
||
def test_dict_size_expire_default(): | ||
cache = {} | ||
|
||
@ring.dict(cache, expire=1) | ||
def f_expire_default(i): | ||
return i | ||
|
||
for i in range(1000): | ||
f_expire_default(i) | ||
assert len(cache) <= 128 | ||
assert len(cache) <= 128 | ||
|
||
def test_dict_size_expire_1000(): | ||
cache = {} | ||
MAX_SIZE = 1000 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE, expire=1) | ||
def f_expire_1(i): | ||
return i | ||
|
||
for i in range(MAX_SIZE * 100): | ||
f_expire_1(i) | ||
assert len(cache) <= MAX_SIZE | ||
|
||
assert len(cache) <= MAX_SIZE | ||
|
||
def test_dict_size_expire_some(): | ||
import time | ||
cache = {} | ||
MAX_SIZE = 150 | ||
|
||
@ring.dict(cache, maxsize=MAX_SIZE, expire=1) | ||
def f_expire_some_expire(i): | ||
return i | ||
|
||
for _ in range(5): | ||
for i in range(100): | ||
f_expire_some_expire(i) | ||
assert len(cache) <= MAX_SIZE | ||
time.sleep(1) | ||
for i in range(100, 200): | ||
f_expire_some_expire(i) | ||
assert len(cache) <= MAX_SIZE | ||
assert len(cache) <= MAX_SIZE | ||
|
||
|
||
def test_dict_size_expire_with_delete(): | ||
import time | ||
cache = {} | ||
|
||
@ring.dict(cache, expire=1) | ||
def f_expire_with_delete(i): | ||
return i | ||
|
||
for i in range(1000): | ||
f_expire_with_delete(i) | ||
time.sleep(1) | ||
for i in range(1000, 2000): | ||
f_expire_with_delete(i) | ||
assert len(cache) <= 128 | ||
if i % 17 == 0: | ||
for pop_count in range(8): | ||
if len(cache) > 0: | ||
cache.popitem() | ||
|
||
assert len(cache) <= 128 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code looks like it changes the
maxsize
value of the storage class. It will cause side effect for other calls with different value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed
if storage_class is None:
means that we're in a 'default' mode so we're in the somewhat private zone... what about
_maxsize
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is not about the naming rule. The next code will make problem.
We shouldn't set any kind of class variable for decorator parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally missed that. What a shame. I'll figure out a better way.