-
Feature descriptionUse caseOn raspberrypi writing cache file to sdcards kills the sd card. Please add memory only switch to allow the cache to be stored in memory only and maybe with a max cache size parameter. for example : SESSION = CachedSession('demo_cache', expire_after=300, mem_only=0) where mem_only=0 is mem only caching with no size limit; mem_only=1000000 is mem only caching with 1000000 bytes/ 1MB max ram usage for cache; mem_only=None/Not specified then use file based cache as normal. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
Some options are:
Requests-cache currently doesn't have an option to limit the cache to a specific size, but that is a feature I've considered adding. |
Beta Was this translation helpful? Give feedback.
-
Yes this works; I didn't see it in the docs at first. Thanks for this awesome project !! |
Beta Was this translation helpful? Give feedback.
-
btw this might be documented somewhere but what's the benefit of SQL lite in memory vs basic ? |
Beta Was this translation helpful? Give feedback.
-
There won't be a noticeable difference for most use cases, but for advanced or specialized usage, SQLite will give you some more features. For example:
It's possible that one of those backends might be more memory-efficient than the other in some situations, but I haven't benchmarked that in a long time. I'll leave this issue open as a reminder to do that. |
Beta Was this translation helpful? Give feedback.
-
Ok last question I promise; I see docs for custom filtering function that returns a boolean, but is possible to specify a custom cache time using a custom function? For example of the response is less than a megabyte cache it for 1hour else if more than ache it for only 20 minutes ? |
Beta Was this translation helpful? Give feedback.
-
There's not an option to provide a callback for setting expiration time, but here's an example that accomplishes the same thing: from requests_cache import CachedSession
from datetime import datetime, timedelta
session = CachedSession(expire_after=timedelta(hours=1))
r = session.get(url)
# Set a shorter expiration for responses over 1MB
if len(r.content) > 1000000:
expires = datetime.utcnow() + timedelta(minutes=20) # Requires an absolute datetime in UTC
session.cache.save_response(r, expires=expires) |
Beta Was this translation helpful? Give feedback.
Some options are:
backend='memory'
)backend=SQLiteCache(use_memory=True)
)Requests-cache currently doesn't have an option to limit the cache to a specific size, but that is a feature I've considered adding.