Erste Gehversuche mit einer Redis DB & Python.
Was ist Redis?
The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.
Ich nutze Redis schon länger, z.B. in diesem Forum als Datenbank oder als lokaler Cache für meine Nextcloud Installation.
Die Redis DB läuft als Docker Container zum Testen.
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
und in der Python Umgebung dann
pip install redis
Erster Test
import redis
data = {
'project' : 'Portfolio',
"version": "0.0.1",
"settings_path": "/home/frankm/settings_portfolio.json",
"theme": "dark"
}
r = redis.Redis(host='localhost', port=6379, db=0)
r.json().set('settings', '$', data) # DB setzen und mit Daten (data) füllen
json_settings = r.json().get('settings', '$')
main_settings = r.json().get('settings', '$.main_settings')
project_name = r.json().get('settings', '$..project')
version = r.json().get('settings', '$..version')
print(json_settings)
# [{'main_settings': {'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}}]
print(main_settings)
# [{'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}]
print(project_name)
# ['Portfolio']
print(project_name[0])
# Portfolio
# Wir aktualisieren das data objekt mit einem neuen Wert.
data.update({"version": "0.0.2"})
print("DATA", data)
# Wir speichern das data objekt in der Redis Datenbank
r.json().set('settings', '$', data) # DB setzen und mit Daten (data) füllen
# Kontrolle ob Version 0.0.2 ist
json_settings = r.json().get('settings', '$')
version = r.json().get('settings', '$..version')
print(json_settings)
print(version)
Ausgabe
/home/frankm/PycharmProjects/redis/venv/bin/python /home/frankm/PycharmProjects/redis/main.py
[{'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}]
[]
['Portfolio']
Portfolio
DATA {'project': 'Portfolio', 'version': '0.0.2', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}
[{'project': 'Portfolio', 'version': '0.0.2', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}]
['0.0.2']
Process finished with exit code 0
Erster Test erfolgreich!
Zweiter Test
import redis
data = {
'main_settings': {
'project' : 'Portfolio',
"version": "0.0.1",
"settings_path": "/home/frankm/settings_portfolio.json",
"theme": "dark"
}
}
r = redis.Redis(host='localhost', port=6379, db=0)
r.json().set('settings', '$', data) # DB setzen und mit Daten (data) füllen
json_settings = r.json().get('settings', '$')
main_settings = r.json().get('settings', '$.main_settings')
project_name = r.json().get('settings', '$..project')
version = r.json().get('settings', '$..version')
print(json_settings)
# [{'main_settings': {'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}}]
print(main_settings)
# [{'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}]
print(project_name)
# ['Portfolio']
print(project_name[0])
# Portfolio
# Wir aktualisieren das data objekt mit einem neuen Wert.
data['main_settings']["version"] = "0.0.2"
data.update()
#data.update({"version": "0.0.2"})
print("DATA", data)
# Wir speichern das data objekt in der Redis Datenbank
r.json().set('settings', '$', data) # DB setzen und mit Daten (data) füllen
# Kontrolle ob Version 0.0.2 ist
json_settings = r.json().get('settings', '$')
version = r.json().get('settings', '$..version')
print(json_settings)
print(version)
Ausgabe
/home/frankm/PycharmProjects/redis/venv/bin/python /home/frankm/PycharmProjects/redis/main.py
[{'main_settings': {'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}}]
[{'project': 'Portfolio', 'version': '0.0.1', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}]
['Portfolio']
Portfolio
DATA {'main_settings': {'project': 'Portfolio', 'version': '0.0.2', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}}
[{'main_settings': {'project': 'Portfolio', 'version': '0.0.2', 'settings_path': '/home/frankm/settings_portfolio.json', 'theme': 'dark'}}]
['0.0.2']
Process finished with exit code 0
Process finished with exit code 0
Zweiter Test erfolgreich
Bitte niemals Redis Datenbanken OHNE Passwort im Netz laufen lassen. Danke!!