-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisLambda_v2.py
224 lines (200 loc) · 8.48 KB
/
redisLambda_v2.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
import boto3
import os
from decimal import Decimal
from boto3.dynamodb.conditions import Key
import redis
# DynamoDB configuration
dynamodb = boto3.resource('dynamodb')
TABLE_NAME = os.environ.get('API_TABLE_NAME')
table = dynamodb.Table(TABLE_NAME)
dynamodb_client = boto3.client('dynamodb')
env = os.environ.get('ENV')
aws_region = os.environ.get('REGION')
# Redis configuration
redis_host = os.environ.get('REDIS_ENDPOINT')
redis_port = int(6379)
redis_client = redis.StrictRedis(host=redis_host, port=redis_port, decode_responses=True)
print("redis_client_keys ", redis_client.keys())
# Constants for Redis sorted set
RANKING_KEY = "ranking"
# Get data from Redis
def get_data_from_redis(keys):
try:
pipeline = redis_client.pipeline()
for key in keys:
pipeline.get(json.dumps(key, sort_keys=True))
data = pipeline.execute()
return [json.loads(item) if item else None for item in data]
except Exception as ex:
print(f"Error getting data from Redis: {ex}")
return [None] * len(keys)
# Set data into Redis sorted list with rankings
def set_data_in_redis(data_list):
try:
# Sort data list based on the year
sorted_data = sorted(data_list, key=lambda x: x['year'])
# Store data in Redis and assign rankings
for i, data in enumerate(sorted_data):
# Assign ranking based on sorted order
ranking = i + 1 # Start ranking from 1
# Store data in Redis list with ranking as key
redis_client.set(f'{RANKING_KEY}:{ranking}', json.dumps(data))
print("Data set in Redis")
except Exception as ex:
print(f"Error setting data in Redis: {ex}")
# Get data from Redis sorted list
def get_data_from_redis_sorted_list():
try:
# Get data from each key in Redis sorted list and concatenate
data = []
for key in redis_client.keys(f'{RANKING_KEY}:*'):
data.append(redis_client.get(key))
print("Data retrieved from Redis:")
print(data)
return [json.loads(item) for item in data]
except Exception as ex:
print(f"Error getting data from Redis sorted list: {ex}")
return None
# Print keys in Redis
def print_redis_keys():
try:
keys = redis_client.keys(f'{RANKING_KEY}:*')
print("Keys in Redis:")
for key in keys:
print(key)
except Exception as ex:
print(f"Error printing keys in Redis: {ex}")
# Lambda handler
def handler(event, context):
print('received event:')
print(event)
event1 = json.loads(event['body'])
http_method = event1['httpMethod']
requested_data = event1['body']
# POST Method
if http_method == 'POST':
inserted_items = []
try:
# Batch opration to store the data into dynamodb
with table.batch_writer() as batch:
for item in requested_data:
item = {key: int(value) if isinstance(value, Decimal) else value for key, value in item.items()}
batch.put_item(Item=item)
inserted_items.append(item)
print("inserted_items ", inserted_items)
return {
'statusCode': 200,
'body': json.dumps(f'Successfully inserted {len(inserted_items)} items')
}
except Exception as e:
print(f"Error adding data: {e}")
return {
'statusCode': 500,
'body': json.dumps('An error occurred: {}'.format(e))
}
# GET Method
elif http_method == 'GET':
total_redis_data = []
total_dynamodb_data = []
batch_size = 100
try:
# Batch opration to get the data from dynamodb and redis
print("redis_client_keys_before_get ", redis_client.keys())
for i in range(0, len(requested_data), batch_size):
batch_data = requested_data[i:i + batch_size]
keys_to_get = [{'year': int(item['year']), 'title': item['title']} for item in batch_data]
# Try to fetch data from Redis first
data_from_redis = get_data_from_redis(keys_to_get)
print("Data from Redis", data_from_redis)
# If data is not found in Redis, add into list
total_redis_data.extend(data_from_redis)
# For keys not found in Redis, fetch from DynamoDB
keys_not_in_redis = [key for key in keys_to_get if json.dumps(key, sort_keys=True) not in redis_client.keys()]
print("Keys not in Redis", keys_not_in_redis)
if keys_not_in_redis:
dynamo_response_items = []
for key_batch in [keys_not_in_redis[i:i + batch_size] for i in range(0, len(keys_not_in_redis), batch_size)]:
response = dynamodb.batch_get_item(
RequestItems={
TABLE_NAME: {
'Keys': key_batch
}
}
)
dynamo_response_items.extend(response.get('Responses', {}).get(TABLE_NAME, []))
dynamo_response_items = [{key: int(value) if isinstance(value, Decimal) else value for key, value in item.items()} for item in dynamo_response_items]
print("Data from DynamoDB", dynamo_response_items)
# data found from dynamo
total_dynamodb_data.extend(dynamo_response_items)
# Set fetched data in Redis
set_data_in_redis(dynamo_response_items)
print("redis_client_keys_after_get ", redis_client.keys())
# Call the function to get data sorted by ranking from Redis
print_redis_keys()
sorted_data = get_data_from_redis_sorted_list()
print("Redis data:", sorted_data)
# Print the data and their rankings
print("Data sorted by ranking with rankings in Redis:")
for item, ranking in sorted_data:
print(f"Ranking: {ranking}, Data: {item}")
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps({
'redis_data': total_redis_data,
'dynamodb_data': total_dynamodb_data
})
}
except Exception as ex:
print(f"Error handling GET request: {ex}")
return {
'statusCode': 500,
'body': json.dumps('An error occurred while processing the GET request.')
}
# Delete Method
elif http_method == 'DELETE':
not_found = []
found = []
try:
for item in requested_data[:200]:
key = {'year': item['year'], 'title': item['title']}
# Check if the item exists in DynamoDB
response = table.get_item(Key=key)
if 'Item' in response:
# If the item exists, delete it and add its key to the found list
table.delete_item(Key=key)
found.append(key)
# Also remove from Redis
redis_client.delete(json.dumps(key, sort_keys=True))
redis_client.zrem(RANKING_KEY, json.dumps(key))
else:
# If the item does not exist, add its key to the not_found list
not_found.append(key)
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps({
'found': found,
'not_found': not_found
})
}
except Exception as e:
print(f"Error adding data: {e}")
return {
'statusCode': 500,
'body': json.dumps('An error occurred: {}'.format(e))
}
else:
return {
'statusCode': 405,
'body': json.dumps('Method not allowed')
}