【发布时间】:2021-01-31 01:59:15
【问题描述】:
我有一个连接到 mysql rds 的简单 AWS Lambda 函数。当我在我的应用程序 UI 中更新一个字段时,它会在从 MySQL 工作台查看时在数据库中更新它,但是当使用 Lambda 函数时,它会返回相同的值,直到我重新部署该函数,然后它会为我提供新的正确值
"""Search Function for Lambda"""
from urllib.parse import unquote
import json
import pymysql
# Configuration Values
##CONFIG VALUES REMOVED
# Connection
connection = pymysql.connect(ENDPOINT, user=USERNAME,
passwd=PASSWORD, db=DATABASE_NAME)
def get(event, context):
"""Takes searches key from event and searches the database on that key"""
print(context)
cursor = connection.cursor()
search_key = unquote(event['pathParameters']['search_key'])
cmd = ('SELECT * from LCI_Data WHERE Description Like "{}"'.format("%"+search_key+"%"))
cursor.execute(cmd)
result = [dict((cursor.description[i][0], value)
for i, value in enumerate(row)) for row in cursor.fetchall()]
response = {
"statusCode": 200,
"body": json.dumps(result),
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true"
}
}
return response
【问题讨论】:
-
看起来您正在使用 API 网关。您是否在 API Gateway 中启用了缓存?
-
我还没有检查过,将连接初始化移到函数中解决了我的问题。
-
这没有任何意义。仅仅因为您正在重用连接,它仍然应该每次都执行查询,而不是返回上一个查询的结果。
-
啊,可能和这个有关:github.com/PyMySQL/PyMySQL/issues/76
标签: python mysql amazon-web-services aws-lambda pymysql