Last active
December 17, 2021 09:24
-
-
Save peacock0803sz/1dd640d91543dbd6c94ce234ba603077 to your computer and use it in GitHub Desktop.
A minimal example for dogpile.cache in pyramid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import random | |
from dogpile.cache import make_region | |
from pyramid.config import Configurator | |
logger = logging.getLogger(__name__) | |
region = make_region() | |
def cache_init(settings: dict): | |
region.configure_from_config( | |
{ | |
"cache.redis.backend": "dogpile.cache.redis", | |
**settings, | |
}, | |
"cache.redis.", | |
) | |
@region.cache_on_arguments() | |
def count_up(i): | |
print(i) | |
return i + random.randint(1, 10) | |
def hello(request): | |
first = count_up(1) | |
second = count_up(1) | |
count_up.invalidate(1) | |
third = count_up(1) | |
another = count_up(2) | |
return {"first": first, "second": second, "third": third, "another": another} | |
def main(global_conf, **settings): | |
config = Configurator(settings=settings) | |
config.add_route("hello", "/hello") | |
config.add_view(hello, route_name="hello", renderer="json") | |
cache_init(settings) | |
return config.make_wsgi_app() | |
if __name__ == "__main__": | |
import waitress | |
waitress.serve(main({}), host="0.0.0.0", port=9999) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from dogpile.cache import make_region | |
from pyramid.config import Configurator | |
logger = logging.getLogger(__name__) | |
region = make_region() | |
def cache_init(settings: dict): | |
region.configure_from_config( | |
{ | |
"cache.redis.backend": "dogpile.cache.redis", | |
**settings, | |
}, | |
"cache.redis.", | |
) | |
class MyClass: # actually, use zope.interface | |
def __init__(self) -> None: | |
pass | |
@region.cache_on_arguments(namespace="foo") | |
def count_up(self, i): | |
print(i) | |
return i + random.randint(1, 10) | |
def hello(request): | |
first = MyClass().count_up(1) | |
second = MyClass().count_up(1) | |
MyClass().count_up.invalidate(1) | |
third = MyClass().count_up(1) | |
another = MyClass().count_up(2) | |
return {"first": first, "second": second, "third": third, "another": another} | |
def main(global_conf, **settings): | |
config = Configurator(settings=settings) | |
config.add_route("hello", "/hello") | |
config.add_view(hello, route_name="hello", renderer="json") | |
cache_init(settings) | |
return config.make_wsgi_app() | |
if __name__ == "__main__": | |
import waitress | |
waitress.serve(main({}), host="0.0.0.0", port=9999) |
Behaviors
Expected (works on dogpile_minimal.py
)
First access: {"first": x, "second": x, "third": y, "another": a}
Second: {"first": y, "second": y, "third": z, "another": a}
(a, x, y, z are integers)
Actually, in with_class.py
Always {"first": x, "second": x, "third": x, "another": a}
Why didn't work invalidation?
ref: Doc for Region.cache_on_arguments
https://dogpilecache.sqlalchemy.org/en/latest/api.html#dogpile.cache.region.CacheRegion.cache_on_arguments
Here is a work-around for me.
def hello(request):
first = MyClass().count_up(1)
second = MyClass().count_up(1)
MyClass().count_up.invalidate(None, 1)
third = MyClass().count_up(1)
another = MyClass().count_up(2)
return {"first": first, "second": second, "third": third, "another": another}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements:
How to run: