import logging from firefly_client import FireflyDatabase # Set up logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) logger = logging.getLogger("FireflyExample") def demonstrate_string_operations(db): """Example of basic string operations""" print("\n=== String Operations ===") # Set a string value db.string_set("greeting", "Hello, Firefly!") # Get the string value value = db.string_get("greeting") print(f"Retrieved string: {value}") # Delete the key db.delete("greeting") def demonstrate_list_operations(db): """Example of list operations""" print("\n=== List Operations ===") # Push items to a list list_key = "todo_list" db.list_right_push(list_key, "Buy groceries") db.list_right_push(list_key, "Write code") db.list_right_push(list_key, "Exercise") # Get all items items = db.list_range(list_key, 0, -1) print("Todo list items:") for item in items: print(f"- {item}") # Pop an item from the list completed = db.list_left_pop(list_key) print(f"Completed task: {completed}") # Clean up db.delete(list_key) def demonstrate_hash_operations(db): """Example of hash operations""" print("\n=== Hash Operations ===") # Store user profile in a hash user_key = "user:123" db.hash_set(user_key, "name", "Alice") db.hash_set(user_key, "email", "alice@example.com") db.hash_set(user_key, "location", "San Francisco") # Get all user data user_data = db.hash_get_all(user_key) print("User profile:") for field, value in user_data.items(): print(f" {field}: {value}") # Check if a field exists has_email = db.hash_field_exists(user_key, "email") print(f"Has email? {has_email}") # Clean up db.delete(user_key) def demonstrate_pipeline_operations(db): """Example of pipeline operations""" print("\n=== Pipeline Operations ===") # Enable pipeline mode db.set_pipeline_mode(True) db.set_batch_size(3) # Queue multiple commands print("Queueing commands...") db.string_set("counter:1", "100") db.string_set("counter:2", "200") db.string_set("counter:3", "300") # Check queue size queued = db.get_queued_command_count() print(f"Queued commands: {queued}") # Execute all commands print("Executing pipeline...") results = db.flush_pipeline() print(f"Pipeline results: {results}") # Disable pipeline mode db.set_pipeline_mode(False) # Verify results for i in range(1, 4): value = db.string_get(f"counter:{i}") print(f"counter:{i} = {value}") # Clean up for i in range(1, 4): db.delete(f"counter:{i}") def main(): """Main example script demonstrating Firefly database operations""" try: # Connect to Firefly database with FireflyDatabase(host="localhost", port=6379) as db: print("Connected to Firefly database") # Test connection if db.ping(): print("Server is responsive") else: print("Server not responding") return # Run demonstrations demonstrate_string_operations(db) demonstrate_list_operations(db) demonstrate_hash_operations(db) demonstrate_pipeline_operations(db) print("\nExample completed successfully") except Exception as e: logger.error(f"Error: {e}") if __name__ == "__main__": main()