File: memory.py

package info (click to toggle)
python-django-constance 4.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 2,080; makefile: 25; javascript: 23; sh: 6
file content (37 lines) | stat: -rw-r--r-- 954 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)

    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

    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)