|
| 1 | +import json |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from server.caching.cache_provider import CacheProvider |
| 5 | +from server.caching.cache_provider.cache_provider import TimedCache |
| 6 | +from server.caching.cache_request import CacheRequest |
| 7 | +import mysql.connector |
| 8 | +import mysql.connector.cursor |
| 9 | + |
| 10 | +_TABLE_SQL = """ |
| 11 | + CREATE TABLE IF NOT EXISTS cache ( |
| 12 | + id INTEGER PRIMARY KEY AUTO_INCREMENT, |
| 13 | + method TEXT NOT NULL, |
| 14 | + url TEXT NOT NULL, |
| 15 | + response MEDIUMBLOB NOT NULL, |
| 16 | + body TEXT NULL, |
| 17 | + headers TEXT NULL, |
| 18 | + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP |
| 19 | + );""" |
| 20 | + |
| 21 | + |
| 22 | +def _get_current_timestamp() -> str: |
| 23 | + """ |
| 24 | + Get the current timestamp as a string |
| 25 | + :return: |
| 26 | + """ |
| 27 | + return datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 28 | + |
| 29 | + |
| 30 | +class MySQLCacheProvider(CacheProvider): |
| 31 | + def __init__(self, host: str, user: str, password: str, database: str, port: int = 3306): |
| 32 | + self.host = host |
| 33 | + self.user = user |
| 34 | + self.password = password |
| 35 | + self.database = database |
| 36 | + self.port = port |
| 37 | + self.initialized = False |
| 38 | + try: |
| 39 | + self._create_table() |
| 40 | + self.initialized = True |
| 41 | + except mysql.connector.errors.Error as e: |
| 42 | + print("Error while initializing cache: " + str(e)) |
| 43 | + |
| 44 | + def _connect(self) -> mysql.connector.MySQLConnection: |
| 45 | + return mysql.connector.connect( |
| 46 | + host=self.host, |
| 47 | + user=self.user, |
| 48 | + password=self.password, |
| 49 | + database=self.database, |
| 50 | + port=self.port |
| 51 | + ) # type: ignore |
| 52 | + |
| 53 | + def _create_table(self): |
| 54 | + db = self._connect() |
| 55 | + try: |
| 56 | + c = db.cursor() |
| 57 | + c.execute(_TABLE_SQL) |
| 58 | + db.commit() |
| 59 | + except mysql.connector.errors.Error as e: |
| 60 | + print(e) |
| 61 | + |
| 62 | + def _get(self, request: CacheRequest) -> TimedCache | None: |
| 63 | + """ |
| 64 | + Get the response and timestamp from the cache |
| 65 | + :param request: The request to get the response for |
| 66 | + :return: A tuple containing the response and timestamp |
| 67 | + """ |
| 68 | + |
| 69 | + headers = json.dumps(request.headers) if request.headers else "" |
| 70 | + |
| 71 | + db = self._connect() |
| 72 | + c = db.cursor() |
| 73 | + c.execute( |
| 74 | + "SELECT response, timestamp FROM cache WHERE method = %s AND url = %s AND body <=> %s AND headers <=> %s", |
| 75 | + (request.method, request.url, request.body, headers)) |
| 76 | + |
| 77 | + result = c.fetchone() |
| 78 | + if result: |
| 79 | + return result[0], result[1] |
| 80 | + return None |
| 81 | + |
| 82 | + def set(self, request: CacheRequest, response: bytes) -> None: |
| 83 | + """ |
| 84 | + Save the response to the cache |
| 85 | + :param request: The request to save the response for |
| 86 | + :param response: The response to save |
| 87 | + :return: |
| 88 | + """ |
| 89 | + headers = json.dumps(request.headers) if request.headers else "" |
| 90 | + |
| 91 | + db = self._connect() |
| 92 | + c = db.cursor() |
| 93 | + c.execute( |
| 94 | + "SELECT id FROM cache WHERE method = %s AND url = %s AND body <=> %s AND headers <=> %s", |
| 95 | + (request.method, request.url, request.body, headers)) |
| 96 | + existing = c.fetchone() |
| 97 | + |
| 98 | + if existing: |
| 99 | + c.execute("UPDATE cache SET response = %s, timestamp = %s WHERE id = %s", |
| 100 | + (response, _get_current_timestamp(), existing[0])) |
| 101 | + else: |
| 102 | + c.execute( |
| 103 | + "INSERT INTO cache (method, url, response, body, headers, timestamp) VALUES (%s, %s, %s, %s, %s, %s, %s)", |
| 104 | + (request.method, request.url, response, request.body, headers, _get_current_timestamp())) |
| 105 | + |
| 106 | + db.commit() |
0 commit comments