Getting Started with Redis
This page explains how to start using Redis in Macrometa.
- Create a Redis Mode collection.
- Choose how to interact:
- SDKs
- API
You can access all the familiar Redis commands using the Macrometa SDK or API.
- Python
- REST API
- Step 1. Install the SDK.
- Step 2. Create an instance of the C8Client
- Step 3. Access Redis commands
client.redis.<Redis command>
.
from c8 import C8Client
# Create a connection to GDN
client = C8Client(protocol='https', host='play.paas.macrometa.io', port=443,
email='nemo@nautilus.com', password='xxxxx',
geofabric='_system')
# String data type example
# Set string
client.redis.set("test", "1", REDIS_COLLECTION)
# Get string
response = client.redis.get("test", REDIS_COLLECTION)
# Response from platform
print(response)
# Sorted set data type example
# Add sorted set
client.redis.zadd("testZadd", [1, "test"], REDIS_COLLECTION)
# Return range of elements
response = client.redis.zrange("testZadd", 0, 1, REDIS_COLLECTION)
# Response from platform
print(response)
# List data type example
list_data = ["iron", "gold", "copper"]
client.redis.lpush("list", list_data, REDIS_COLLECTION)
# Return range of list elements
response = client.redis.lrange("list", 0, 1, REDIS_COLLECTION)
# Response from platform
print(response)
# Hash data type example
# Set hash
client.redis.hset(
"games",
{"action": "elden", "driving": "GT7"},
REDIS_COLLECTION
)
# Get hash
response = client.redis.hget("games", "action", REDIS_COLLECTION)
# Response from platform
print(response)
# Sets data type example
client.redis.sadd("animals", ["dog"], REDIS_COLLECTION)
# Pop sets data
response = client.redis.spop("animals", 1, REDIS_COLLECTION)
# Response from platform
print(response)