-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from nattvara/invalidate-query-caches
Add support for invalidating query caches
- Loading branch information
Showing
9 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
from db.crud import get_all_lectures | ||
|
||
|
||
def invalidate_all_query_caches(): | ||
print('invalidating all query caches') | ||
|
||
lectures = get_all_lectures() | ||
for lecture in lectures: | ||
print(f'invalidating cache for lecture {lecture}: ', end='') | ||
queries = lecture.queries() | ||
print(f'found {len(queries)} queries', end='') | ||
for query in queries: | ||
query.cache_is_valid = False | ||
query.save() | ||
print(' done.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
db/migrations/011_add_more_cache_is_valid_column_to_query_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Peewee migrations -- 011_add_more_cache_is_valid_column_to_query_table.py.""" | ||
import peewee as pw | ||
from peewee_migrate import Migrator | ||
|
||
from db.models import Query | ||
|
||
|
||
def migrate(migrator: Migrator, database: pw.Database, fake=False, **kwargs): | ||
"""Write your migrations here.""" | ||
field = pw.BooleanField(null=False, default=True) | ||
migrator.add_fields( | ||
Query, | ||
cache_is_valid=field, | ||
) | ||
migrator.run() | ||
|
||
|
||
def rollback(migrator: Migrator, database: pw.Database, fake=False, **kwargs): | ||
"""Write your rollback migrations here.""" | ||
migrator.remove_fields(Query, 'cache_is_valid', cascade=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
|
||
def test_query_can_be_made_about_lecture(mocker, api_client, analysed_lecture): | ||
mocker.patch('tools.text.ai.gpt3', return_value='gpt-3 response') | ||
|
||
response = api_client.post('/query', json={ | ||
'lecture_id': analysed_lecture.public_id, | ||
'language': analysed_lecture.language, | ||
'query_string': 'some interesting question', | ||
}) | ||
|
||
assert response.json()['response'] == 'gpt-3 response' | ||
|
||
|
||
def test_query_response_is_cached(mocker, api_client, analysed_lecture): | ||
gpt3 = mocker.patch('tools.text.ai.gpt3', return_value='gpt-3 response') | ||
|
||
def request(): | ||
return api_client.post('/query', json={ | ||
'lecture_id': analysed_lecture.public_id, | ||
'language': analysed_lecture.language, | ||
'query_string': 'some interesting question', | ||
}) | ||
|
||
response = request() | ||
response = request() | ||
|
||
assert response.json()['response'] == 'gpt-3 response' | ||
assert gpt3.call_count == 1 | ||
|
||
|
||
def test_query_response_cache_can_be_overridden(mocker, api_client, analysed_lecture): | ||
gpt3 = mocker.patch('tools.text.ai.gpt3', return_value='gpt-3 response') | ||
|
||
def request(): | ||
return api_client.post('/query', json={ | ||
'lecture_id': analysed_lecture.public_id, | ||
'language': analysed_lecture.language, | ||
'query_string': 'some interesting question', | ||
'override_cache': True, | ||
}) | ||
|
||
response = request() | ||
response = request() | ||
|
||
assert response.json()['response'] == 'gpt-3 response' | ||
assert gpt3.call_count == 2 | ||
|
||
|
||
def test_query_response_cache_can_be_invalidated(mocker, api_client, analysed_lecture): | ||
gpt3 = mocker.patch('tools.text.ai.gpt3', return_value='gpt-3 response') | ||
|
||
def make_query(query_string: str): | ||
return api_client.post('/query', json={ | ||
'lecture_id': analysed_lecture.public_id, | ||
'language': analysed_lecture.language, | ||
'query_string': query_string, | ||
}) | ||
|
||
def make_requests(): | ||
make_query('some query') | ||
make_query('some query') | ||
make_query('some other query') | ||
make_query('some third query') | ||
|
||
make_requests() | ||
assert gpt3.call_count == 3 | ||
|
||
# invalidate the cache | ||
queries = analysed_lecture.queries() | ||
|
||
for query in queries: | ||
query.cache_is_valid = False | ||
query.save() | ||
|
||
make_requests() | ||
assert gpt3.call_count == 6 |