adaptai / projects /oui-max /tools /kv_redis.py
ADAPT-Chase's picture
Add files using upload-large-folder tool
fbf3c28 verified
"""
id: kv_redis
title: Key/Value (Redis/DragonFly)
author: system
description: Minimal get/set/del for Redis-compatible endpoints (DragonFly).
version: 0.1.0
requirements: redis>=5.0.0
"""
from typing import Optional
import os
class Tools:
def __init__(self):
pass
def set(self, key: str, value: str, host: str = "127.0.0.1", port: int = 18000) -> dict:
"""
Set a key to a value.
:param key: key name
:param value: string value
:param host: Redis host (DragonFly master default 127.0.0.1)
:param port: Redis port (DragonFly master default 18000)
"""
try:
import redis
except Exception as e:
return {"ok": False, "error": f"redis-py not installed: {e}"}
try:
r = redis.Redis(host=host, port=port, decode_responses=True)
r.set(key, value)
return {"ok": True}
except Exception as e:
return {"ok": False, "error": str(e)}
def get(self, key: str, host: str = "127.0.0.1", port: int = 18000) -> dict:
"""
Get a key's value.
:param key: key name
:param host: Redis host
:param port: Redis port
"""
try:
import redis
except Exception as e:
return {"ok": False, "error": f"redis-py not installed: {e}"}
try:
r = redis.Redis(host=host, port=port, decode_responses=True)
val = r.get(key)
return {"ok": True, "value": val}
except Exception as e:
return {"ok": False, "error": str(e)}
def delete(self, key: str, host: str = "127.0.0.1", port: int = 18000) -> dict:
"""
Delete a key.
"""
try:
import redis
except Exception as e:
return {"ok": False, "error": f"redis-py not installed: {e}"}
try:
r = redis.Redis(host=host, port=port, decode_responses=True)
r.delete(key)
return {"ok": True}
except Exception as e:
return {"ok": False, "error": str(e)}