-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathexample_basic.py
More file actions
135 lines (106 loc) · 4.29 KB
/
example_basic.py
File metadata and controls
135 lines (106 loc) · 4.29 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
import json
import pickle
import memcache
from testcontainers.memcached import MemcachedContainer
def basic_example():
with MemcachedContainer() as memcached:
# Get connection parameters
host = memcached.get_container_host_ip()
port = memcached.get_exposed_port(memcached.port)
# Create Memcached client
client = memcache.Client([f"{host}:{port}"])
print("Connected to Memcached")
# Store simple values
client.set("string_key", "Hello from Memcached")
client.set("int_key", 42)
client.set("float_key", 3.14)
print("Stored simple values")
# Store complex data
complex_data = {"name": "test", "values": [1, 2, 3], "nested": {"key": "value"}}
client.set("complex_key", json.dumps(complex_data))
print("Stored complex data")
# Store with expiration
client.set("expiring_key", "This will expire", time=5)
print("Stored value with expiration")
# Store with pickle
class TestObject:
def __init__(self, name, value):
self.name = name
self.value = value
test_obj = TestObject("test", 123)
client.set("object_key", pickle.dumps(test_obj))
print("Stored pickled object")
# Retrieve values
print("\nRetrieved values:")
print(f"string_key: {client.get('string_key')}")
print(f"int_key: {client.get('int_key')}")
print(f"float_key: {client.get('float_key')}")
# Retrieve complex data
complex_value = json.loads(client.get("complex_key"))
print("\nComplex data:")
print(json.dumps(complex_value, indent=2))
# Retrieve pickled object
obj_data = pickle.loads(client.get("object_key"))
print("\nPickled object:")
print(f"name: {obj_data.name}")
print(f"value: {obj_data.value}")
# Check expiration
print("\nChecking expiring key:")
print(f"expiring_key: {client.get('expiring_key')}")
print("Waiting for key to expire...")
import time
time.sleep(6)
print(f"expiring_key after expiration: {client.get('expiring_key')}")
# Store multiple values
multi_data = {"key1": "value1", "key2": "value2", "key3": "value3"}
client.set_multi(multi_data)
print("\nStored multiple values")
# Retrieve multiple values
multi_keys = ["key1", "key2", "key3"]
multi_values = client.get_multi(multi_keys)
print("\nMultiple values:")
print(json.dumps(multi_values, indent=2))
# Increment and decrement
client.set("counter", 0)
client.incr("counter")
client.incr("counter", 2)
print("\nCounter after increment:")
print(f"counter: {client.get('counter')}")
client.decr("counter")
print("Counter after decrement:")
print(f"counter: {client.get('counter')}")
# Store with flags
client.set("flagged_key", "value", flags=1)
print("\nStored value with flags")
# Get stats
stats = client.get_stats()
print("\nMemcached stats:")
for server, server_stats in stats:
print(f"\nServer: {server}")
print(json.dumps(dict(server_stats), indent=2))
# Delete values
client.delete("string_key")
client.delete_multi(["key1", "key2", "key3"])
print("\nDeleted values")
# Check deleted values
print("\nChecking deleted values:")
print(f"string_key: {client.get('string_key')}")
print(f"key1: {client.get('key1')}")
# Store with CAS
client.set("cas_key", "initial")
cas_value = client.gets("cas_key")
print("\nCAS value:")
print(f"value: {cas_value}")
# Update with CAS
success = client.cas("cas_key", "updated", cas_value[1])
print(f"CAS update success: {success}")
print(f"Updated value: {client.get('cas_key')}")
# Try to update with invalid CAS
success = client.cas("cas_key", "failed", 0)
print(f"Invalid CAS update success: {success}")
print(f"Value after failed update: {client.get('cas_key')}")
# Clean up
client.flush_all()
print("\nFlushed all values")
if __name__ == "__main__":
basic_example()