Skip to content

Commit f3709fa

Browse files
committed
reuse Options
1 parent 98634bc commit f3709fa

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

django_mongodb_backend/cache.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime, timezone
33

44
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
5+
from django.core.cache.backends.db import Options
56
from django.db import connections, router
67
from django.utils.functional import cached_property
78
from pymongo.errors import DuplicateKeyError
@@ -25,26 +26,11 @@ def loads(self, data):
2526
return pickle.loads(data) # noqa: S301
2627

2728

28-
class Options:
29-
"""A class that will quack like a Django model _meta class.
30-
31-
This allows cache operations to be controlled by the router
32-
"""
33-
34-
def __init__(self, collection_name):
35-
self.collection_name = collection_name
36-
self.app_label = "django_cache"
37-
self.model_name = "cacheentry"
38-
self.verbose_name = "cache entry"
39-
self.verbose_name_plural = "cache entries"
40-
self.object_name = "CacheEntry"
41-
self.abstract = False
42-
self.managed = True
43-
self.proxy = False
44-
self.swapped = False
29+
class MongoDBCache(BaseCache):
30+
# This class uses collection provided by the database connection.
4531

32+
pickle_protocol = pickle.HIGHEST_PROTOCOL
4633

47-
class BaseDatabaseCache(BaseCache):
4834
def __init__(self, collection_name, params):
4935
super().__init__(params)
5036
self._collection_name = collection_name
@@ -54,12 +40,6 @@ class CacheEntry:
5440

5541
self.cache_model_class = CacheEntry
5642

57-
58-
class MongoDBCache(BaseDatabaseCache):
59-
# This class uses collection provided by the database connection.
60-
61-
pickle_protocol = pickle.HIGHEST_PROTOCOL
62-
6343
def create_indexes(self):
6444
self.collection.create_index("expires_at", expireAfterSeconds=0)
6545
self.collection.create_index("key", unique=True)

django_mongodb_backend/management/commands/createcachecollection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
router,
88
)
99

10-
from django_mongodb_backend.cache import BaseDatabaseCache, MongoDBCache
10+
from django_mongodb_backend.cache import MongoDBCache
1111

1212

1313
class Command(BaseCommand):
@@ -42,7 +42,7 @@ def handle(self, *collection_names, **options):
4242
else:
4343
for cache_alias in settings.CACHES:
4444
cache = caches[cache_alias]
45-
if isinstance(cache, BaseDatabaseCache):
45+
if isinstance(cache, MongoDBCache):
4646
self.check_collection(db, cache._collection_name)
4747

4848
def check_collection(self, database, collection_name):

0 commit comments

Comments
 (0)