-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_client.py
More file actions
151 lines (121 loc) · 4.7 KB
/
Copy pathpython_client.py
File metadata and controls
151 lines (121 loc) · 4.7 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""
Ignix Python Client Example
This example demonstrates how to use Ignix with the redis-py library.
Ignix is fully compatible with Redis clients.
Installation:
pip install redis
Usage:
python examples/python_client.py
IGNIX_PASSWORD=secret python examples/python_client.py # ignix --requirepass secret
"""
import os
import redis
import time
import sys
def main():
print("🔥 Ignix Python Client Example")
print("=" * 40)
try:
# Connect to Ignix server
print("Connecting to Ignix server at localhost:7379...")
client = redis.Redis(
host='localhost',
port=7379,
# Sent with HELLO or AUTH when the server runs with --requirepass
password=os.environ.get('IGNIX_PASSWORD'),
decode_responses=True, # Automatically decode bytes to strings
socket_connect_timeout=5,
socket_timeout=5
)
# Test connection
print("Testing connection...")
response = client.ping()
print(f"PING response: {response}")
print("\n📝 Basic Operations:")
print("-" * 20)
# SET operation
print("Setting key 'hello' to 'world'...")
client.set('hello', 'world')
print("✅ SET hello world")
# GET operation
value = client.get('hello')
print(f"✅ GET hello: {value}")
# EXISTS operation
exists = client.exists('hello')
print(f"✅ EXISTS hello: {exists}")
# SET with different data types
client.set('counter', 0)
client.set('user:1:name', 'Alice')
client.set('user:1:age', 25)
print("\n🔢 Counter Operations:")
print("-" * 25)
# INCR operation
for i in range(5):
counter = client.incr('counter')
print(f"✅ INCR counter: {counter}")
time.sleep(0.1)
print("\n👤 User Data:")
print("-" * 15)
# Multiple GET operations
name = client.get('user:1:name')
age = client.get('user:1:age')
print(f"✅ User: {name}, Age: {age}")
print("\n🗂️ Bulk Operations:")
print("-" * 20)
# MSET - Multiple SET
client.mset({
'fruit:1': 'apple',
'fruit:2': 'banana',
'fruit:3': 'orange'
})
print("✅ MSET fruit:1=apple, fruit:2=banana, fruit:3=orange")
# MGET - Multiple GET
fruits = client.mget(['fruit:1', 'fruit:2', 'fruit:3'])
print(f"✅ MGET fruits: {fruits}")
print("\n🔄 Key Management:")
print("-" * 20)
# RENAME operation
client.rename('hello', 'greeting')
print("✅ RENAME hello -> greeting")
# Verify rename
old_exists = client.exists('hello')
new_exists = client.exists('greeting')
new_value = client.get('greeting')
print(f"✅ hello exists: {old_exists}, greeting exists: {new_exists}, value: {new_value}")
# DEL operation
deleted = client.delete('greeting')
print(f"✅ DEL greeting: {deleted} key(s) deleted")
print("\n⏳ Expiry:")
print("-" * 10)
# SET with an expiry, then read and remove it
client.set('session:1', 'token', ex=60)
print(f"✅ SET session:1 EX 60, TTL: {client.ttl('session:1')} s")
client.persist('session:1')
print(f"✅ PERSIST session:1, TTL: {client.ttl('session:1')} (no expiry)")
client.set('session:1', 'token', px=100)
time.sleep(0.2)
print(f"✅ After PX 100 and 200 ms: session:1 = {client.get('session:1')}")
print("\n📊 Statistics:")
print("-" * 15)
# The keys this example created, found with SCAN
found = sorted(client.scan_iter(match='*:*'))
print(f"✅ SCAN MATCH *:* found {len(found)} keys: {found}")
print(f"✅ DBSIZE: {client.dbsize()}")
print("\n✅ All operations completed successfully!")
except redis.AuthenticationError as e:
print(f"❌ Authentication Error: {e}")
print("The server runs with --requirepass: set IGNIX_PASSWORD to its password")
sys.exit(1)
except redis.ConnectionError as e:
print(f"❌ Connection Error: {e}")
print("Make sure Ignix server is running: cargo run --release")
sys.exit(1)
except redis.RedisError as e:
print(f"❌ Redis Error: {e}")
sys.exit(1)
except Exception as e:
print(f"❌ Unexpected Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()