Skip to content

Instantly share code, notes, and snippets.

@peacock0803sz
Last active December 17, 2021 09:24
Show Gist options
  • Save peacock0803sz/1dd640d91543dbd6c94ce234ba603077 to your computer and use it in GitHub Desktop.
Save peacock0803sz/1dd640d91543dbd6c94ce234ba603077 to your computer and use it in GitHub Desktop.
A minimal example for dogpile.cache in pyramid
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)
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)
@peacock0803sz
Copy link
Author

Requirements:

pyramid
dogpile.cache

How to run:

docker run -d --rm -p 6379 redis
python dogpile_minimal.py

@peacock0803sz
Copy link
Author

peacock0803sz commented Dec 16, 2021

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?

@peacock0803sz
Copy link
Author

@peacock0803sz
Copy link
Author

peacock0803sz commented Dec 17, 2021

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