Skip to content

Commit

Permalink
Fixes #17 - Implement Redis Caching (#22)
Browse files Browse the repository at this point in the history
* Fixes #17 - Implement Redis Caching

Also moves the MySQL volume into Docker

You need the setting REDIS_LOCATION in your .env which the value in
.env.sample works.

To test this do this when it's running, this will set and get from the
cache.

```
docker exec -it clrt_web /code/manage.py shell

Python 3.10.14 (main, Jul  2 2024, 22:12:36) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.cache import cache; cache.set('test_key', 'test_value', timeout=60);cache.get('test_key')
True
'test_value'
```

* Update backend/settings.py

* Just switch to using default Django Redis

* Adding Redis dependency

* Reverting mysql volume change
  • Loading branch information
jonespm authored Jul 12, 2024
1 parent 2f72256 commit 09997a6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ MYSQL_PASSWORD=clrt_pwd
MYSQL_HOST=clrt_mysql_host
MYSQL_PORT=3306

# Redis settings
REDIS_LOCATION=redis://clrt_redis_host:6379

#Django settings
SECRET_KEY='your-django-key'
DJANGO_DEBUG=True
Expand Down
9 changes: 3 additions & 6 deletions backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,13 @@
}
}

# This is needed to allow the LTI tool to work with the Django cache when running on multiple workers
CACHES = {
"default": {
"BACKEND": "django_mysql.cache.MySQLCache",
"LOCATION": "django_clrt_cache",
"KEY_PREFIX": "clrt",
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": config('REDIS_LOCATION', "redis://clrt_redis_host:6379")
}
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -214,4 +211,4 @@
CSP_STYLE_SRC = ["'self'", "https:", "'unsafe-inline'"]
CSP_FRAME_ANCESTORS = ["'self'"] + [url.strip() for url in config('CSP_FRAME_ANCESTORS', 'canvas-test.it.umich.edu').split(',')]
CSP_IMG_SRC = ["'self'", "data:"]
CSP_FONT_SRC = ["'self'"]
CSP_FONT_SRC = ["'self'"]
18 changes: 16 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ services:
ports:
- "4306:3306"
volumes:
- ./.data/mysql:/var/lib/mysql
- ./.data/mysql:/var/lib/mysql
container_name: clrt_mysql_host

redis:
image: redis:7
command: redis-server --stop-writes-on-bgsave-error no --save ""
# This is to simulate an Openshift container with no write permission except to /tmp
read_only: true
tmpfs:
- /tmp
volumes:
- clrt-redis-data:/data:ro
container_name: clrt_redis_host

web:
build:
context: .
Expand All @@ -30,4 +41,7 @@ services:
- mysql
env_file:
- .env
container_name: clrt_web
container_name: clrt_web

volumes:
clrt-redis-data:
13 changes: 13 additions & 0 deletions lti_redirect/migrations/0002_drop_mysql_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import migrations

class Migration(migrations.Migration):

dependencies = [
('lti_redirect', '0001_mysql_cache'), # Ensure this is the correct dependency
]

operations = [
migrations.RunSQL(
sql="DROP TABLE IF EXISTS django_clrt_cache;",
),
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ django-mysql==4.14.0
gunicorn==22.0.0
whitenoise==6.7.0
debugpy==1.8.2
redis==5.0.7

0 comments on commit 09997a6

Please sign in to comment.