summaryrefslogtreecommitdiff
path: root/requests_cache/backends
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-03-24 16:02:48 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-03-24 16:10:42 -0500
commited7aac51b44d69bd8d1c5d8bc2272d6b89976a87 (patch)
tree638be2ecb62e706514ebd9a2b059cdb8f87f53ff /requests_cache/backends
parentc4fd835c935c699ee8e4950afc59bafca6b10f69 (diff)
downloadrequests-cache-ed7aac51b44d69bd8d1c5d8bc2272d6b89976a87.tar.gz
Pass along optional kwargs to all storage classes, and make default table names consistent across backends (`'http_cache'`)
Diffstat (limited to 'requests_cache/backends')
-rw-r--r--requests_cache/backends/__init__.py6
-rw-r--r--requests_cache/backends/base.py2
-rw-r--r--requests_cache/backends/dynamodb.py19
-rw-r--r--requests_cache/backends/gridfs.py9
-rw-r--r--requests_cache/backends/mongo.py11
-rw-r--r--requests_cache/backends/redis.py9
-rw-r--r--requests_cache/backends/sqlite.py27
7 files changed, 41 insertions, 42 deletions
diff --git a/requests_cache/backends/__init__.py b/requests_cache/backends/__init__.py
index 6d1282d..1b8e151 100644
--- a/requests_cache/backends/__init__.py
+++ b/requests_cache/backends/__init__.py
@@ -21,6 +21,8 @@ BACKEND_KWARGS = [
'namespace',
'read_capacity_units',
'region_name',
+ 'salt',
+ 'secret_key',
'write_capacity_units',
]
@@ -73,14 +75,14 @@ except ImportError:
DynamoDbCache = None
-def create_backend(backend_name, cache_name, options):
+def create_backend(backend_name, cache_name, kwargs):
if isinstance(backend_name, BaseCache):
return backend_name
if backend_name is None:
backend_name = _get_default_backend_name()
try:
- return registry[backend_name](cache_name, **options)
+ return registry[backend_name](cache_name, **kwargs)
except KeyError:
if backend_name in _backend_dependencies:
raise ImportError('You must install the python package: %s' % _backend_dependencies[backend_name])
diff --git a/requests_cache/backends/base.py b/requests_cache/backends/base.py
index 4c229ed..5687ef0 100644
--- a/requests_cache/backends/base.py
+++ b/requests_cache/backends/base.py
@@ -19,7 +19,7 @@ from ..response import AnyResponse, CachedResponse, ExpirationTime
DEFAULT_HEADERS = requests.utils.default_headers()
-class BaseCache(object):
+class BaseCache:
"""Base class for cache implementations, which can also be used as in-memory cache.
To extend it you can provide dictionary-like objects for
diff --git a/requests_cache/backends/dynamodb.py b/requests_cache/backends/dynamodb.py
index 38d318a..92e9eb1 100644
--- a/requests_cache/backends/dynamodb.py
+++ b/requests_cache/backends/dynamodb.py
@@ -7,22 +7,15 @@ from .base import BaseCache, BaseStorage
class DynamoDbCache(BaseCache):
"""`DynamoDB cache backend"""
- def __init__(self, table_name='requests-cache', **options):
+ def __init__(self, table_name='http_cache', **kwargs):
"""
:param namespace: dynamodb table name (default: ``'requests-cache'``)
:param connection: (optional) ``boto3.resource('dynamodb')``
"""
- super().__init__(**options)
- self.responses = DynamoDbDict(
- table_name,
- 'responses',
- options.get('connection'),
- options.get('endpont_url'),
- options.get('region_name'),
- options.get('read_capacity_units'),
- options.get('write_capacity_units'),
- )
- self.redirects = DynamoDbDict(table_name, 'redirects', self.responses.connection)
+ super().__init__(**kwargs)
+ self.responses = DynamoDbDict(table_name, namespace='responses', **kwargs)
+ kwargs['connection'] = self.responses.connection
+ self.redirects = DynamoDbDict(table_name, namespace='redirects', **kwargs)
class DynamoDbDict(BaseStorage):
@@ -31,7 +24,7 @@ class DynamoDbDict(BaseStorage):
def __init__(
self,
table_name,
- namespace='dynamodb_dict_data',
+ namespace='http_cache',
connection=None,
endpoint_url=None,
region_name='us-east-1',
diff --git a/requests_cache/backends/gridfs.py b/requests_cache/backends/gridfs.py
index d39bed5..2c59fd1 100644
--- a/requests_cache/backends/gridfs.py
+++ b/requests_cache/backends/gridfs.py
@@ -17,14 +17,15 @@ class GridFSCache(BaseCache):
requests_cache.install_cache(backend='gridfs', connection=MongoClient('another-host.local'))
"""
- def __init__(self, db_name, **options):
+ def __init__(self, db_name, **kwargs):
"""
:param db_name: database name
:param connection: (optional) ``pymongo.Connection``
"""
- super().__init__(**options)
- self.responses = GridFSPickleDict(db_name, options.get('connection'))
- self.redirects = MongoDict(db_name, 'redirects', self.responses.connection)
+ super().__init__(**kwargs)
+ self.responses = GridFSPickleDict(db_name, **kwargs)
+ kwargs['connection'] = self.responses.connection
+ self.redirects = MongoDict(db_name, collection_name='redirects', **kwargs)
class GridFSPickleDict(BaseStorage):
diff --git a/requests_cache/backends/mongo.py b/requests_cache/backends/mongo.py
index cfb7900..e0531a3 100644
--- a/requests_cache/backends/mongo.py
+++ b/requests_cache/backends/mongo.py
@@ -6,20 +6,21 @@ from .base import BaseCache, BaseStorage
class MongoCache(BaseCache):
"""MongoDB cache backend"""
- def __init__(self, db_name='requests-cache', **options):
+ def __init__(self, db_name='http_cache', **kwargs):
"""
:param db_name: database name (default: ``'requests-cache'``)
:param connection: (optional) ``pymongo.Connection``
"""
- super().__init__(**options)
- self.responses = MongoPickleDict(db_name, 'responses', options.get('connection'))
- self.redirects = MongoDict(db_name, 'redirects', self.responses.connection)
+ super().__init__(**kwargs)
+ self.responses = MongoPickleDict(db_name, collection_name='responses', **kwargs)
+ kwargs['connection'] = self.responses.connection
+ self.redirects = MongoDict(db_name, collection_name='redirects', **kwargs)
class MongoDict(BaseStorage):
"""A dictionary-like interface for a MongoDB collection"""
- def __init__(self, db_name, collection_name='mongo_dict_data', connection=None, **kwargs):
+ def __init__(self, db_name, collection_name='http_cache', connection=None, **kwargs):
"""
:param db_name: database name (be careful with production databases)
:param collection_name: collection name (default: mongo_dict_data)
diff --git a/requests_cache/backends/redis.py b/requests_cache/backends/redis.py
index f08084a..cacb7c2 100644
--- a/requests_cache/backends/redis.py
+++ b/requests_cache/backends/redis.py
@@ -11,10 +11,11 @@ class RedisCache(BaseCache):
connection: (optional) Redis connection instance to use instead of creating a new one
"""
- def __init__(self, namespace='requests-cache', connection: Redis = None, **kwargs):
+ def __init__(self, namespace='http_cache', **kwargs):
super().__init__(**kwargs)
- self.responses = RedisDict(namespace, 'responses', connection)
- self.redirects = RedisDict(namespace, 'redirects', self.responses.connection)
+ self.responses = RedisDict(namespace, collection_name='responses', **kwargs)
+ kwargs['connection'] = self.responses.connection
+ self.redirects = RedisDict(namespace, collection_name='redirects', **kwargs)
class RedisDict(BaseStorage):
@@ -30,7 +31,7 @@ class RedisDict(BaseStorage):
connection: (optional) Redis connection instance to use instead of creating a new one
"""
- def __init__(self, namespace, collection_name='redis_dict_data', connection=None, **kwargs):
+ def __init__(self, namespace, collection_name='http_cache', connection=None, **kwargs):
super().__init__(**kwargs)
if connection is not None:
self.connection = connection
diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py
index 43a7588..ac06ec3 100644
--- a/requests_cache/backends/sqlite.py
+++ b/requests_cache/backends/sqlite.py
@@ -1,6 +1,7 @@
import sqlite3
import threading
from contextlib import contextmanager
+from os.path import expanduser
from .base import BaseCache, BaseStorage
@@ -8,20 +9,20 @@ from .base import BaseCache, BaseStorage
class DbCache(BaseCache):
"""SQLite cache backend.
- Reading is fast, saving is a bit slower. It can store big amount of data
- with low memory usage.
+ Reading is fast, saving is a bit slower. It can store big amount of data with low memory usage.
+
+ Args:
+ location: database filename prefix
+ extension: Database file extension
+ fast_save: Speedup cache saving up to 50 times but with possibility of data loss.
+ See :ref:`backends.DbDict <backends_dbdict>` for more info
"""
- def __init__(self, location='cache', fast_save=False, extension='.sqlite', **options):
- """
- :param location: database filename prefix (default: ``'cache'``)
- :param fast_save: Speedup cache saving up to 50 times but with possibility of data loss.
- See :ref:`backends.DbDict <backends_dbdict>` for more info
- :param extension: extension for filename (default: ``'.sqlite'``)
- """
- super().__init__(**options)
- self.responses = DbPickleDict(str(location) + extension, 'responses', fast_save=fast_save)
- self.redirects = DbDict(location + extension, 'redirects')
+ def __init__(self, location='http_cache', extension='.sqlite', fast_save=False, **kwargs):
+ super().__init__(**kwargs)
+ db_path = expanduser(str(location) + extension)
+ self.responses = DbPickleDict(db_path, table_name='responses', fast_save=fast_save, **kwargs)
+ self.redirects = DbDict(db_path, table_name='redirects', **kwargs)
class DbDict(BaseStorage):
@@ -38,7 +39,7 @@ class DbDict(BaseStorage):
correspondent tables: ``table1``, ``table2`` and ``table3``
"""
- def __init__(self, filename, table_name='data', fast_save=False, **kwargs):
+ def __init__(self, filename, table_name='http_cache', fast_save=False, **kwargs):
"""
:param filename: filename for database (without extension)
:param table_name: table name