FreedomBox/plinth/kvstore.py
Sunil Mohan Adapa daabeccb60
db: Serialize most of the database queries using locks
See db.py for rationale.

Tests:

- Run functional tests and unit tests.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-10-23 12:40:13 -04:00

44 lines
1.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Simple key/value store using Django models
"""
from . import db
def get(key):
"""Return the value of a key"""
from plinth.models import KVStore
with db.lock:
# pylint: disable-msg=E1101
return KVStore.objects.get(pk=key).value
def get_default(key, default_value):
"""Return the value of the key if key exists else return default_value"""
with db.lock:
try:
return get(key)
except Exception:
return default_value
def set(key, value): # pylint: disable-msg=W0622
"""Store the value of a key"""
from plinth.models import KVStore
with db.lock:
store = KVStore(key=key, value=value)
store.save()
def delete(key, ignore_missing=False):
"""Delete a key"""
from plinth.models import KVStore
with db.lock:
try:
return KVStore.objects.get(key=key).delete()
except KVStore.DoesNotExist:
if not ignore_missing:
raise