Skip to content

Commit a4e3b99

Browse files
committed
adding cache to the environment. Adding cached output and session to the app
1 parent b906c6b commit a4e3b99

File tree

6 files changed

+14
-4
lines changed

6 files changed

+14
-4
lines changed

azureproject/production.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'django.middleware.clickjacking.XFrameOptionsMiddleware',
2323
]
2424

25+
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
2526
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
2627
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
2728

azureproject/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
'django.middleware.clickjacking.XFrameOptionsMiddleware',
5454
]
5555

56+
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
5657
ROOT_URLCONF = 'azureproject.urls'
5758

5859
TEMPLATES = [

infra/resources.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
185185
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
186186
SECRET_KEY: secretKey
187187
//TODO: add settings for Redis Cache
188+
CACHELOCATION: 'rediss://${redisCache.name}.redis.cache.windows.net:6380/0'
189+
CACHEKEY: redisCache.listKeys().primaryKey
188190
}
189191
}
190192

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Django==4.2.5
22
psycopg2-binary==2.9.7
33
python-dotenv==1.0.0
4-
whitenoise==6.5.0
4+
whitenoise==6.5.0
5+
django-redis==5.3.0

restaurant_review/templates/restaurant_review/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
</style>
4343
{% endblock %}
4444
{% block content %}
45+
{% if LastViewedRestaurant %}
46+
<h3>Last viewed restaurant: {{ LastViewedRestaurant }}</h3>
47+
{% endif %}
4548
<h1>Restaurants</h1>
4649

4750
{% if restaurants %}

restaurant_review/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010

1111
# Create your views here.
1212

13-
@cache_page(60)
1413
def index(request):
1514
print('Request for index page received')
1615
restaurants = Restaurant.objects.annotate(avg_rating=Avg('review__rating')).annotate(review_count=Count('review'))
17-
return render(request, 'restaurant_review/index.html', {'restaurants': restaurants})
18-
16+
lastViewedRestaurant = request.session.get("lastViewedRestaurant", False)
17+
print(lastViewedRestaurant)
18+
return render(request, 'restaurant_review/index.html', {'LastViewedRestaurant': lastViewedRestaurant, 'restaurants': restaurants})
1919

20+
@cache_page(60)
2021
def details(request, id):
2122
print('Request for restaurant details page received')
2223
restaurant = get_object_or_404(Restaurant, pk=id)
24+
request.session["lastViewedRestaurant"] = restaurant.name
2325
return render(request, 'restaurant_review/details.html', {'restaurant': restaurant})
2426

2527

0 commit comments

Comments
 (0)