cache with another python module

131 views
Skip to first unread message

stifan kristi

unread,
Jan 14, 2021, 7:05:23 PM1/14/21
to py4web
objective
cache db with another python module lru_cache, TTLCache or another ?

e.g.
from functools import lru_cache
from cachetools import TTLCache

rows = db(query).select(cache = lru_cache(maxsize = 100) )

question
is it possible to cache with another python module

thanks

Massimo DiPierro

unread,
Jan 15, 2021, 1:24:23 AM1/15/21
to stifan kristi, py4web
Short answer yes. They all work. I would need to look at the docs before I can write an example. 

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/67932cf3-30d4-4f00-9c0e-22d49f4615b8n%40googlegroups.com.

stifan kristi

unread,
Jan 15, 2021, 2:43:20 AM1/15/21
to py4web
cachetools
tried and work fine in bottle, and another web framework (blacksheep, fastapi, flask, klein, quart, responder, sanic, starlette) with pydal as backend, should be fine with py4web too
e.g.
from cachetools import TTLCache
cache = TTLCache(maxsize = 99, ttl = 60)
rows = db(query).select(cache = cache, cacheable = True)

Please be aware that all these classes are not thread-safe. Access to a shared cache from multiple threads must be properly synchronized, e.g. by using one of the memoizing decorators with a suitable lock object.
ref

question
- any idea how to make cachetools.TTLCache() more safe thread ?
- any example for use functools.lru_cache in py4web ?

n.b.
pydal dont provide cache in their own package

thanks n best regards,
stifan

Massimo DiPierro

unread,
Jan 15, 2021, 2:57:26 AM1/15/21
to stifan kristi, py4web
There is cache is py4web.


Should be trivil to make it thread safe. I can do it this weekend if nobody does it first.


--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.

Russell

unread,
Jan 17, 2021, 11:16:49 PM1/17/21
to py4web
The py4web cache seems to have left pydal behind?  

For example:
>>>db(db.message.id==35).select(cache=(cache, 60))

Results in:
builtins.TypeError: 'Cache' object is not callable



Massimo

unread,
Jan 17, 2021, 11:25:19 PM1/17/21
to py4web
Made it thread safe
Reply all
Reply to author
Forward
0 new messages