mirror of
https://github.com/jazzband/django-constance.git
synced 2026-03-16 22:40:24 +00:00
Some checks failed
Docs / docs (push) Has been cancelled
Test / ruff-format (push) Has been cancelled
Test / ruff-lint (push) Has been cancelled
Test / build (3.10) (push) Has been cancelled
Test / build (3.11) (push) Has been cancelled
Test / build (3.12) (push) Has been cancelled
Test / build (3.13) (push) Has been cancelled
Test / build (3.14) (push) Has been cancelled
Test / build (3.8) (push) Has been cancelled
Test / build (3.9) (push) Has been cancelled
* Added async logic * Added tests and fixed async deadlock on aset * Used abstract base class for backend to simplify code coverage * Reordered try except block * Added explicit thread safety * Fixed linting error * Worked on redis init block * Fixed async test setup * Added tests for redis instantiation * Fixed linting errors
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from threading import Lock
|
|
|
|
from constance import config
|
|
from constance import signals
|
|
|
|
from . import Backend
|
|
|
|
|
|
class MemoryBackend(Backend):
|
|
"""Simple in-memory backend that should be mostly used for testing purposes."""
|
|
|
|
_storage = {}
|
|
_lock = Lock()
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def get(self, key):
|
|
with self._lock:
|
|
return self._storage.get(key)
|
|
|
|
async def aget(self, key):
|
|
# Memory operations are fast enough that we don't need true async here
|
|
return self.get(key)
|
|
|
|
def mget(self, keys):
|
|
if not keys:
|
|
return None
|
|
result = []
|
|
with self._lock:
|
|
for key in keys:
|
|
value = self._storage.get(key)
|
|
if value is not None:
|
|
result.append((key, value))
|
|
return result
|
|
|
|
async def amget(self, keys):
|
|
if not keys:
|
|
return {}
|
|
with self._lock:
|
|
return {key: self._storage[key] for key in keys if key in self._storage}
|
|
|
|
def set(self, key, value):
|
|
with self._lock:
|
|
old_value = self._storage.get(key)
|
|
self._storage[key] = value
|
|
signals.config_updated.send(sender=config, key=key, old_value=old_value, new_value=value)
|
|
|
|
async def aset(self, key, value):
|
|
# Memory operations are fast enough that we don't need true async here
|
|
self.set(key, value)
|