Skip to content

Commit

Permalink
Initial version of caching on top of memcached. Added runserver scrip…
Browse files Browse the repository at this point in the history
…t to start daemon as well.
  • Loading branch information
bmatican committed Jan 25, 2013
1 parent 16e6089 commit cb44cc3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
15 changes: 9 additions & 6 deletions CodeStreak/contests/models/contest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.db import models
from django.contrib.auth.models import User

from django.utils.timezone import now
from django.core.cache import cache

from CodeStreak.contests.models.task import Task

class Contest(models.Model):
CACHE_PREFIX = 'contest'
name = models.CharField(max_length=128)
start_date = models.DateTimeField(null=True, blank=True, default=None)
end_date = models.DateTimeField(null=True, blank=True, default=None)
Expand Down Expand Up @@ -36,17 +39,17 @@ def get_contest(cls, contest_id):

@classmethod
def get_task_ordering(cls, contest_id):
cached = False
if cached:
tasks = []
else:
cache_key = cls.CACHE_PREFIX + ":" + str(contest_id)
tasks = cache.get(cache_key)
if tasks == None:
tasks = cls.objects.get(
id=contest_id
).assigned_tasks.values(
'id'
)
tasks = [el['id'] for el in tasks]
return list(enumerate(tasks))
tasks = list(enumerate([el['id'] for el in tasks]))
cache.set(cache_key, tasks)
return tasks

def get_registered_user_count(self):
return self.registered_users.count()
Expand Down
12 changes: 12 additions & 0 deletions CodeStreak/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@
)

MIDDLEWARE_CLASSES = (
#'django.middleware.cache.UpdateCacheMiddleware',
############# first ######################################################
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
############## last ######################################################
#'django.middleware.cache.FetchFromCacheMiddleware',
)

ROOT_URLCONF = 'CodeStreak.urls'
Expand Down Expand Up @@ -186,6 +190,14 @@

AUTH_PROFILE_MODULE = 'django_facebook.FacebookProfile'

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 30,
}
}

try:
from local_settings import *
except ImportError:
Expand Down
22 changes: 22 additions & 0 deletions runserver
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# /bin/bash

MEMCACHED_PORT=11211

start_memcached()
{
memcached -p $MEMCACHED_PORT &
MEMCACHED_PID=$!
echo "Starting memcached: pid=$MEMCACHED_PID"
}

kill_memcached()
{
kill $MEMCACHED_PID
echo "Killing memcached: pid=$MEMCACHED_PID"
exit $?
}

# trap kill_memcached SIGINT

start_memcached
./manage.py runserver

0 comments on commit cb44cc3

Please sign in to comment.