Files
task-001/memory_server.py
2026-05-27 08:29:54 +00:00

39 lines
903 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Memory Server implementation.
This module provides a simple in-memory key-value store server that can be used for testing or as a lightweight cache.
"""
from typing import Dict, Any
import json
class MemoryServer:
"""A very small in-memory keyvalue store."""
def __init__(self) -> None:
self._store: Dict[str, Any] = {}
def set(self, key: str, value: Any) -> None:
self._store[key] = value
def get(self, key: str) -> Any:
return self._store[key]
def delete(self, key: str) -> None:
del self._store[key]
def dump(self) -> str:
return json.dumps(self._store)
@classmethod
def load(cls, data: str) -> "MemoryServer":
obj = cls()
obj._store = json.loads(data)
return obj
if __name__ == "__main__":
srv = MemoryServer()
srv.set("foo", 42)
print(srv.get("foo"))
print(srv.dump())