Cache Files#

Note

This section only applies to the SQLite and Filesystem backends.

For file-based backends, the cache name will be used as a path to the cache file(s). You can use a relative path, absolute path, or use some additional options for system-specific default paths.

Relative Paths#

>>> # Database path for SQLite cache
>>> session = CachedSession('http_cache', backend='sqlite')
>>> print(session.cache.db_path)
'<current working dir>/http_cache.sqlite'
>>> # Base directory for Filesystem cache
>>> session = CachedSession('http_cache', backend='filesystem')
>>> print(session.cache.cache_dir)
'<current working dir>/http_cache/'

Note

Parent directories will always be created, if they don’t already exist.

Absolute Paths#

You can also give an absolute path, including user paths (with ~).

>>> session = CachedSession('~/.myapp/http_cache', backend='sqlite')
>>> print(session.cache.db_path)
'/home/user/.myapp/http_cache.sqlite'

System Paths#

If you don’t know exactly where you want to put your cache files, your system’s temp directory or cache directory is a good choice. Some options are available as shortcuts for these locations.

Use the default temp directory with the use_temp option:

>>> session = CachedSession('http_cache', backend='sqlite', use_temp=True)
>>> print(session.cache.db_path)
'/tmp/http_cache.sqlite'
>>> session = CachedSession('http_cache', backend='sqlite', use_temp=True)
>>> print(session.cache.db_path)
'/var/folders/xx/http_cache.sqlite'
>>> session = CachedSession('http_cache', backend='sqlite', use_temp=True)
>>> print(session.cache.db_path)
'C:\\Users\\user\\AppData\\Local\\temp\\http_cache.sqlite'

Or use the default cache directory with the use_cache_dir option:

>>> session = CachedSession('http_cache', backend='filesystem', use_cache_dir=True)
>>> print(session.cache.cache_dir)
'/home/user/.cache/http_cache/'
>>> session = CachedSession('http_cache', backend='filesystem', use_cache_dir=True)
>>> print(session.cache.cache_dir)
'/Users/user/Library/Caches/http_cache/'
>>> session = CachedSession('http_cache', backend='filesystem', use_cache_dir=True)
>>> print(session.cache.cache_dir)
'C:\\Users\\user\\AppData\\Local\\http_cache\\'

Note

If the cache name is an absolute path, the use_temp and use_cache_dir options will be ignored. If it’s a relative path, it will be relative to the temp or cache directory, respectively.

There are a number of other system default locations that might be appropriate for a cache file. See the platformdirs library for an easy cross-platform way to get the most commonly used ones.