Skip to content

Commit

Permalink
Switched to Johnny Cache. User caching works.
Browse files Browse the repository at this point in the history
Summary:
Now we get all queries cached directly.
Not sure if it scales that well, but it works...
Also, not sure if we will EVER have transaction problems. Hope not :)

Test Plan: Go anywhere, check runserver logs for DB. Should only have session.

Reviewers: bogdan2412

Reviewed By: bogdan2412

Differential Revision: http://phab.code4fun.de/D9
  • Loading branch information
bmatican committed Mar 7, 2013
1 parent ea0552a commit e50a2bf
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CodeStreak/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from johnny.cache import enable
enable()
8 changes: 2 additions & 6 deletions CodeStreak/contests/models/contest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 4 additions & 5 deletions CodeStreak/contests/models/log_entry.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions CodeStreak/contests/models/participation.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 2 additions & 5 deletions CodeStreak/contests/models/score.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 1 addition & 5 deletions CodeStreak/contests/models/task.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion CodeStreak/contests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def toggle_log(request, log_id):
}


@login_required
# @login_required
def data_provider(request, action):
if request.is_ajax():
try:
Expand Down
17 changes: 8 additions & 9 deletions CodeStreak/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@

MIDDLEWARE_CLASSES = (
#'django.middleware.cache.UpdateCacheMiddleware',
'johnny.middleware.QueryCacheMiddleware',
############# first ######################################################
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -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',
)

Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion runserver
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
# /bin/bash

# just inc ase, we define base python here
# just in case, we define base python here
if [ -z $PYTHON ];
then
PYTHON=python
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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e50a2bf

Please sign in to comment.