Adds a ProcessedQuery to the cache Args: key(str): A key generated by QueryCache.get_key() for this example processed_query(ProcessedQuery): The ProcessedQuery generated for this example Returns: integer: The unique id of the query in the
(self, key, processed_query)
| 165 | return row[0] if row else None |
| 166 | |
| 167 | def put(self, key, processed_query): |
| 168 | """ |
| 169 | Adds a ProcessedQuery to the cache |
| 170 | |
| 171 | Args: |
| 172 | key(str): A key generated by QueryCache.get_key() for this example |
| 173 | processed_query(ProcessedQuery): The ProcessedQuery generated for this example |
| 174 | Returns: |
| 175 | integer: The unique id of the query in the cache. |
| 176 | """ |
| 177 | data = json.dumps(processed_query.to_cache()) |
| 178 | |
| 179 | def commit_to_db(connection): |
| 180 | cursor = connection.cursor() |
| 181 | cursor.execute(""" |
| 182 | INSERT OR IGNORE into queries values (?, ?, ?, ?, ?); |
| 183 | """, (key, |
| 184 | data, |
| 185 | processed_query.query.text, |
| 186 | processed_query.domain, |
| 187 | processed_query.intent, |
| 188 | )) |
| 189 | connection.commit() |
| 190 | |
| 191 | if self.memory_connection: |
| 192 | commit_to_db(self.memory_connection) |
| 193 | rowid = self.key_to_row_id(key) |
| 194 | self.batch_writes.append(str(rowid)) |
| 195 | if len(self.batch_writes) == self.batch_write_size: |
| 196 | self.flush_to_disk() |
| 197 | else: |
| 198 | commit_to_db(self.disk_connection) |
| 199 | |
| 200 | return self.key_to_row_id(key) |
| 201 | |
| 202 | @lru_cache(maxsize=1) |
| 203 | def get(self, row_id): |