Merge lp:~nigelbabu/harvest/cache-add into lp:harvest

Proposed by Nigel Babu
Status: Merged
Merged at revision: 316
Proposed branch: lp:~nigelbabu/harvest/cache-add
Merge into: lp:harvest
Diff against target: 56 lines (+24/-8)
2 files modified
harvest/common/views.py (+23/-7)
harvest/settings.py (+1/-1)
To merge this branch: bzr merge lp:~nigelbabu/harvest/cache-add
Reviewer Review Type Date Requested Status
Daniel Holbach Approve
Review via email: mp+71904@code.launchpad.net

Description of the change

Try to add some caching and reducing the DB load for '/'

To post a comment you must log in.
Revision history for this message
Daniel Holbach (dholbach) wrote :

Looks great and makes loading '/' less heavy, it's just not clear yet if it causes problems with users logging in. Can somebody provide some insight into that?

lp:~nigelbabu/harvest/cache-add updated
316. By Nigel Babu

Don't cache the entire view, just the queries

317. By Nigel Babu

Fix the bugs in my previous approach

318. By Nigel Babu

Use only one cache get call

Revision history for this message
Michael Hall (mhall119) wrote :

Opportunity.objects.only_valid().count()

is not the same as:

Opportunity.objects.filter(valid=True, last_updated=F('opportunitylist__last_updated')).count()

It adds an extra filter: opportunitylist__active=True

Revision history for this message
Daniel Holbach (dholbach) wrote :

> Opportunity.objects.only_valid().count()
>
> is not the same as:
>
> Opportunity.objects.filter(valid=True,
> last_updated=F('opportunitylist__last_updated')).count()
>
> It adds an extra filter: opportunitylist__active=True

I know, but it's intentional. We use the same check in a couple of others places, but we missed to change it here as well.

Revision history for this message
Daniel Holbach (dholbach) wrote :

Good work!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'harvest/common/views.py'
2--- harvest/common/views.py 2010-12-08 08:15:50 +0000
3+++ harvest/common/views.py 2011-08-17 17:06:24 +0000
4@@ -4,19 +4,35 @@
5 from django.http import HttpResponseRedirect
6 from django.shortcuts import render_to_response
7 from django.template import RequestContext
8+from django.core.cache import cache
9
10 from opportunities.models import SourcePackage, OpportunityList, Opportunity, Note
11
12 def index(request):
13-
14+ values = cache.get_many(['user_count', 'source_packages',
15+ 'opp_lists', 'opp', 'notes'])
16+ if 'user_count' not in values:
17+ values['user_count'] = User.objects.count()
18+ cache.set('user_count', values['user_count'], 60 * 5)
19+ if 'source_packages' not in values:
20+ values['source_packages'] = SourcePackage.objects.count()
21+ cache.set('source_packages', values['source_packages'], 60 * 5)
22+ if 'opp_lists' not in values:
23+ values['opp_lists'] = OpportunityList.objects.filter(active=True).count()
24+ cache.set('opp_lists', values['opp_lists'], 60 * 5)
25+ if 'opp' not in values:
26+ values['opp'] = Opportunity.objects.only_valid().count()
27+ cache.set('opp', values['opp'], 60 * 5)
28+ if 'notes' not in values:
29+ values['notes'] = Note.objects.count()
30+ cache.set('notes', values['notes'], 60 * 5)
31 context = {
32 'pageSection': 'home',
33- 'users': User.objects.count(),
34- 'sourcepackages': SourcePackage.objects.count(),
35- 'opportunitylists': OpportunityList.objects.filter(active=True).count(),
36- 'opportunities': Opportunity.objects.filter(valid=True,
37- last_updated=F('opportunitylist__last_updated')).count(),
38- 'notes': Note.objects.count(),
39+ 'users': values['user_count'],
40+ 'sourcepackages': values['source_packages'],
41+ 'opportunitylists': values['opp_lists'],
42+ 'opportunities': values['opp'],
43+ 'notes': values['notes'],
44 }
45 return render_to_response('index.html', context,
46 context_instance=RequestContext(request))
47
48=== modified file 'harvest/settings.py'
49--- harvest/settings.py 2011-06-28 13:42:41 +0000
50+++ harvest/settings.py 2011-08-17 17:06:24 +0000
51@@ -162,4 +162,4 @@
52 VERSION_NAME_STRING = utils.get_harvest_version_name(
53 os.path.join(PROJECT_PATH, "version"),
54 DEBUG)
55-
56+CACHE_BACKEND = 'locmem://'

Subscribers

People subscribed via source and target branches

to all changes: