# redis

***

**1. Caching**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Set a key-value pair in the cache
r.set("name", "John Smith")

# Retrieve the value associated with the key
name = r.get("name")
print(name)
```

**2. Rate Limiting**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Define the maximum number of requests per minute
max_requests = 100

# Get the current timestamp
timestamp = time.time()

# Check if the user has exceeded the request limit
requests = r.incr("user:requests", 1)
if requests > max_requests:
    # Block the user from making further requests
    return "Too many requests"
```

**3. Pub/Sub**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a subscriber and subscribe to a channel
pubsub = r.pubsub()
pubsub.subscribe("channel:updates")

# Listen for messages on the channel
for message in pubsub.listen():
    print(message['data'])
```

**4. Session Management**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a session ID
session_id = uuid.uuid4()

# Set the session data in the cache
r.hset("session:" + session_id, "user_id", 1)
r.hset("session:" + session_id, "username", "John Smith")

# Retrieve the session data from the cache
user_id = r.hget("session:" + session_id, "user_id")
username = r.hget("session:" + session_id, "username")
```

**5. Leader Election**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Define the key to be used for the leader election
leader_key = "leader"

# Acquire the lock
with r.lock(leader_key):
    # Perform leader-related tasks
    print("I am the leader")

# Release the lock
r.unlock(leader_key)
```

**6. Geospatial Indexing**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Add a geospatial point to the index
r.geoadd("cities", 12.34, 45.67, "New York")

# Perform a geospatial query
near_cities = r.georadius("cities", 12.34, 45.67, 100, "km")

# Iterate over the results
for city in near_cities:
    print(city['name'])
```

**7. Bloom Filters**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a Bloom filter
bf = r.bf.blpop("my_filter")

# Insert elements into the filter
bf.bfadd("element1")
bf.bfadd("element2")

# Check if an element is in the filter
exists = bf.bfexists("element1")
```

**8. HyperLogLog**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a HyperLogLog
hll = r.hll("my_hll")

# Add elements to the HyperLogLog
hll.pfadd("element1")
hll.pfadd("element2")

# Get the approximate number of unique elements
count = hll.pfcount()
```

**9. Time Series**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a time series database
ts = r.ts("my_ts")

# Add a value to the time series
ts.add("temperature", 25)

# Retrieve the values from the time series
values = ts.range("temperature", -10, -1)

# Iterate over the values
for timestamp, value in values:
    print(timestamp, value)
```

**10. Graph Databases**

```python
import redis

# Initialize Redis client
r = redis.Redis(host='localhost', port=6379)

# Create a graph database
graph = r.graph("my_graph")

# Add nodes to the graph
graph.add_node("John")
graph.add_node("Mary")

# Add edges to the graph
graph.add_edge("John", "Mary")

# Find the shortest path between two nodes
path = graph.path("John", "Mary")

# Iterate over the path
for node in path:
    print(node)
```
