diff --git a/CodeStreak/__init__.py b/CodeStreak/__init__.py index e69de29..a180653 100644 --- a/CodeStreak/__init__.py +++ b/CodeStreak/__init__.py @@ -0,0 +1,2 @@ +from johnny.cache import enable +enable() diff --git a/CodeStreak/contests/models/contest.py b/CodeStreak/contests/models/contest.py index d5e9780..be1f38a 100644 --- a/CodeStreak/contests/models/contest.py +++ b/CodeStreak/contests/models/contest.py @@ -1,17 +1,13 @@ from django.db import models, transaction, IntegrityError -from django.contrib.auth.models import User from django.core.urlresolvers import reverse as url_reverse from django.utils.timezone import now - -from caching.base import CachingMixin, CachingManager +from django.contrib.auth.models import User from CodeStreak.contests.models.participation import Participation from CodeStreak.contests.models.score import Score from CodeStreak.contests.models.log_entry import LogEntry - -class Contest(CachingMixin, models.Model): - objects = CachingManager() +class Contest(models.Model): UNASSIGNED = 0 STARTED = 1 diff --git a/CodeStreak/contests/models/log_entry.py b/CodeStreak/contests/models/log_entry.py index 8a2f395..9e33af2 100644 --- a/CodeStreak/contests/models/log_entry.py +++ b/CodeStreak/contests/models/log_entry.py @@ -1,10 +1,7 @@ from django.db import models from django.contrib.auth.models import User -from caching.base import CachingMixin, CachingManager - -class LogEntry(CachingMixin, models.Model): - objects = CachingManager() +class LogEntry(models.Model): CONTEST_STARTED = 1 CONTEST_PAUSED = 2 @@ -46,7 +43,9 @@ def get_log_entry(cls, log_id): @classmethod def get_all_entries(cls, contest_id, last_log_entry=None): - query = cls.objects.filter(contest__id=contest_id) + query = cls.objects.select_related( + 'user' + ).filter(contest__id=contest_id) if last_log_entry: query = query.filter(id__gt=last_log_entry) return query.all() diff --git a/CodeStreak/contests/models/participation.py b/CodeStreak/contests/models/participation.py index b870b8a..0da70ac 100644 --- a/CodeStreak/contests/models/participation.py +++ b/CodeStreak/contests/models/participation.py @@ -1,10 +1,7 @@ from django.db import models from django.contrib.auth.models import User -from caching.base import CachingMixin, CachingManager - -class Participation(CachingMixin, models.Model): - objects = CachingManager() +class Participation(models.Model): contest = models.ForeignKey('Contest') user = models.ForeignKey(User) diff --git a/CodeStreak/contests/models/score.py b/CodeStreak/contests/models/score.py index a69090c..bf17cc0 100644 --- a/CodeStreak/contests/models/score.py +++ b/CodeStreak/contests/models/score.py @@ -1,15 +1,12 @@ from django.db import models, transaction, IntegrityError -from django.contrib.auth.models import User from datetime import datetime - -from caching.base import CachingMixin, CachingManager +from django.contrib.auth.models import User from CodeStreak.contests.models.task import Task from CodeStreak.contests.models.log_entry import LogEntry from CodeStreak.contests.models.participation import Participation -class Score(CachingMixin, models.Model): - objects = CachingManager() +class Score(models.Model): SKIPPED = 0.5 FULL = 1.0 diff --git a/CodeStreak/contests/models/task.py b/CodeStreak/contests/models/task.py index 9777a81..e1b3b00 100644 --- a/CodeStreak/contests/models/task.py +++ b/CodeStreak/contests/models/task.py @@ -1,10 +1,6 @@ from django.db import models -from django.contrib.auth.models import User -from caching.base import CachingMixin, CachingManager - -class Task(CachingMixin, models.Model): - objects = CachingManager() +class Task(models.Model): UNASSIGNED = 0 EASY = 1 diff --git a/CodeStreak/contests/views.py b/CodeStreak/contests/views.py index 586c4f4..55e2f1d 100644 --- a/CodeStreak/contests/views.py +++ b/CodeStreak/contests/views.py @@ -452,7 +452,7 @@ def toggle_log(request, log_id): } -@login_required +# @login_required def data_provider(request, action): if request.is_ajax(): try: diff --git a/CodeStreak/settings.py b/CodeStreak/settings.py index 4b1ae8f..d5aace1 100644 --- a/CodeStreak/settings.py +++ b/CodeStreak/settings.py @@ -100,6 +100,7 @@ MIDDLEWARE_CLASSES = ( #'django.middleware.cache.UpdateCacheMiddleware', + 'johnny.middleware.QueryCacheMiddleware', ############# first ###################################################### 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -109,6 +110,7 @@ # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ############## last ###################################################### + 'johnny.middleware.LocalStoreClearMiddleware', # clears data at the end #'django.middleware.cache.FetchFromCacheMiddleware', ) @@ -196,16 +198,13 @@ DEFAULT_LOCATION = '127.0.0.1:11211' CACHES = { - 'default': { - 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', - 'LOCATION': DEFAULT_LOCATION, - 'TIMEOUT': 60, # we should modify this - 'KEY_PREFIX': DEFAULT_LOCATION, - } + 'default': { + 'BACKEND': 'johnny.backends.memcached.MemcachedCache', + 'LOCATION': [DEFAULT_LOCATION], + 'JOHNNY_CACHE': True, + } } -CACHE_BACKEND = 'caching.backends.memcached://{}'.format(CACHES['default']['LOCATION']) -CACHE_PREFIX = CACHES['default']['KEY_PREFIX'] -CACHE_COUNT_TIMEOUT = CACHES['default']['TIMEOUT'] / 2 # seconds, not too long. +JOHNNY_MIDDLEWARE_KEY_PREFIX = 'jc_CS' try: # import here to override, also catch error if file not here diff --git a/runserver b/runserver index c80ea85..1bf3e8a 100755 --- a/runserver +++ b/runserver @@ -1,6 +1,6 @@ # /bin/bash -# just inc ase, we define base python here +# just in case, we define base python here if [ -z $PYTHON ]; then PYTHON=python @@ -8,6 +8,21 @@ fi DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +MEMCACHED_PORT=11211 +MEMCACHED_PID_FILE=/tmp/memcached.pid + +start_memcached() +{ + memcached -d -p $MEMCACHED_PORT -P $MEMCACHED_PID_FILE + echo "Starting memcached" +} + +kill_memcached() +{ + kill $(cat $MEMCACHED_PID_FILE) + echo "Killing memcached" +} + start_uwsgi() { echo "Starting uWSGI" @@ -41,6 +56,10 @@ start_normal() $PYTHON manage.py runserver --settings=CodeStreak.settings_debug $@ } +############################################################################## +start_memcached +trap kill_memcached SIGINT +############################################################################## case $1 in "--uwsgi") shift